Skip to content

ref(angular): Extract zonejs error unwrapper into a dedicated function #6443

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

Merged
merged 1 commit into from
Dec 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions packages/angular/src/errorhandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ export interface ErrorHandlerOptions {
extractor?(error: unknown, defaultExtractor: (error: unknown) => unknown): unknown;
}

// https://github.com/angular/angular/blob/master/packages/core/src/util/errors.ts
function tryToUnwrapZonejsError(error: unknown): unknown | Error {
// TODO: once Angular14 is the minimum requirement ERROR_ORIGINAL_ERROR and
// getOriginalError from error.ts can be used directly.
return error && (error as { ngOriginalError: Error }).ngOriginalError
? (error as { ngOriginalError: Error }).ngOriginalError
: error;
}

/**
* Implementation of Angular's ErrorHandler provider that can be used as a drop-in replacement for the stock one.
*/
Expand Down Expand Up @@ -86,13 +95,7 @@ class SentryErrorHandler implements AngularErrorHandler {
* Default implementation of error extraction that handles default error wrapping, HTTP responses, ErrorEvent and few other known cases.
*/
protected _defaultExtractor(errorCandidate: unknown): unknown {
let error = errorCandidate;

// Try to unwrap zone.js error.
// https://github.com/angular/angular/blob/master/packages/core/src/util/errors.ts
if (error && (error as { ngOriginalError: Error }).ngOriginalError) {
error = (error as { ngOriginalError: Error }).ngOriginalError;
}
const error = tryToUnwrapZonejsError(errorCandidate);

// We can handle messages and Error objects directly.
if (typeof error === 'string' || error instanceof Error) {
Expand Down