Snapshot-sentry
snapshot-sentry
is an npm package that contains the Sentry setup files and configurations for Snapshot backend projects.
It simplifies the process of setting up Sentry for error tracking in any Snapshot service.
Install
yarn add @snapshot-labs/snapshot-sentry
Configure
Set the following env variables:
SENTRY_DSN
(required)SENTRY_TRACE_SAMPLE_RATE
(Optional, default to 0.01
)
Usage
Instantiate
For express
Import the library in your root file
import { initLogger, fallbackLogger } from '@snapshot-labs/snapshot-sentry'
Init sentry just after creating your express instance, before any middleware
const app = express();
initLogger(app);
Set the fallthrough error handler after all controllers, but before any other error middleware
fallbackLogger(app);
Note: If you have a catch-all route, set fallbackLogger()
before it.
All uncaught errors, with code >= 500, will be handled automatically by Sentry. See Capture exception for how to handle caught exceptions.
For vanilla js
Import the library in your root file
import { initLogger } from '@snapshot-labs/snapshot-sentry'
Init sentry, as soon as possible to catch all errors.
initLogger()
See Capture exception for how to handle exceptions.
Capture exception
To capture an exception, use the capture
function:
import { capture } from '@snapshot-labs/snapshot-sentry'
try {
throw new Error('Ooops, someting went wrong');
} catch (e: any) {
capture(e)
}
You can also pass additional context data to the capture function:
import { capture } from '@snapshot-labs/snapshot-sentry'
let url = '';
try {
url = getUrlFromSomewhere('argOne')
throw new Error('Ooops, someting went wrong');
} catch (e: any) {
capture(e, { contexts: { input: { url: url } } })
}
When sentry is disabled, the capture
function will fallback to a simple console.log
, so no code change will be required when toggling this library on/off.
More info
License
Snapshot-sentry is open-sourced software licensed under the © MIT license.