My understanding for the "right" way to make a custom Error class in JavaScript is something like this:
function MyError(message) {
this.name = "MyError";
this.message = message || "Default Message";
}
MyError.prototype = new Error();
MyError.prototype.constructor = MyError;
(Code snippet mooked from https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error.)
With NodeJS, if I try to check for an error of this type like:
var err = new MyError("whoops");
assert.ifError(err);
...the backtrace will show the context of the Error object I created at compile time to be the prototype for MyError, not the MyError object I created with "new MyError()".
Is there some way that I can get the correct backtrace data for the actual error, rather than the prototype?
To create a custom exception, we have to extend the java. lang. Exception class. Note that we also have to provide a constructor that takes a String as the error message and called the parent class constructor.
While the majority of exceptions are implementations of the global Error class, any old object can be thrown. With this in mind, there are two ways to throw an exception: directly via an Error object, and through a custom object.
We need to invoke the super function - captureStackTrace
var util = require('util');
function MyError(message) {
Error.call(this); //super constructor
Error.captureStackTrace(this, this.constructor); //super helper method to include stack trace in error object
this.name = this.constructor.name; //set our function’s name as error name.
this.message = message; //set the error message
}
// inherit from Error
util.inherits(MyError, Error);
You can use this node module to extend Error types easily https://github.com/jayyvis/extend-error
@Jay Kumar, has one good answer here. However, maybe here is another similar solution here
module.exports = function CustomError(message, extra) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
this.extra = extra;
};
require('util').inherits(module.exports, Error);
Error.call(this) - creates another error object (wasting a bunch of time) and doesn't touch this at all
Since the ECMAScript6
could be supported in the latest Node.js
version. The answer under ES6
could be refer to this link.
class MyError extends Error {
constructor(message) {
super(message);
this.message = message;
this.name = 'MyError';
}
}
Here are test codes under Node v4.2.1
class MyError extends Error{
constructor(msg, extra) {
super(msg);
this.message = msg;
this.name = 'MyError';
this.extra = extra;
}
};
var myerr = new MyError("test", 13);
console.log(myerr.stack);
console.log(myerr);
Output:
MyError: test
at MyError (/home/bsadmin/test/test.js:5:8)
at Object.<anonymous> (/home/bsadmin/test/test.js:12:13)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:134:18)
at node.js:961:3
{ [MyError: test] name: 'MyError', extra: 13 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With