-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ref(angular): Set Angular ErrorHandler specific Exception Mechanism #3844
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
438e74f
feat(browser): Allow mechanism to be provided as a hint.
onurtemizkan 755a519
Use event processor to pass mechanism.
onurtemizkan 5d94e2a
Add `errorhandler` tests.
onurtemizkan 914aa56
Fix linter.
onurtemizkan bd1944d
Remove unneeded default fallback mechanism.
onurtemizkan e8b9a86
Fix linter
onurtemizkan 53a1d8b
Remove changes on `eventbuilder`.
onurtemizkan File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { createErrorHandler, SentryErrorHandler } from '../src/errorhandler'; | ||
|
||
import * as SentryBrowser from '@sentry/browser'; | ||
import * as SentryUtils from '@sentry/utils'; | ||
import { HttpErrorResponse } from '@angular/common/http'; | ||
import { Scope } from '@sentry/browser'; | ||
|
||
const FakeScope = new Scope(); | ||
|
||
jest.mock('@sentry/browser', () => { | ||
const original = jest.requireActual('@sentry/browser'); | ||
return { | ||
...original, | ||
captureException: (err: unknown, cb: (arg0?: unknown) => unknown) => { | ||
cb(FakeScope); | ||
return original.captureException(err, cb); | ||
}, | ||
}; | ||
}); | ||
|
||
const captureExceptionSpy = jest.spyOn(SentryBrowser, 'captureException'); | ||
|
||
jest.spyOn(console, 'error').mockImplementation(); | ||
|
||
describe('SentryErrorHandler', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('`createErrorHandler `creates a SentryErrorHandler with an empty config', () => { | ||
const errorHandler = createErrorHandler(); | ||
|
||
expect(errorHandler).toBeInstanceOf(SentryErrorHandler); | ||
}); | ||
|
||
it('handleError method assigns the correct mechanism', () => { | ||
const addEventProcessorSpy = jest.spyOn(FakeScope, 'addEventProcessor').mockImplementationOnce(callback => { | ||
callback({}, { event_id: 'fake-event-id' }); | ||
return FakeScope; | ||
}); | ||
|
||
const addExceptionMechanismSpy = jest.spyOn(SentryUtils, 'addExceptionMechanism'); | ||
|
||
const errorHandler = createErrorHandler(); | ||
errorHandler.handleError(new Error('test')); | ||
|
||
expect(addEventProcessorSpy).toBeCalledTimes(1); | ||
expect(addExceptionMechanismSpy).toBeCalledTimes(1); | ||
expect(addExceptionMechanismSpy).toBeCalledWith({}, { handled: false, type: 'angular' }); | ||
}); | ||
|
||
it('handleError method extracts `null` error', () => { | ||
createErrorHandler().handleError(null); | ||
|
||
expect(captureExceptionSpy).toHaveBeenCalledTimes(1); | ||
expect(captureExceptionSpy).toHaveBeenCalledWith('Handled unknown error', expect.any(Function)); | ||
}); | ||
|
||
it('handleError method extracts `undefined` error', () => { | ||
createErrorHandler().handleError(undefined); | ||
|
||
expect(captureExceptionSpy).toHaveBeenCalledTimes(1); | ||
expect(captureExceptionSpy).toHaveBeenCalledWith('Handled unknown error', expect.any(Function)); | ||
}); | ||
|
||
it('handleError method extracts a string', () => { | ||
const str = 'sentry-test'; | ||
createErrorHandler().handleError(str); | ||
|
||
expect(captureExceptionSpy).toHaveBeenCalledTimes(1); | ||
expect(captureExceptionSpy).toHaveBeenCalledWith(str, expect.any(Function)); | ||
}); | ||
|
||
it('handleError method extracts an empty Error', () => { | ||
const err = new Error(); | ||
createErrorHandler().handleError(err); | ||
|
||
expect(captureExceptionSpy).toHaveBeenCalledTimes(1); | ||
expect(captureExceptionSpy).toHaveBeenCalledWith(err, expect.any(Function)); | ||
}); | ||
|
||
it('handleError method extracts an Error with `ngOriginalError`', () => { | ||
const ngErr = new Error('sentry-ng-test'); | ||
const err = { | ||
ngOriginalError: ngErr, | ||
}; | ||
|
||
createErrorHandler().handleError(err); | ||
|
||
expect(captureExceptionSpy).toHaveBeenCalledTimes(1); | ||
expect(captureExceptionSpy).toHaveBeenCalledWith(ngErr, expect.any(Function)); | ||
}); | ||
|
||
it('handleError method extracts an `HttpErrorResponse` with `Error`', () => { | ||
const httpErr = new Error('sentry-http-test'); | ||
const err = new HttpErrorResponse({ error: httpErr }); | ||
|
||
createErrorHandler().handleError(err); | ||
|
||
expect(captureExceptionSpy).toHaveBeenCalledTimes(1); | ||
expect(captureExceptionSpy).toHaveBeenCalledWith(httpErr, expect.any(Function)); | ||
}); | ||
|
||
it('handleError method extracts an `HttpErrorResponse` with `ErrorEvent`', () => { | ||
const httpErr = new ErrorEvent('http', { message: 'sentry-http-test' }); | ||
const err = new HttpErrorResponse({ error: httpErr }); | ||
|
||
createErrorHandler().handleError(err); | ||
|
||
expect(captureExceptionSpy).toHaveBeenCalledTimes(1); | ||
expect(captureExceptionSpy).toHaveBeenCalledWith('sentry-http-test', expect.any(Function)); | ||
}); | ||
|
||
it('handleError method extracts an `HttpErrorResponse` with string', () => { | ||
const err = new HttpErrorResponse({ error: 'sentry-http-test' }); | ||
createErrorHandler().handleError(err); | ||
|
||
expect(captureExceptionSpy).toHaveBeenCalledTimes(1); | ||
expect(captureExceptionSpy).toHaveBeenCalledWith( | ||
'Server returned code 0 with body "sentry-http-test"', | ||
expect.any(Function), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that we're using the scope eventprocessor - do we still need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default fallback mechanism seems unneeded at this point, removing it.
Not sure about the rest, though.
The original intent on this PR was to mimic how mechanisms are passed in Node SDK on browser SDK, but a lot changed since, so I don't know if that's still relevant.
Still, it looks like this can also be moved into
scope.addEventProcessor
to be consistent, if we are keeping it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to change the Node approach to how we do it in Express (and Angular). (But let's do this in another PR). Which makes me think that we don't need to query the hint here. The original call to
addExceptionMechanism
shouldn't be a problem because it won't overwrite the already set mechanism with the default value (see here)So overall, I don't think we need this change here (unless I'm missing something)