Handle HAL and HTTP errors fully within the sentry reporter for filtering

pull/9728/head
Oliver Günther 3 years ago
parent 39c8ad59b4
commit 75cbc832ab
No known key found for this signature in database
GPG Key ID: A3A8BDAD7C0C552C
  1. 28
      frontend/src/app/core/errors/sentry/sentry-reporter.ts
  2. 22
      frontend/src/app/features/hal/services/hal-aware-error-handler.ts

@ -36,6 +36,7 @@ import { environment } from '../../../../environments/environment';
import { EventHint } from '@sentry/angular'; import { EventHint } from '@sentry/angular';
import { HttpErrorResponse } from '@angular/common/http'; import { HttpErrorResponse } from '@angular/common/http';
import { debugLog } from 'core-app/shared/helpers/debug_output'; import { debugLog } from 'core-app/shared/helpers/debug_output';
import { HalError } from 'core-app/features/hal/services/hal-error';
export type ScopeCallback = (scope:Scope) => void; export type ScopeCallback = (scope:Scope) => void;
export type MessageSeverity = 'fatal'|'error'|'warning'|'log'|'info'|'debug'; export type MessageSeverity = 'fatal'|'error'|'warning'|'log'|'info'|'debug';
@ -103,10 +104,10 @@ export class SentryReporter implements ErrorReporter {
switch (samplingContext.transactionContext.op) { switch (samplingContext.transactionContext.op) {
case 'op': case 'op':
case 'navigation': case 'navigation':
// Trace 1% of page loads and navigation events // Trace 1% of page loads and navigation events
return Math.min(0.01 * traceFactor, 1.0); return Math.min(0.01 * traceFactor, 1.0);
default: default:
// Trace 0.1% of requests // Trace 0.1% of requests
return Math.min(0.001 * traceFactor, 1.0); return Math.min(0.001 * traceFactor, 1.0);
} }
}, },
@ -215,8 +216,13 @@ export class SentryReporter implements ErrorReporter {
// avoid duplicate requests on thrown angular errors, they // avoid duplicate requests on thrown angular errors, they
// are handled by the hal error handler // are handled by the hal error handler
// https://github.com/getsentry/sentry-javascript/issues/2532#issuecomment-875428325 // https://github.com/getsentry/sentry-javascript/issues/2532#issuecomment-875428325
if (hint?.originalException instanceof HttpErrorResponse) { const exception = hint?.originalException;
return null; if (exception instanceof HttpErrorResponse) {
return SentryReporter.filterHttpError(event, exception);
}
if (exception instanceof HalError) {
return SentryReporter.filterHttpError(event, exception.httpError);
} }
const unsupportedBrowser = document.body.classList.contains('-unsupported-browser'); const unsupportedBrowser = document.body.classList.contains('-unsupported-browser');
@ -227,4 +233,18 @@ export class SentryReporter implements ErrorReporter {
return event; return event;
} }
/**
* Filter http errors before sending to sentry. For now, we only want 5xx+ errors
* @param event
* @param exception
* @private
*/
private static filterHttpError(event:SentryEvent, exception:HttpErrorResponse):SentryEvent|null {
if (exception.status >= 500) {
return event;
}
return null;
}
} }

@ -35,7 +35,7 @@ export class HalAwareErrorHandler extends ErrorHandler {
if (error instanceof HalError) { if (error instanceof HalError) {
console.error('Returned HTTP HAL error resource %O', error.message); console.error('Returned HTTP HAL error resource %O', error.message);
message = error.httpError?.status >= 500 ? `${message} ${error.message}` : error.message; message = error.httpError?.status >= 500 ? `${message} ${error.message}` : error.message;
HalAwareErrorHandler.captureHttpError(error.httpError); HalAwareErrorHandler.captureError(error.httpError);
} else if (error instanceof ErrorResource) { } else if (error instanceof ErrorResource) {
console.error('Returned error resource %O', error); console.error('Returned error resource %O', error);
message += ` ${error.errorMessages.join('\n')}`; message += ` ${error.errorMessages.join('\n')}`;
@ -43,9 +43,9 @@ export class HalAwareErrorHandler extends ErrorHandler {
console.error('Returned hal resource %O', error); console.error('Returned hal resource %O', error);
message += `Resource returned ${error.name}`; message += `Resource returned ${error.name}`;
} else if (error instanceof Error) { } else if (error instanceof Error) {
HalAwareErrorHandler.reportError(error); window.ErrorReporter.captureException(error);
} else if (error instanceof HttpErrorResponse) { } else if (error instanceof HttpErrorResponse) {
HalAwareErrorHandler.captureHttpError(error); HalAwareErrorHandler.captureError(error);
message = error.message; message = error.message;
} else if (typeof error === 'string') { } else if (typeof error === 'string') {
window.ErrorReporter.captureMessage(error); window.ErrorReporter.captureMessage(error);
@ -55,18 +55,14 @@ export class HalAwareErrorHandler extends ErrorHandler {
super.handleError(message); super.handleError(message);
} }
private static reportError(error:Error):void {
window.ErrorReporter.captureException(error);
}
/** /**
* Report any 5xx errors to sentry, if configured. * Report any errors to sentry, if configured.
* @param httpError * Sentry will filter according to their error status
*
* @param error
* @private * @private
*/ */
private static captureHttpError(httpError:HttpErrorResponse):void { private static captureError(error:Error|HttpErrorResponse):void {
if (httpError.status >= 500) { window.ErrorReporter.captureException(error);
HalAwareErrorHandler.reportError(httpError);
}
} }
} }

Loading…
Cancel
Save