Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console.error enhancement #4452

Closed
DylanPiercey opened this issue Dec 28, 2015 · 7 comments
Closed

console.error enhancement #4452

DylanPiercey opened this issue Dec 28, 2015 · 7 comments
Labels
console Issues and PRs related to the console subsystem. feature request Issues that request new features to be added to Node.js.

Comments

@DylanPiercey
Copy link

I'm not sure exactly how this works and if this is the right place to raise the issue but:

In the browser (chrome)

console.error(new Error);

// Logs the error and an expandable stack trace.
Error()
    (anonymous function) @ VM297:2
    InjectedScript._evaluateOn @VM174:878
    InjectedScript._evaluateAndWrap @ VM174:811
    InjectedScript.evaluate @ VM174:667

In node:

console.error(new Error)
[Error]

// To properly show the stack trace I would have to console.error the stack.
// In the browser this causes the stack to be visible twice.
console.error((new Error).stack)

Is it possible to make it so that node will automatically add stack traces on console.error?

@mscdex mscdex added console Issues and PRs related to the console subsystem. feature request Issues that request new features to be added to Node.js. labels Dec 28, 2015
@julianduque
Copy link
Contributor

I like this suggestion

@zeusdeux
Copy link
Contributor

zeusdeux commented Jan 8, 2016

This will impact behaviour of console.warn as well since in the browser, even that prints the stack trace.
Also, so does console.log in chrome/firefox/etc.

So, from what I gather, it's the way util.format handles Error objects that needs to change.

@evanlucas
Copy link
Contributor

I always forget to add the check for err.stack || err and it drives me nuts. I would be +1 on this, but it would be a semver-major change.

@targos
Copy link
Member

targos commented Jan 8, 2016

I would also like this, but on the other hand I'm sure there are a lot of libraries out there that use console.error to output text to stderr (I often do it myself in my utility scripts) so it would definitely be semver-major.

@evanlucas
Copy link
Contributor

Yea I think it should be a change to util.inspect though for Errors. I think that would make the change less breaking to the ecosystem (although still a major change)

@zeusdeux
Copy link
Contributor

zeusdeux commented Jan 8, 2016

I agree that the change should be in util.inspect. The way Errors are displayed could be changed in formatError.

Edit: I can pick this up and make the changes in formatError if someone hasn't already picked this up.
Edit 2: Had some time so got it done.

@GeorgeBailey
Copy link

Certainly util.inspect is the right place. +1 this change for the next major version.

zeusdeux added a commit to zeusdeux/node that referenced this issue Jan 8, 2016
Previously, Error objects were formatted as the result of a `toString`
call bounded by square brackets. They are now formatted as the stack
trace for the given error object. The intention initially was to emulate
how browsers do `console.error` but since that would also impact
`console.warn`, `console.log`, etc, it was decided to make the change at
`util.inspect` level which is in turn used by the `console` package.

Fixes: nodejs#4452
scovetta pushed a commit to scovetta/node that referenced this issue Apr 2, 2016
Previously, Error objects were formatted as the result of a `toString`
call bounded by square brackets. They are now formatted as the stack
trace for the given error object. The intention initially was to emulate
how browsers do `console.error` but since that would also impact
`console.warn`, `console.log`, etc, it was decided to make the change at
`util.inspect` level which is in turn used by the `console` package.

Fixes: nodejs#4452
PR-URL: nodejs#4582
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
console Issues and PRs related to the console subsystem. feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

No branches or pull requests

7 participants