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

Wrapped errors from sendMessage should include the original error name, stacktrace #210

Open
lguychard opened this issue Oct 31, 2019 · 10 comments

Comments

@lguychard
Copy link

Errors returned by onMessage listeners are currently converted to JSON:

// Send a JSON representation of the error if the rejected value
// is an instance of error, or the object itself otherwise.
let message;
if (error && (error instanceof Error ||
typeof error.message === "string")) {
message = error.message;
} else {
message = "An unexpected error occurred";
}
sendResponse({
__mozWebExtensionPolyfillReject__: true,
message,
});

At present, only the message property of the original error is sent in the response. In the case where the rejected value is an Error instance, it would be nice to include other structured clone serializable properties from the original error, such as name and stacktrace.

@Rob--W
Copy link
Member

Rob--W commented Nov 7, 2019

This could be useful for debugging, but adds complexity and overhead to the implementation. Most users probably don't need this functionality.

Therefore we'll close this bug.

@Rob--W Rob--W closed this as completed Nov 7, 2019
@felixfbecker
Copy link

but adds complexity and overhead to the implementation

If we just copy over two additional properties that every Error has, name and stack, would that still be too much complexity and overhead? That should be only
two lines of code.

Most users probably don't need this functionality.

I'd actually argue the opposite extreme: Wanting to see the stack traces of errors is not a niche use case, but rather something 100% of the users of the polyfill would want.

@Rob--W
Copy link
Member

Rob--W commented Nov 7, 2019

If we just copy over two additional properties that every Error has, name and stack, would that still be too much complexity and overhead? That should be only two lines of code.

To implement this, we would need to special-case errors and add the stack here:

if (error && (error instanceof Error ||

Then at the other side, we would have to reconstruct the (typed) error object at

reject(new Error(reply.message));
.

In terms of code, it is not very difficult, but nonetheless it's more than nothing.

Wanting to see the stack traces of errors is not a niche use case, but rather something 100% of the users of the polyfill would want.

I'm sympathic to the idea of stack traces (I first-hand experience the pain of some bugs in missing/incorrect stack traces in Firefox during some unit tests), but I still maintain that it's mainly a development feature, and not something that can or should be relied upon for control flow in the extension (chrome.runtime.lastError doesn't have anything else besides .message, and the stack is not even documented on MDN).

But if rejected messages are expected to be rare, then the overhead of supporting stack traces are not as bad as I thought. I may consider re-opening the feature request (and review+accept a pull request), if @rpl agrees to.

@rpl
Copy link
Member

rpl commented Nov 7, 2019

@Rob--W sure, I'm also ok with re-opening this and marking it as "contrib: welcome".

@lguychard
Copy link
Author

I'll happily send up a PR for this.

@franciscolourenco
Copy link

Stack traces are needed. @lguychard are you actively working on this?

@franciscolourenco
Copy link

@lguychard any news? Thanks!

@philipp-tailor
Copy link

Then at the other side, we would have to reconstruct the (typed) error object at

reject(new Error(reply.message));

.

+1 for the idea of converting error objects with a message as Error instances!

I'm contributing to an extension where rejections are shown as UnhandledRejection: Non-Error promise rejection captured with keys: message in sentry because the argument of the rejection is not an instance of Error. That makes it unnecessary hard to maintain / monitor the extension.

If this change would be welcome, I could be open to contributing it.

@philipp-tailor
Copy link

philipp-tailor commented Apr 9, 2021

Actually I'm realizing that the change I proposed was issue #257, which has been implemented with #293. Thank you! 👏🏼

@fregante
Copy link
Contributor

It might be useful to use serialize-errors, if it matches Firefox’ behavior: https://github.com/sindresorhus/serialize-error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants