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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

check before running captureStackTrace #4

Merged
merged 1 commit into from
Apr 3, 2020

Conversation

salvocanna
Copy link
Contributor

@salvocanna salvocanna commented Mar 13, 2020

This little lib is used by both node and frontend, and because some browser might not implement captureStackTrace, it will throw an exception, badly.

This PR is meant to safe check / fix this call and make it safe for the web 馃暦馃暩馃憣

@coveralls
Copy link

Coverage Status

Coverage increased (+1.2%) to 58.824% when pulling fd1a81e on check-capture-stack-trace into 85eeb58 on master.

Comment on lines +19 to +20
if (Error.captureStackTrace)
Error.captureStackTrace(this, this.constructor);
Copy link

@deepakrb deepakrb Mar 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move to using the raw Error.stack instead of captureStackTrace (which is more V8 specific and not widely supported)

Suggested change
if (Error.captureStackTrace)
Error.captureStackTrace(this, this.constructor);
if (Error().stack)
this.stack = Error().stack;

captureStackTrace doesn't seem to do more than setting the .stack variable ourselves. It does support hiding some calls from the stack trace (anything from this.constructor in our case). Moving to the raw .stack call seems like a decent solution to get a wider supported stack trace.

https://nodejs.org/api/errors.html#errors_error_capturestacktrace_targetobject_constructoropt

Thoughts?

@deepakrb
Copy link

deepakrb commented Mar 16, 2020

The purpose of this change is to fix error resolution in the frontend.

We're aware that because captureStackTrace is not widely supported Firefox (and other browsers) would fail to resolve backend errors and their messages correctly.

Although this is a -node package it also seems to be used in the frontend so its probably best we add support for other web browsers which encounter this error.

@salvocanna
Copy link
Contributor Author

I was having a look at the docs and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack seems to be non-standard too, which makes me wonder about just using a polyfill like https://www.npmjs.com/package/error-polyfill
What do you think?

@g-wilson
Copy link
Contributor

It says non-standard but seems to have extremely good support?

Why don't we continue with captureStackTrace if it exists, then check Error.stack

Copy link
Contributor

@0xdeafcafe 0xdeafcafe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the Error.stack support is rather wonky. We also don't really need the stack for the kind of errors we throw with this package on the web.

@deepakrb
Copy link

Yeah, the Error.stack support is rather wonky. We also don't really need the stack for the kind of errors we throw with this package on the web.

I would rather we just used the .stack method (which is generally supported) behind a check than use a V8 specific API behind a check. The V8 captureStackTrace is so simple we may as well use the more supported Error().stack

@salvocanna
Copy link
Contributor Author

Stack seems to be already there by default it's not serialisable, adding it manually with .stack = would mean it becomes serialisable (which I guess we could fix) but I fear this might introduce some other unexpected changes for backend and I fear opening that can of worms.
I'd merge it like this as it is

@salvocanna salvocanna merged commit d9d74ff into master Apr 3, 2020
@salvocanna salvocanna deleted the check-capture-stack-trace branch April 3, 2020 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants