Skip to content

Commit f052394

Browse files
authored
ref(angular) Add error-like objects handling (#6446)
It is more beneficial to handle error shaped objects instead of just instances of `Error`s
1 parent 7316b85 commit f052394

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

packages/angular/src/errorhandler.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { HttpErrorResponse } from '@angular/common/http';
22
import { ErrorHandler as AngularErrorHandler, Inject, Injectable } from '@angular/core';
33
import * as Sentry from '@sentry/browser';
44
import { captureException } from '@sentry/browser';
5-
import { addExceptionMechanism } from '@sentry/utils';
5+
import { addExceptionMechanism, isString } from '@sentry/utils';
66

77
import { runOutsideAngular } from './zone';
88

@@ -32,7 +32,7 @@ function tryToUnwrapZonejsError(error: unknown): unknown | Error {
3232

3333
function extractHttpModuleError(error: HttpErrorResponse): string | Error {
3434
// The `error` property of http exception can be either an `Error` object, which we can use directly...
35-
if (error.error instanceof Error) {
35+
if (isErrorOrErrorLikeObject(error.error)) {
3636
return error.error;
3737
}
3838

@@ -50,6 +50,31 @@ function extractHttpModuleError(error: HttpErrorResponse): string | Error {
5050
return error.message;
5151
}
5252

53+
type ErrorCandidate = {
54+
name?: unknown;
55+
message?: unknown;
56+
stack?: unknown;
57+
};
58+
59+
function isErrorOrErrorLikeObject(value: unknown): value is Error {
60+
if (value instanceof Error) {
61+
return true;
62+
}
63+
64+
if (value === null || typeof value !== 'object') {
65+
return false;
66+
}
67+
68+
const candidate = value as ErrorCandidate;
69+
70+
return (
71+
isString(candidate.name) &&
72+
isString(candidate.name) &&
73+
isString(candidate.message) &&
74+
(undefined === candidate.stack || isString(candidate.stack))
75+
);
76+
}
77+
5378
/**
5479
* Implementation of Angular's ErrorHandler provider that can be used as a drop-in replacement for the stock one.
5580
*/
@@ -117,16 +142,16 @@ class SentryErrorHandler implements AngularErrorHandler {
117142
protected _defaultExtractor(errorCandidate: unknown): unknown {
118143
const error = tryToUnwrapZonejsError(errorCandidate);
119144

120-
// We can handle messages and Error objects directly.
121-
if (typeof error === 'string' || error instanceof Error) {
122-
return error;
123-
}
124-
125145
// If it's http module error, extract as much information from it as we can.
126146
if (error instanceof HttpErrorResponse) {
127147
return extractHttpModuleError(error);
128148
}
129149

150+
// We can handle messages and Error objects directly.
151+
if (typeof error === 'string' || isErrorOrErrorLikeObject(error)) {
152+
return error;
153+
}
154+
130155
// Nothing was extracted, fallback to default error message.
131156
return null;
132157
}

packages/angular/test/errorhandler.test.ts

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CustomError extends Error {
3333
}
3434

3535
class ErrorLikeShapedClass implements Partial<Error> {
36-
constructor(public message: string) {}
36+
constructor(public name: string, public message: string) {}
3737
}
3838

3939
function createErrorEvent(message: string, innerError: any): ErrorEvent {
@@ -118,8 +118,7 @@ describe('SentryErrorHandler', () => {
118118
createErrorHandler().handleError(errorLikeWithoutStack);
119119

120120
expect(captureExceptionSpy).toHaveBeenCalledTimes(1);
121-
// TODO: to be changed; see https://github.com/getsentry/sentry-javascript/issues/6332
122-
expect(captureExceptionSpy).toHaveBeenCalledWith('Handled unknown error', expect.any(Function));
121+
expect(captureExceptionSpy).toHaveBeenCalledWith(errorLikeWithoutStack, expect.any(Function));
123122
});
124123

125124
it('extracts an error-like object with a stack', () => {
@@ -132,8 +131,7 @@ describe('SentryErrorHandler', () => {
132131
createErrorHandler().handleError(errorLikeWithStack);
133132

134133
expect(captureExceptionSpy).toHaveBeenCalledTimes(1);
135-
// TODO: to be changed; see https://github.com/getsentry/sentry-javascript/issues/6332
136-
expect(captureExceptionSpy).toHaveBeenCalledWith('Handled unknown error', expect.any(Function));
134+
expect(captureExceptionSpy).toHaveBeenCalledWith(errorLikeWithStack, expect.any(Function));
137135
});
138136

139137
it('extracts an object that could look like an error but is not (does not have a message)', () => {
@@ -150,7 +148,6 @@ describe('SentryErrorHandler', () => {
150148

151149
it('extracts an object that could look like an error but is not (does not have an explicit name)', () => {
152150
const notErr: Partial<Error> = {
153-
// missing name; but actually is always there as part of the Object prototype
154151
message: 'something failed.',
155152
};
156153

@@ -194,12 +191,12 @@ describe('SentryErrorHandler', () => {
194191
});
195192

196193
it('extracts an instance of class not extending Error but that has an error-like shape', () => {
197-
const err = new ErrorLikeShapedClass('something happened');
194+
const err = new ErrorLikeShapedClass('sentry-error', 'something happened');
198195

199196
createErrorHandler().handleError(err);
200197

201198
expect(captureExceptionSpy).toHaveBeenCalledTimes(1);
202-
expect(captureExceptionSpy).toHaveBeenCalledWith('Handled unknown error', expect.any(Function));
199+
expect(captureExceptionSpy).toHaveBeenCalledWith(err, expect.any(Function));
203200
});
204201

205202
it('extracts an instance of a class that does not extend Error and does not have an error-like shape', () => {
@@ -304,11 +301,7 @@ describe('SentryErrorHandler', () => {
304301
createErrorHandler().handleError(err);
305302

306303
expect(captureExceptionSpy).toHaveBeenCalledTimes(1);
307-
// TODO: to be changed; see https://github.com/getsentry/sentry-javascript/issues/6332
308-
expect(captureExceptionSpy).toHaveBeenCalledWith(
309-
'Http failure response for (unknown url): undefined undefined',
310-
expect.any(Function),
311-
);
304+
expect(captureExceptionSpy).toHaveBeenCalledWith(errorLikeWithoutStack, expect.any(Function));
312305
});
313306

314307
it('extracts an `HttpErrorResponse` with error-like object with a stack', () => {
@@ -322,11 +315,7 @@ describe('SentryErrorHandler', () => {
322315
createErrorHandler().handleError(err);
323316

324317
expect(captureExceptionSpy).toHaveBeenCalledTimes(1);
325-
// TODO: to be changed; see https://github.com/getsentry/sentry-javascript/issues/6332
326-
expect(captureExceptionSpy).toHaveBeenCalledWith(
327-
'Http failure response for (unknown url): undefined undefined',
328-
expect.any(Function),
329-
);
318+
expect(captureExceptionSpy).toHaveBeenCalledWith(errorLikeWithStack, expect.any(Function));
330319
});
331320

332321
it('extracts an `HttpErrorResponse` with an object that could look like an error but is not (does not have a message)', () => {
@@ -347,7 +336,6 @@ describe('SentryErrorHandler', () => {
347336

348337
it('extracts an `HttpErrorResponse` with an object that could look like an error but is not (does not have an explicit name)', () => {
349338
const notErr: Partial<Error> = {
350-
// missing name; but actually is always there as part of the Object prototype
351339
message: 'something failed.',
352340
};
353341
const err = new HttpErrorResponse({ error: notErr });
@@ -453,16 +441,13 @@ describe('SentryErrorHandler', () => {
453441
});
454442

455443
it('extracts an `HttpErrorResponse` with an instance of class not extending Error but that has an error-like shape', () => {
456-
const innerErr = new ErrorLikeShapedClass('something happened');
444+
const innerErr = new ErrorLikeShapedClass('sentry-error', 'something happened');
457445
const err = new HttpErrorResponse({ error: innerErr });
458446

459447
createErrorHandler().handleError(err);
460448

461449
expect(captureExceptionSpy).toHaveBeenCalledTimes(1);
462-
expect(captureExceptionSpy).toHaveBeenCalledWith(
463-
'Http failure response for (unknown url): undefined undefined',
464-
expect.any(Function),
465-
);
450+
expect(captureExceptionSpy).toHaveBeenCalledWith(innerErr, expect.any(Function));
466451
});
467452

468453
it('extracts an `HttpErrorResponse` with an instance of a class that does not extend Error and does not have an error-like shape', () => {

0 commit comments

Comments
 (0)