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
Feature: Support cause
property on errors
#1401
Comments
Can be done with event hints in new SDK now |
@kamilogorek can you explain what you mean by "event hints" and how that will work with the |
Event and Breadcrumb hints are objects containing various information used to put together an event or a breadcrumb. For events, those are things like They are available in two places. Examples based on your
import { init } from '@sentry/browser';
init({
dsn: 'https://some@dsn.com/123',
beforeSend(event, hint) {
const processedEvent = { ...event };
const cause = hint.originalException.cause;
if (cause) {
processedEvent.message = cause.message;
}
return processedEvent;
},
beforeBreadcrumb(breadcrumb, hint) {
if (breadcrumb.category === 'ui.click') {
const target = hint.event.target;
if (target.ariaLabel) breadcrumb.message = target.ariaLabel;
}
return breadcrumb;
},
});
import { getCurrentHub } from '@sentry/browser';
getCurrentHub().configureScope(scope => {
scope.addEventProcessor(async (event, hint) => {
const processedEvent = { ...event };
const cause = hint.originalException.cause;
if (cause) {
processedEvent.message = cause.message;
}
return processedEvent;
});
}); |
hmm, if I understand correctly this would overwrite the original exception though, right? my hope was that it could display them in parallel like in Java/Python 🤔 |
Can you show me an example of desired output? |
This is an example of how it looks like in Java:
try {
somethingThatThrows();
} catch (innerError) {
let error = new Error('something failed!');
error.cause = innerError;
throw error;
} Without the |
The platform itself doesn't support this use-case, so there's only this much we can do on the SDK side. In JS, there's only one "main" exception, and we don't have an easy way to display more. I'd add this as an beforeSend(event, hint) {
const processedEvent = { ...event };
const cause = hint.originalException.cause;
if (cause) {
processedEvent.extra = {
...processedEvent.extra,
cause: {
type: cause.type
message: cause.message,
stack: cause.stack
}
}
}
return processedEvent;
} |
yeah, unfortunately the
do you not support this for Java/Python either? |
@Turbo87 like this? :) |
exactly! 😍 I'm not sure, but I think the order needs to be flipped though. I'd expect to see the "Parent process error" on top of the other one 🤔 |
It's PoC so far. I'll make it happen before the final release :) |
Great, thanks a lot!! |
@kamilogorek did this land yet? |
Is a |
@lily-controlzee yes, it's a default, out-of-the-box integration (called sentry-javascript/packages/browser/src/sdk.ts Lines 24 to 33 in 11bb8ed
|
Thanks! |
The TC39 error-cause proposal does not define a type for cause, and in fact in real implementations you can attach whatever metadata you want as a "cause" and in TypeScript it's defined as LinkedErrors just gives up if the cause is not an Error instance. I would expect it to capture non-Error cause values in some way rather than just chucking them out. |
the same is true for |
Sentry supports throwing non-Error objects just fine so I think that is actually a good example of why it should work with non-Error If you think this shouldn't be supported please take it up with TC39 (someone should, exceptions in JS are...not great) but since the spec and implementations allow it, people are going to do this even in code I don't control. |
The recommendation to throw an Error is because of stack information, and you don't always need stack information. All tooling and type systems for JS should acknowledge and allow any value to be thrown or caught, or set in |
Yes, would love to be able to attach more info to the Here's an example where I'm attaching the original error inside a custom object to capture more info: try {
const json = await res.json()
} catch (error) {
throw new Error('Something went wrong!', {
cause: {
problem: `Non-JSON response from GraphQL endpoint: ${GRAPHQL_PATH}`,
responseBody: res.body,
originalError: error,
},
})
} |
@Merott you can already do that for years now :) https://docs.sentry.io/platforms/javascript/configuration/integrations/plugin/#extraerrordata |
I believe the React error boundary feature is breaking the Error chain. sentry-javascript/packages/react/src/errorboundary.tsx Lines 84 to 91 in fd4db0d
I guess it should set errorBoundaryError.cause = error.cause; before overwriting the error cause. Edit I'm confused why it's not just creating a new error and setting the cause of the new error equal to the original error? Something like const boundaryError = new ReactBoundaryError("...");
boundaryError.cause = error;
captureException(boundaryError, ...); |
Yup, you're right that we overwrite the error cause in this case, which we shouldn't. We set the Let me see if this is a situation we can improve though |
Any update @AbhiPrasad ? Was also wondering if the Sentry UI supports rendering error cause properly? (including nested stacktraces?) |
Yup, it should work just fine by default, as SDK has |
Do you want to request a feature or report a bug?
feature
What is the current behavior?
With the current behavior only the outer/wrapping
Error
is shown in the stack trace.What is the desired behavior?
It would be great if the inner
Error
was also displayed in the stacktrace view, roughly similar to how it is done for Java and Python.I'm happy to help implement this with a bit of guidance :)
The text was updated successfully, but these errors were encountered: