0

I am requiring a module and saving it in a variable. But when I call a module function by wrong name, it does not throw any error or consoles any error. How do I make this throw error?

            var module = require('../pre_process/' + preProcessFolder + '/' + preProcessModule);
            // module -> { XYZ: [Function] }
            //Following does not throw error and doesn't console anything.How to handle/debug this error  
            module['XY'](result, userId)
              .then(function(recData) {

              })

I am using q library for promise.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
john doe
  • 806
  • 3
  • 14
  • 28
  • Is this entire code itself inside a promise's `.then()`? In that case it might be throwing but you may not have a `.catch()` outside. Checkout Bluebird promise library which is great for automatically logging such uncaught errors – laggingreflex Dec 06 '15 at 14:29
  • @jondoe: I ask myself, what do you want to do and why? It makes no sense for me, to include a module completely dynamicly. – apxp Dec 06 '15 at 18:03

1 Answers1

0

So you want to check if a function (provided by a modul) exists. You could use try like the example here: Javascript check if function exists

Community
  • 1
  • 1
apxp
  • 5,240
  • 4
  • 23
  • 43
  • Should'nt it throw some error if function does not exist and I am trying to call it? – john doe Dec 06 '15 at 13:38
  • @johndoe so it isn't throwing even inside a try catch? Are you really sure it doesn't exist? Can you `console.log(typeof module['XY'])` before calling it? – laggingreflex Dec 06 '15 at 14:34
  • @jondoe: maybe you can check typeof === "function" like here: http://www.sitepoint.com/call-javascript-function-string-without-using-eval/ – apxp Dec 06 '15 at 18:04