-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate from raven-js to @sentry/browser
Migrate frontend crash reporting to the modern Sentry JS client library.
- Loading branch information
1 parent
0655caa
commit 7ec925f
Showing
12 changed files
with
238 additions
and
199 deletions.
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 was deleted.
Oops, something went wrong.
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,52 @@ | ||
/** | ||
* This module configures Sentry crash reporting. | ||
* | ||
* Logging requires the Sentry DSN and Hypothesis version to be provided via the | ||
* app's settings object. | ||
*/ | ||
|
||
import * as Sentry from '@sentry/browser'; | ||
|
||
export type SentryConfig = { | ||
dsn: string; | ||
release: string; | ||
userid?: string; | ||
}; | ||
|
||
export function init(config: SentryConfig) { | ||
Sentry.init({ | ||
dsn: config.dsn, | ||
release: config.release, | ||
}); | ||
|
||
if (config.userid) { | ||
Sentry.setUser({ id: config.userid }); | ||
} | ||
} | ||
|
||
/** | ||
* Report an error to Sentry. | ||
* | ||
* @param error - An error object describing what went wrong | ||
* @param when - A string describing the context in which | ||
* the error occurred. | ||
* @param context - A JSON-serializable object containing additional | ||
* information which may be useful when investigating the error. | ||
*/ | ||
export function report(error: unknown, when: string, context?: unknown) { | ||
if (!(error instanceof Error)) { | ||
// If the passed object is not an Error, Sentry will serialize it using | ||
// toString() which produces unhelpful results for objects that do not | ||
// provide their own toString() implementations. | ||
// | ||
// If the error is a plain object or non-Error subclass with a message | ||
// property, such as errors returned by chrome.extension.lastError, | ||
// use that instead. | ||
if (typeof error === 'object' && error && 'message' in error) { | ||
error = error.message; | ||
} | ||
} | ||
|
||
const extra = Object.assign({ when: when }, context); | ||
Sentry.captureException(error, { extra }); | ||
} |
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
This file was deleted.
Oops, something went wrong.
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,83 @@ | ||
import * as sentry from '../../base/sentry'; | ||
|
||
describe('sentry', () => { | ||
let fakeSentry; | ||
|
||
beforeEach(() => { | ||
fakeSentry = { | ||
init: sinon.stub(), | ||
captureException: sinon.stub(), | ||
setUser: sinon.stub(), | ||
}; | ||
|
||
sentry.$imports.$mock({ | ||
'@sentry/browser': fakeSentry, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
sentry.$imports.$restore(); | ||
}); | ||
|
||
describe('init', () => { | ||
it('configures the Sentry client', () => { | ||
sentry.init({ | ||
dsn: 'dsn', | ||
release: 'release', | ||
userid: 'acct:foobar@hypothes.is', | ||
}); | ||
assert.calledWith( | ||
fakeSentry.init, | ||
sinon.match({ | ||
dsn: 'dsn', | ||
release: 'release', | ||
}), | ||
); | ||
}); | ||
|
||
it('sets the user context when a userid is specified', () => { | ||
sentry.init({ | ||
dsn: 'dsn', | ||
release: 'release', | ||
userid: 'acct:foobar@hypothes.is', | ||
}); | ||
assert.calledWith( | ||
fakeSentry.setUser, | ||
sinon.match({ | ||
id: 'acct:foobar@hypothes.is', | ||
}), | ||
); | ||
}); | ||
|
||
it('does not set the user context when a userid is not specified', () => { | ||
sentry.init({ | ||
dsn: 'dsn', | ||
release: 'release', | ||
userid: null, | ||
}); | ||
assert.notCalled(fakeSentry.setUser); | ||
}); | ||
}); | ||
|
||
describe('report', () => { | ||
it('extracts the message property from Error-like objects', () => { | ||
sentry.report({ message: 'An error' }, 'context'); | ||
assert.calledWith(fakeSentry.captureException, 'An error', { | ||
extra: { | ||
when: 'context', | ||
}, | ||
}); | ||
}); | ||
|
||
it('passes extra details through', () => { | ||
const error = new Error('an error'); | ||
sentry.report(error, 'some operation', { url: 'foobar.com' }); | ||
assert.calledWith(fakeSentry.captureException, error, { | ||
extra: { | ||
when: 'some operation', | ||
url: 'foobar.com', | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.