forked from alexayan/sentry-mina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.js
101 lines (86 loc) · 2.88 KB
/
backend.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { BaseBackend, SentryError } from '@sentry/core';
import { Status } from '@sentry/types';
import { isError, isErrorEvent, isPlainObject } from '@sentry/utils/is';
import { logger } from '@sentry/utils/logger';
import { eventFromPlainObject, eventFromStacktrace, prepareFramesForEvent } from './parsers';
import { computeStackTrace } from './tracekit';
import { RequestTransport } from './transports';
import { supportRequest} from './env';
export class MinaBackend extends BaseBackend {
install() {
const dsn = this.options.dsn;
if (!dsn) {
throw new SentryError('Invariant exception: install() must not be called when disabled');
}
Error.stackTraceLimit = 50;
return true;
}
async eventFromException(exception, hint) {
let event;
if (isErrorEvent(exception) && exception.error) {
const ex = exception;
exception = ex.error;
event = eventFromStacktrace(computeStackTrace(exception));
} else if (isError(exception)) {
event = eventFromStacktrace(computeStackTrace(exception ));
} else if (isPlainObject(exception) && hint && hint.syntheticException) {
const ex = exception;
event = eventFromPlainObject(ex, hint.syntheticException);
} else {
const ex = exception;
event = await this.eventFromMessage(ex, undefined, hint);
}
event = {
...event,
event_id: hint && hint.event_id,
exception: {
...event.exception,
mechanism: {
handled: true,
type: 'generic',
},
},
};
return event;
}
async eventFromMessage(
message,
level,
hint,
) {
const event = {
event_id: hint && hint.event_id,
level,
message,
};
if (this.options.attachStacktrace && hint && hint.syntheticException) {
const stacktrace = computeStackTrace(hint.syntheticException);
const frames = prepareFramesForEvent(stacktrace.stack);
event.stacktrace = {
frames,
};
}
return event;
}
async sendEvent(event) {
if (!this.options.dsn) {
logger.warn('Event has been skipped because no Dsn is configured.');
return { status: Status.Skipped, reason: 'Event has been skipped because no Dsn is configured.'};
}
if (!this.transport) {
const transportOptions = this.options.transportOptions
? this.options.transportOptions
: { dsn: this.options.dsn };
if (this.options.transport) {
this.transport = new this.options.transport({ dsn: this.options.dsn });
} else if (supportRequest()) {
this.transport = new RequestTransport(transportOptions);
}
}
if (!this.transport) {
logger.warn('Event has been skipped because no transport is configured.');
return { status: Status.Skipped, reason: 'Event has been skipped because no transport is configured.'};
}
return this.transport.captureEvent(event);
}
}