-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcreateSentryHandleError.ts
More file actions
36 lines (33 loc) · 1.08 KB
/
createSentryHandleError.ts
File metadata and controls
36 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { captureException, flushIfServerless } from '@sentry/core';
import type { HandleErrorFunction } from 'react-router';
export type SentryHandleErrorOptions = {
logErrors?: boolean;
};
/**
* A complete Sentry-instrumented handleError implementation that handles error reporting
*
* @returns A Sentry-instrumented handleError function
*/
export function createSentryHandleError({ logErrors = false }: SentryHandleErrorOptions): HandleErrorFunction {
const handleError: HandleErrorFunction = async function handleError(error, args): Promise<void> {
// React Router may abort some interrupted requests, don't report those
if (!args.request.signal.aborted) {
captureException(error, {
mechanism: {
type: 'react-router',
handled: false,
},
});
if (logErrors) {
// eslint-disable-next-line no-console
console.error(error);
}
try {
await flushIfServerless();
} catch {
// Ignore flush errors to ensure error handling completes gracefully
}
}
};
return handleError;
}