-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy patherrors.ts
43 lines (37 loc) · 1.52 KB
/
errors.ts
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
37
38
39
40
41
42
43
import { DEBUG_BUILD } from '../debug-build';
import { addGlobalErrorInstrumentationHandler } from '../utils-hoist/instrument/globalError';
import { addGlobalUnhandledRejectionInstrumentationHandler } from '../utils-hoist/instrument/globalUnhandledRejection';
import { logger } from '../utils-hoist/logger';
import { getActiveSpan, getRootSpan } from '../utils/spanUtils';
import { SPAN_STATUS_ERROR } from './spanstatus';
let errorsInstrumented = false;
/** Only exposed for testing */
export function _resetErrorsInstrumented(): void {
errorsInstrumented = false;
}
/**
* Ensure that global errors automatically set the active span status.
*/
export function registerSpanErrorInstrumentation(): void {
if (errorsInstrumented) {
return;
}
errorsInstrumented = true;
addGlobalErrorInstrumentationHandler(errorCallback);
addGlobalUnhandledRejectionInstrumentationHandler(errorCallback);
}
/**
* If an error or unhandled promise occurs, we mark the active root span as failed
*/
function errorCallback(): void {
const activeSpan = getActiveSpan();
const rootSpan = activeSpan && getRootSpan(activeSpan);
if (rootSpan) {
const message = 'internal_error';
DEBUG_BUILD && logger.log(`[Tracing] Root span: ${message} -> Global error occurred`);
rootSpan.setStatus({ code: SPAN_STATUS_ERROR, message });
}
}
// The function name will be lost when bundling but we need to be able to identify this listener later to maintain the
// node.js default exit behaviour
errorCallback.tag = 'sentry_tracingErrorCallback';