Skip to content

Commit 6c7d87f

Browse files
committed
ref(angular) Add error-like objects handling
Closes #6332. As discussed in the linked issue, it is more beneficial to handle error shaped objects instead of just instances of `Error`s.
1 parent 2a063d9 commit 6c7d87f

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

packages/angular/src/errorhandler.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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,35 @@ function extractHttpModuleError(error: HttpErrorResponse): string | Error {
5050
return error.message;
5151
}
5252

53+
function isObject(value: unknown): value is Record<string, unknown> {
54+
return value !== null && typeof value === 'object';
55+
}
56+
57+
function hasOwnProperty<ObjectType extends Record<string, unknown>, PropertyType extends PropertyKey>(
58+
object: ObjectType,
59+
propertyName: PropertyType,
60+
): object is ObjectType & Record<PropertyType, unknown> {
61+
return Object.prototype.hasOwnProperty.call(object, propertyName);
62+
}
63+
64+
function hasErrorName(value: Record<string, unknown>): value is Pick<Error, 'name'> {
65+
return hasOwnProperty(value, 'name') && typeof value.name === 'string';
66+
}
67+
68+
function hasErrorMessage(value: Record<string, unknown>): value is Pick<Error, 'message'> {
69+
return hasOwnProperty(value, 'message') && typeof value.message === 'string';
70+
}
71+
72+
function hasErrorStack(value: Record<string, unknown>): value is Pick<Error, 'stack'> {
73+
return !hasOwnProperty(value, 'stack') || typeof value.stack === 'string';
74+
}
75+
76+
function isErrorOrErrorLikeObject(value: unknown): value is Error {
77+
return (
78+
value instanceof Error || (isObject(value) && hasErrorName(value) && hasErrorMessage(value) && hasErrorStack(value))
79+
);
80+
}
81+
5382
/**
5483
* Implementation of Angular's ErrorHandler provider that can be used as a drop-in replacement for the stock one.
5584
*/
@@ -117,16 +146,16 @@ class SentryErrorHandler implements AngularErrorHandler {
117146
protected _defaultExtractor(errorCandidate: unknown): unknown {
118147
const error = tryToUnwrapZonejsError(errorCandidate);
119148

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

154+
// We can handle messages and Error objects directly.
155+
if (typeof error === 'string' || isErrorOrErrorLikeObject(error)) {
156+
return error;
157+
}
158+
130159
// Nothing was extracted, fallback to default error message.
131160
return null;
132161
}

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)