Skip to content

Commit 241f784

Browse files
authored
Extract HttpModule error extraction into a dedicated function (#6444)
1 parent aafa7cb commit 241f784

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

packages/angular/src/errorhandler.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ function tryToUnwrapZonejsError(error: unknown): unknown | Error {
3030
: error;
3131
}
3232

33+
function extractHttpModuleError(error: HttpErrorResponse): string | Error {
34+
// The `error` property of http exception can be either an `Error` object, which we can use directly...
35+
if (error.error instanceof Error) {
36+
return error.error;
37+
}
38+
39+
// ... or an`ErrorEvent`, which can provide us with the message but no stack...
40+
if (error.error instanceof ErrorEvent && error.error.message) {
41+
return error.error.message;
42+
}
43+
44+
// ...or the request body itself, which we can use as a message instead.
45+
if (typeof error.error === 'string') {
46+
return `Server returned code ${error.status} with body "${error.error}"`;
47+
}
48+
49+
// If we don't have any detailed information, fallback to the request message itself.
50+
return error.message;
51+
}
52+
3353
/**
3454
* Implementation of Angular's ErrorHandler provider that can be used as a drop-in replacement for the stock one.
3555
*/
@@ -104,23 +124,7 @@ class SentryErrorHandler implements AngularErrorHandler {
104124

105125
// If it's http module error, extract as much information from it as we can.
106126
if (error instanceof HttpErrorResponse) {
107-
// The `error` property of http exception can be either an `Error` object, which we can use directly...
108-
if (error.error instanceof Error) {
109-
return error.error;
110-
}
111-
112-
// ... or an`ErrorEvent`, which can provide us with the message but no stack...
113-
if (error.error instanceof ErrorEvent && error.error.message) {
114-
return error.error.message;
115-
}
116-
117-
// ...or the request body itself, which we can use as a message instead.
118-
if (typeof error.error === 'string') {
119-
return `Server returned code ${error.status} with body "${error.error}"`;
120-
}
121-
122-
// If we don't have any detailed information, fallback to the request message itself.
123-
return error.message;
127+
return extractHttpModuleError(error);
124128
}
125129

126130
// Nothing was extracted, fallback to default error message.

0 commit comments

Comments
 (0)