Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What properties does Node.js express's Error object expose?

I would like to know what are the functions the Error object of nodejs express exposes for use in Error Handling?

A console.log of an error call new Error('NotFound') is showing only [Error: NotFound], is this because .toString() method is overriden? How do find the properties and functions exposed by these objects?

like image 630
shahalpk Avatar asked May 16 '12 18:05

shahalpk


People also ask

What does error captureStackTrace do?

captureStackTrace returns a string that represents the location of that particular error in the call. It gives us a stack that helps us to find the location of that error in the code at which new Error() was Called. this will help us to find the exact error in our code.

How do you declare errors in node JS?

An error in Node. js is any instance of the Error object. Common examples include built-in error classes, such as ReferenceError , RangeError , TypeError , URIError , EvalError , and SyntaxError .


1 Answers

The Error object is actually a native object provided by V8, rather than by node.js or express.

The property that will most likely be of the most use to you is stack. E.g.,

console.log(new Error('NotFound').stack); 

There are other properties available as well, such as name and message. You can read up on them here. Just be aware that those docs are for Mozilla's JavaScript engine, so don't count on anything flagged as Non-standard to work in node.js.

like image 110
jmar777 Avatar answered Sep 17 '22 13:09

jmar777