Custom error formatting

I made my own mistake:

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 am missing is to make it appear on the screen as if a regular uncontrolled error occurred, i.e. if we do throw new Error('Hello!') , we get the 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 when I do this, I want the same well formatted output:

 try { throw new MyError("Ops!"); } catch (e) { console.log(e); } 

but instead I get:

 { [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) automatically output the same beautifully formatted view for MyError without having to use an explicit e.stack link?

UPDATE: At first, I saw some recommendations regarding the toJSON method that I made, but that didn’t quite work. I assume that there should be an overridable method that console.log uses to format the error object, but what is it?

+7
javascript
source share
4 answers

Instead of overriding the toJSON method (or, as usual, toString ), you should override the inspect that is used when trying to console.log object in V8.

For example:

 MyError.prototype.inspect = function () { return this.stack; }; 

should probably do the trick.

+2
source share

try it

 try { throw new MyError("Ops!"); } catch (e) { console.log(e.stack); } 

If you execute this code in your browser console, it will look like this:

 MyError: Ops! at MyError.Error (native) at new MyError (<anonymous>:3:22) at <anonymous>:18:11 at Object.InjectedScript._evaluateOn (<anonymous>:875:140) at Object.InjectedScript._evaluateAndWrap (<anonymous>:808:34) at Object.InjectedScript.evaluate (<anonymous>:664:21) 

If I understand correctly what you want is to print a stack of your error?

0
source share

I wrote a function for handling uncaughtException errors so that the server does not stop on unhandled errors.

Function

 function errorHandler (){ process.on('uncaughtException', function (err) { console.log(err); // Displays the Error; console.error((new Date).toUTCString() + ' uncaughtException:', err.message); // Displays the Date and Error Message. console.error(err.stack); // Stack in Which the error occurred. }); } 

Exit

 ISODate("2015-07-28T04:56:20.000Z") uncaughtException: Cannot read property 'asset' of undefined TypeError: Cannot read property 'asset' of undefined\n at /Path/file.js:31:31\n at /path/node_modules/mongodb/lib/mongo_client.js:436:11\n at process._tickDomainCallback (node.js:463:13) 

Hope this helps.

0
source share

This is not highly recommended, but you can always hook up the console.log handler:

 var oldhandler = console.log; console.log = function () { if (typeof(arguments[0].stack) != "undefined" && arguments[0] instanceof(Error) && typeof(arguments[0].stack) != "undefined") { oldhandler.apply(console,[arguments[0].stack]); } else { oldhandler.apply(console,arguments); } } try { throw new Error("qweqew"); } catch (ex) { console.log(ex); } 

Fiddle: http://jsfiddle.net/xagpf5ff/

0
source share

All Articles