NodeJS assert does not throw passed error - node.js

I have just started using NodeJS's assert Module, and, given that I have understood it correctly, I can pass a custom error instance that should be thrown instead of an AssertionError if the assertion fails.
I.e., the first line in the following code throws an AssertionError and the second line throws an Error:
const assert = require('assert');
assert(false);
assert(false, new Error('abc'));
However, on my system, this does not seem to work. I have the following code:
try {
assert(false, new Error('abc'));
} catch (error) {
console.log(error.constructor.name);
}
and the Output I get is 'AssertionError'. Interestingly, when I try to do the same thing using the REPL, the output is the expected 'Error'.
I use NodeJS v15.8.0 in an alpine Docker container.

Related

Failing jest test when Expected & Received are the same

I'm running into a simple but annoying issue that I feel like I just don't understand how Jest parses and compares results.
it('should throw error when file is encrypted', async () => {
const uint8 = fs.readFileSync('file');
await expect(loadingFile(uint8Array)).rejects.toThrow(TypeError);
});
This test fails & in the terminal I see:
Expected constructor: TypeError
Received constructor: TypeError
typeof(error) on the error itself is an object.
How can I debug this?

Bluebird throwing exception at source code

I am using bluebirdjs for nodejs application. It throws an exception from its source code.
Line : try {throw new Error(); } catch (e) {ret.lastLineError = e;}
Path : bluebird/js/release/util.js
Line : 374
This exception seems unnecessary to me. It only throws exception. Is it rational to delete this line?
Same code also exists inside async.js at line 3.
In IE an Error object will not have a .stack property unless it goes a through try catch. The .stack property is needed to see which line and file the code is in.
ret.lastLineError = new Error() would therefore only work in firefox and chrome

vorpal .validate throwing error

I have a vorpal command that looks like this:
I am running version 1.9.5.
const vorpal = require('vorpal')()
vorpal
.command('temp [dev]')
.validate(function() {
return false
})
.action(function() {
...
})
vorpal.parse(process.argv)
And when i run pnt temp in my terminal I get this error thrown:
/Users/samm/Sites/pnt/node_modules/vorpal/dist/vorpal.js:169
throw new Error(err);
^
Error: null
at EventEmitter.<anonymous> (/Users/samm/Sites/pnt/node_modules/vorpal/dist/vorpal.js:169:17)
at callback (/Users/samm/Sites/pnt/node_modules/vorpal/dist/vorpal.js:830:22)
at /Users/samm/Sites/pnt/node_modules/vorpal/dist/vorpal.js:961:7
at EventEmitter._commandSetCallback (/Users/samm/Sites/pnt/node_modules/vorpal/dist/session.js:446:5)
at EventEmitter.session.completeCommand (/Users/samm/Sites/pnt/node_modules/vorpal/dist/session.js:526:12)
at onCompletion (/Users/samm/Sites/pnt/node_modules/vorpal/dist/session.js:456:10)
at EventEmitter.session.execCommandSet (/Users/samm/Sites/p nt/node_modules/vorpal/dist/session.js:471:5)
at EventEmitter.vorpal._exec (/Users/samm/Sites/pnt/node_modules/vorpal/dist/vorpal.js:960:18)
at EventEmitter.vorpal._execQueueItem (/Users/samm/Sites/pnt/node_modules/vorpal/dist/vorpal.js:751:17)
at EventEmitter.vorpal._queueHandler (/Users/samm/Sites/pnt/node_modules/vorpal/dist/vorpal.js:735:10)
I admit that this is looking ugly, and they will probably fix this in future versions, but this looks like the correct behaviour.
Returning false or a string from .validate should throw an error, as this means that you don't accept the arguments. If you return a string that string will be shown to the user (return "I don't want no scrubs.").
So from the code that you posted this is the correct behaviour.
If you like the errormessage from .validate to be different, I recommend you post an issue on vorpal's issue tracker. (Even though I can see you have already done that).

Error handling in Node.js for calling a function with wrong name

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.
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

[TypeError: Object # has no method 'replace']

First off all. I'm not posting questions about the error reason. This suddenly appeared when running an node.js app where after executing the query this error was received. I just want to know how to find the line in the source code where this error appears. How to set node to display extended info in errors like this?
So the error would be displayed like:
[TypeError: Object # has no method 'replace'] Test.js 123:98
put line 123 in try and catch the error
try{
// line 123
} catch(e){
throw e;
}
so it will print the stack of the error

Resources