question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extending error object in express, but Debug does only show the stack trace

See original GitHub issue

Hello,

Small question of which I don’t know it’s a bug or expected behaviour:

In my express application I have the following app.use at the bottom:

app.use( (error, req, res, next) => {
    const status = error.statusCode || 500;
    const source = error.source || 'Unknown';
    const message = error.message;
    const info = error.info || '';
    console.log(error); // For logging on the server
    res.status(status).json({
        status: status,
        source: source,
        message: message,
        info: info
    })
});

This shows me the stack trace and the error object (with source and info properties).

When I change the console.log to debug, it only shows the stack trace and not the error object.

Why is that? And how can I show the error object with the debug command also?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Qix-commented, Jun 21, 2019

Aha, I thought this got fixed a while back. Apparently not. Thanks for the repro case, by the way 😃

The first argument is not util.format’d where it should be.

The standard .toString() on Error objects does not include extraneous information. console.log() is, more or less, just (...args) => stdout.write(util.format(...args)). That’s why it’s showing the extra information.

For now, use a format string:

debug('%O', error); // 'O' as in 'Open', not a zero.

This will get fixed in the future.

0reactions
RAnders00commented, Jul 5, 2019

I get the same behaviour using the make-error-cause package:

process.env.DEBUG = '*'

const log = require('debug')('test');
const { BaseError } = require('make-error-cause');

const cause = new Error('This is the cause!');
const error = new BaseError('This is the error!', cause);

log("---------")
log(error);
log("---------")
log("%O", error);
2019-07-05T20:26:56.917Z test BaseError: This is the error!
    at ...
2019-07-05T20:26:56.918Z test BaseError: This is the error!
    at ...

The following exception was the direct cause of the above exception:

Error: This is the cause!
    at ...
Read more comments on GitHub >

github_iconTop Results From Across the Web

Re-throwing exception in NodeJS and not losing stack trace
The problem with creating a new Error is you can lose metadata that was attached to the original Error that was thrown, the...
Read more >
Custom errors, extending Error - The Modern JavaScript Tutorial
Our function readUser(json) will not only read JSON, but check (“validate”) the data. If there are no required fields, or the format is...
Read more >
Best Practices for Node.js Error-handling - Toptal
Using Node.js built-in Error object is a good practice because it includes intuitive and clear information about errors like StackTrace, which most developers ......
Read more >
A Guide to Error Handling in Express.js | Scout APM Blog
Mishandled errors can lead to a bad UX and negatively affect your ... client with the error's status code, message, and even the...
Read more >
Error handling - Apollo GraphQL Docs
Your resolvers can also throw errors in situations where Apollo Server doesn't ... The stacktrace error field is useful while developing and debugging...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found