I have implemented my own custom error:
function MyError() {
var temp = Error.apply(this, arguments);
temp.name = this.name = 'MyError';
this.stack = temp.stack;
this.message = temp.message;
}
MyError.prototype = Object.create(Error.prototype, {
constructor: {
value: MyError,
writable: true,
configurable: true
}
});
And what I'm missing is to make it display itself on screen as it would when a regular unhanded error occurs, i.e. if we do throw new Error('Hello!')
, we get output:
throw new Error('Hello!');
^
Error: Hello!
at Object.<anonymous> (D:\NodeJS\tests\test1.js:28:7)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:138:18)
at node.js:974:3
Now I want the same nicely formatted output when I'm doing this:
try {
throw new MyError("Ops!");
} catch (e) {
console.log(e);
}
but instead I'm getting:
{ [MyError: Ops!]
name: 'MyError',
stack: 'MyError: Ops!\n at MyError.Error (native)\n at new MyError (D:\\NodeJS\\tests\\test1.js:2:22)\n at Object.<anonymous> (D:\\NodeJS\\tests\\test1.js:22:11)\n at Module._compile (module.js:425:26)\n at O
bject.Module._extensions..js (module.js:432:10)\n at Module.load (module.js:356:32)\n at Function.Module._load (module.js:313:12)\n at Function.Module.runMain (module.js:457:10)\n at startup (node.js:138:18)\n
at node.js:974:3',
message: 'Ops!' }
What else needs to be done to make console.log(e)
output the same nicely formatted presentation for MyError
automatically, without having to use explicit e.stack
reference?
UPDATE: At first I saw some recommendations regarding method toJSON
to be implemented, which I did, but it didn't quite work. I assume there must be an overridable method that console.log
uses in order to format an error object, but what is it then?
Now custom errors are much shorter, especially ValidationError, as we got rid of the "this.name = ..." line in the constructor. The purpose of the function readUser in the code above is “to read the user data”. There may occur different kinds of errors in the process.
When communicating more complicated error information to your users, or to your future self when debugging, sometimes these two mechanisms are not enough to adequately capture and report what has happened. To convey this more complex error information and attain more functionality, we can implement the standard library interface type, error.
Sometimes a custom error is the cleanest way to capture detailed error information. For example, let’s say we want to capture the status code for errors produced by an HTTP request; run the following program to see an implementation of error that allows us to cleanly capture that information:
For errors in network operations we may need HttpError, for database operations DbError, for searching operations NotFoundError and so on. Our errors should support basic error properties like message, name and, preferably, stack.
Instead of overriding the toJSON
method (or, toString
as is frequently assumed), you should override inspect
, which is used when trying to console.log
an object in V8.
For example:
MyError.prototype.inspect = function () {
return this.stack;
};
should probably do the trick.
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