Return-1
Return-1

Reputation: 2449

Printing an error object in javascript

When throwing an Error object in my promise chain, i attach a "details" property to it so that the handler can know what to do with it in the eventual catch block.

If logging is enabled, what i do in part of the chain is log the error and then rethrow it like so :

.somePartOfThePromiseChain
.catch(err => {
            console.log("ERROR");
            console.log(err.details);
            debugger;
            throw err;
          });

This works fine however, if i wanted to print the entire Error i dont get an object as expected but something that looks different like so :

Error: My error description here
    at API.js:105
    at <anonymous>

while i was expecting a collapsable object format as per usual in Chrome dev tools.

Im not sure why this happens as i essentially just want to print the Error object and view its members when debugging. This has nothing to do with me rethrowing the error as you can imagine.

Thanks a bunch.

Upvotes: 9

Views: 8676

Answers (2)

Sinan
Sinan

Reputation: 5980

Try wrapping it in another object.

Something like:

console.log( {error} );

Upvotes: 6

T.J. Crowder
T.J. Crowder

Reputation: 1073968

console.dir (MDN, Chrome devtools docs) does that on most consoles:

try {
  const e = new Error();
  e.details = "details go here";
  throw e;
} catch (ex) {
  console.dir(ex);
}
(Look in the real console.)

Upvotes: 8

Related Questions