pleroma/src/sentry.ts

73 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-10-21 10:43:32 -07:00
import { NODE_ENV } from 'soapbox/build-config';
2023-10-12 19:51:06 -07:00
import sourceCode from 'soapbox/utils/code';
2023-10-12 18:07:48 -07:00
import type { Account } from './schemas';
import type { CaptureContext } from '@sentry/types';
2023-10-12 18:07:48 -07:00
2023-10-05 14:33:59 -07:00
/** Start Sentry. */
async function startSentry(dsn: string): Promise<void> {
const [Sentry, { Integrations: Integrations }] = await Promise.all([
import('@sentry/react'),
import('@sentry/tracing'),
]);
Sentry.init({
dsn,
debug: false,
2023-10-21 10:43:32 -07:00
enabled: NODE_ENV === 'production',
2023-10-05 14:33:59 -07:00
integrations: [new Integrations.BrowserTracing()],
// Filter events.
// https://docs.sentry.io/platforms/javascript/configuration/filtering/
ignoreErrors: [
// Network errors.
'AxiosError',
// sw.js couldn't be downloaded.
'Failed to update a ServiceWorker for scope',
// Useful for try/catch, useless as a Sentry error.
'AbortError',
// localForage error in FireFox private browsing mode (which doesn't support IndexedDB).
// We only use IndexedDB as a cache, so we can safely ignore the error.
'No available storage method found',
// Virtuoso throws these errors, but it is a false-positive.
// https://github.com/petyosi/react-virtuoso/issues/254
'ResizeObserver loop completed with undelivered notifications.',
'ResizeObserver loop limit exceeded',
2023-10-05 14:33:59 -07:00
],
denyUrls: [
// Browser extensions.
/extensions\//i,
/^chrome:\/\//i,
/^moz-extension:\/\//i,
],
tracesSampleRate: 1.0,
});
2023-10-12 19:51:06 -07:00
Sentry.setContext('soapbox', sourceCode);
2023-10-05 14:33:59 -07:00
}
2023-10-12 18:07:48 -07:00
/** Associate the account with Sentry events. */
async function setSentryAccount(account: Account) {
const Sentry = await import('@sentry/react');
Sentry.setUser({
id: account.id,
username: account.acct,
2023-10-12 19:51:06 -07:00
url: account.url,
2023-10-12 18:07:48 -07:00
});
}
/** Remove the account from Sentry events. */
async function unsetSentryAccount() {
const Sentry = await import('@sentry/react');
Sentry.setUser(null);
}
/** Capture the exception and report it to Sentry. */
async function captureSentryException (exception: any, captureContext?: CaptureContext | undefined): Promise<void> {
const Sentry = await import('@sentry/react');
Sentry.captureException(exception, captureContext);
}
export { startSentry, setSentryAccount, unsetSentryAccount, captureSentryException };