Skip to content

Commit

Permalink
feat(browser): Allow mechanism to be provided as a hint.
Browse files Browse the repository at this point in the history
 - Provide 'angular' as the mechanism for handled angular errors.
  • Loading branch information
onurtemizkan committed Jul 12, 2022
1 parent ce67a38 commit 039919d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/angular/src/errorhandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpErrorResponse } from '@angular/common/http';
import { ErrorHandler as AngularErrorHandler, Inject, Injectable } from '@angular/core';
import * as Sentry from '@sentry/browser';
import { getCurrentHub } from '@sentry/browser';

import { runOutsideAngular } from './zone';

Expand Down Expand Up @@ -40,7 +41,9 @@ class SentryErrorHandler implements AngularErrorHandler {
const extractedError = this._extractError(error) || 'Handled unknown error';

// Capture handled exception and send it to Sentry.
const eventId = runOutsideAngular(() => Sentry.captureException(extractedError));
const eventId = runOutsideAngular(() =>
getCurrentHub().captureException(extractedError, { data: { mechanism: { type: 'angular', handled: false } } }),
);

// When in development mode, log the error to console for immediate feedback.
if (this._options.logErrors) {
Expand Down
22 changes: 20 additions & 2 deletions packages/browser/src/eventbuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Event, EventHint, Exception, Severity, SeverityLevel, StackFrame, StackParser } from '@sentry/types';
import {
Event,
EventHint,
Exception,
Mechanism,
Severity,
SeverityLevel,
StackFrame,
StackParser,
} from '@sentry/types';
import {
addExceptionMechanism,
addExceptionTypeValue,
Expand Down Expand Up @@ -149,8 +158,17 @@ export function eventFromException(
): PromiseLike<Event> {
const syntheticException = (hint && hint.syntheticException) || undefined;
const event = eventFromUnknownInput(stackParser, exception, syntheticException, attachStacktrace);
addExceptionMechanism(event); // defaults to { type: 'generic', handled: true }
const providedMechanism: Mechanism | undefined =
hint && hint.data && (hint.data as { mechanism: Mechanism }).mechanism;
const mechanism: Mechanism = providedMechanism || {
handled: true,
type: 'generic',
};

addExceptionMechanism(event, mechanism);

event.level = 'error';

if (hint && hint.event_id) {
event.event_id = hint.event_id;
}
Expand Down

0 comments on commit 039919d

Please sign in to comment.