最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Printing an error object in javascript - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Feb 1, 2018 at 13:23 Return-1Return-1 2,4494 gold badges22 silver badges58 bronze badges 1
  • In the above you're logging err.details, not err, but your description makes it sound like you're talking about logging err. – T.J. Crowder Commented Feb 1, 2018 at 13:26
Add a ment  | 

2 Answers 2

Reset to default 8

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.)

Try wrapping it in another object.

Something like:

console.log( {error} );
发布评论

评论列表(0)

  1. 暂无评论