user3136769
user3136769

Reputation: 3

Error Object undefined for Express application

I have setup an Express app. It seems that the global Error object is undefined.

console.log(Error) gives 'undefined' , and console.log(JSON.stringify(new Error('error message')) gives {}

Since Error object is undefined I cannot return errors like this

return next( new Error( 'error message!' ) 

Is it possible that Error object is renamed or something? Is there a work around this?

(I used IntelliJ Idea to construct the express app.)

Any help would be appreciated.

Upvotes: 0

Views: 107

Answers (1)

Trott
Trott

Reputation: 70055

Error object is not undefined. If it was, this would throw a TypeError:

console.log(JSON.stringify(new Error('error message')));

Instead, it is returning an empty object, because that is what V8 returns if you send an Error object into JSON.stringify(). I'm not sure if that's a feature or a bug or neither, but regardless, this will give you the result you expect:

console.log(new Error('error message'));

Upvotes: 1

Related Questions