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

Fix representation of simple object errors (currently "[object Object]") #597

Closed
wants to merge 1 commit into from
Closed

Fix representation of simple object errors (currently "[object Object]") #597

wants to merge 1 commit into from

Conversation

GlennKintscher
Copy link

@GlennKintscher GlennKintscher commented Jul 24, 2019

Description

If a library throws a simple object as an error, it is just displayed as "[object Object]", which is not very helpful for troubleshooting. This fix changes the behavior so that errors are converted to a JSON string if they are not of type Error.

Example

Location Current behaviour Expected behaviour
Console image image
Online image image

If a library throws a simple object as an error, it is just displayed as "[object Object]", which is not very helpful for troubleshooting. This fix changes the behavior so that errors are converted to a JSON string if they are not of type Error.
@GlennKintscher GlennKintscher marked this pull request as ready for review July 24, 2019 09:03
@mattdyoung
Copy link
Contributor

Hi @GlennKintscher

Thanks for the PR! This is a good idea which we'd like to take forward, but we have to be careful about trying to JSON stringify things as it can cause errors.

The readme for safe-json-stringify, which we maintain a fork of is quite informative https://github.com/debitoor/safe-json-stringify, so if converting to JSON we should ensure we use our safe-json-stringify module.

Alternatively, there is prior art in the repo for doing this with arbitrary args to for leaving console log arguments as breadcrumbs

let stringified = '[Unknown value]'
// this may fail if the input is:
// - an object whose [[Prototype]] is null (no toString)
// - an object with a broken toString or @@toPrimitive implementation
try { stringified = String(arg) } catch (e) {}
// if it stringifies to [object Object] attempt to JSON stringify
if (stringified === '[object Object]') {
// catch stringify errors and fallback to [object Object]
try { stringified = JSON.stringify(arg) } catch (e) {}
so we could factor that out and use it in both places.

We'll review this and either suggest the necessary changes or more likely factor this in to our own PR.

@mattdyoung mattdyoung added the feature request Request for a new feature label Jul 25, 2019
@abigailbramble abigailbramble added the backlog We hope to fix this feature/bug in the future label Jul 25, 2019
bengourley added a commit that referenced this pull request Jan 6, 2020
throw { ... } or reject({ ... }) is bad practice, but sometimes it happens. Previously (as
highlighted in #597), Bugsnag would just print [object Object], which is unhelpful. This
uses util.inspect() to get a better textual representation of anything that doesn't have
a .stack property, before logging it to the console.

Parially fixes #597
bengourley added a commit that referenced this pull request Jan 20, 2020
throw { ... } or reject({ ... }) is bad practice, but sometimes it happens. Previously (as
highlighted in #597), Bugsnag would just print [object Object], which is unhelpful. This
uses util.inspect() to get a better textual representation of anything that doesn't have
a .stack property, before logging it to the console.

Parially fixes #597
@mattdyoung mattdyoung added released This feature/bug fix has been released and removed backlog We hope to fix this feature/bug in the future labels May 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Request for a new feature released This feature/bug fix has been released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants