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

ref(angular) Add error-like objects handling #6446

Merged
merged 3 commits into from Jan 9, 2023

Conversation

theofidry
Copy link
Contributor

@theofidry theofidry commented Dec 6, 2022

Closes #6332.

As discussed in the linked issue, it is more beneficial to handle error shaped objects instead of just instances of Errors.


function isErrorOrErrorLikeObject(value: unknown): value is Error {
return (
value instanceof Error || (isObject(value) && hasErrorName(value) && hasErrorMessage(value) && hasErrorStack(value))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

of course this could all be inlined, but I find a lot more readable that way

Copy link
Member

Choose a reason for hiding this comment

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

It's very readable but we also have to consider the bundle size impact here. Hence my question if hasOwnProperty is strictly necessary.

Maybe, we could also inline the isObject check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not very familiar with how the code impacts the bundle size, is there a way I could check the result so that I can check different things to see if there is still a way to keep it readable without increasing the size too much?

Closes getsentry#6332.

As discussed in the linked issue, it is more beneficial to handle error shaped objects instead of just instances of `Error`s.
@theofidry theofidry force-pushed the feature/bugfix-error-like-objects branch from 4853d65 to 6c7d87f Compare December 16, 2022 18:06
@theofidry theofidry marked this pull request as ready for review December 16, 2022 18:07
}

function hasErrorName(value: Record<string, unknown>): value is Pick<Error, 'name'> {
return hasOwnProperty(value, 'name') && typeof value.name === 'string';
Copy link
Member

Choose a reason for hiding this comment

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

Not saying it's wrong but why do we need the hasOwnProperty check here? Wouldn't it be enough to check for typeof value.name === 'string'?

Or actually, for the string check, we could simply reuse our isString check from @sentry/utils.


function isErrorOrErrorLikeObject(value: unknown): value is Error {
return (
value instanceof Error || (isObject(value) && hasErrorName(value) && hasErrorMessage(value) && hasErrorStack(value))
Copy link
Member

Choose a reason for hiding this comment

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

It's very readable but we also have to consider the bundle size impact here. Hence my question if hasOwnProperty is strictly necessary.

Maybe, we could also inline the isObject check?

@theofidry
Copy link
Contributor Author

@Lms24 here's a simplified version. I can't say I'm happy, and I feel the code could be simpler, but that's a case where TS is getting a bit in the way; I can't just check isString(value.name) since it will complain that value is an object, so does not have the property name

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

Thanks for addressing my feedback! I see the TS problem and I think we just run with your solution for now.

This looks good to me now. Thanks so much for contributing!

Just a heads-up: We're not gonna make another release until after the holidays so I'll hold of from merging this PR until then.

@theofidry
Copy link
Contributor Author

👍

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

Successfully merging this pull request may close these issues.

"Non-Error exception captured with keys: [...]" due to invalid error instanceof check
3 participants