1 | Object.defineProperty(exports, '__esModule', { value: true });
|
2 |
|
3 | const utils = require('@sentry/utils');
|
4 | const api = require('./api.js');
|
5 | const envelope = require('./envelope.js');
|
6 | const integration = require('./integration.js');
|
7 | const session = require('./session.js');
|
8 | const prepareEvent = require('./utils/prepareEvent.js');
|
9 |
|
10 | const ALREADY_SEEN_ERROR = "Not capturing exception because it's already been captured.";
|
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 | class BaseClient {
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | __init() {this._integrations = {};}
|
50 |
|
51 |
|
52 | __init2() {this._integrationsInitialized = false;}
|
53 |
|
54 |
|
55 | __init3() {this._numProcessing = 0;}
|
56 |
|
57 |
|
58 | __init4() {this._outcomes = {};}
|
59 |
|
60 |
|
61 | __init5() {this._hooks = {};}
|
62 |
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 | constructor(options) {BaseClient.prototype.__init.call(this);BaseClient.prototype.__init2.call(this);BaseClient.prototype.__init3.call(this);BaseClient.prototype.__init4.call(this);BaseClient.prototype.__init5.call(this);
|
69 | this._options = options;
|
70 | if (options.dsn) {
|
71 | this._dsn = utils.makeDsn(options.dsn);
|
72 | const url = api.getEnvelopeEndpointWithUrlEncodedAuth(this._dsn, options);
|
73 | this._transport = options.transport({
|
74 | recordDroppedEvent: this.recordDroppedEvent.bind(this),
|
75 | ...options.transportOptions,
|
76 | url,
|
77 | });
|
78 | } else {
|
79 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.warn('No DSN provided, client will not do anything.');
|
80 | }
|
81 | }
|
82 |
|
83 | |
84 |
|
85 |
|
86 |
|
87 | captureException(exception, hint, scope) {
|
88 |
|
89 | if (utils.checkOrSetAlreadyCaught(exception)) {
|
90 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.log(ALREADY_SEEN_ERROR);
|
91 | return;
|
92 | }
|
93 |
|
94 | let eventId = hint && hint.event_id;
|
95 |
|
96 | this._process(
|
97 | this.eventFromException(exception, hint)
|
98 | .then(event => this._captureEvent(event, hint, scope))
|
99 | .then(result => {
|
100 | eventId = result;
|
101 | }),
|
102 | );
|
103 |
|
104 | return eventId;
|
105 | }
|
106 |
|
107 | |
108 |
|
109 |
|
110 | captureMessage(
|
111 | message,
|
112 |
|
113 | level,
|
114 | hint,
|
115 | scope,
|
116 | ) {
|
117 | let eventId = hint && hint.event_id;
|
118 |
|
119 | const promisedEvent = utils.isPrimitive(message)
|
120 | ? this.eventFromMessage(String(message), level, hint)
|
121 | : this.eventFromException(message, hint);
|
122 |
|
123 | this._process(
|
124 | promisedEvent
|
125 | .then(event => this._captureEvent(event, hint, scope))
|
126 | .then(result => {
|
127 | eventId = result;
|
128 | }),
|
129 | );
|
130 |
|
131 | return eventId;
|
132 | }
|
133 |
|
134 | |
135 |
|
136 |
|
137 | captureEvent(event, hint, scope) {
|
138 |
|
139 | if (hint && hint.originalException && utils.checkOrSetAlreadyCaught(hint.originalException)) {
|
140 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.log(ALREADY_SEEN_ERROR);
|
141 | return;
|
142 | }
|
143 |
|
144 | let eventId = hint && hint.event_id;
|
145 |
|
146 | this._process(
|
147 | this._captureEvent(event, hint, scope).then(result => {
|
148 | eventId = result;
|
149 | }),
|
150 | );
|
151 |
|
152 | return eventId;
|
153 | }
|
154 |
|
155 | |
156 |
|
157 |
|
158 | captureSession(session$1) {
|
159 | if (!this._isEnabled()) {
|
160 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.warn('SDK not enabled, will not capture session.');
|
161 | return;
|
162 | }
|
163 |
|
164 | if (!(typeof session$1.release === 'string')) {
|
165 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.warn('Discarded session because of missing or non-string release');
|
166 | } else {
|
167 | this.sendSession(session$1);
|
168 |
|
169 | session.updateSession(session$1, { init: false });
|
170 | }
|
171 | }
|
172 |
|
173 | |
174 |
|
175 |
|
176 | getDsn() {
|
177 | return this._dsn;
|
178 | }
|
179 |
|
180 | |
181 |
|
182 |
|
183 | getOptions() {
|
184 | return this._options;
|
185 | }
|
186 |
|
187 | |
188 |
|
189 |
|
190 |
|
191 |
|
192 | getSdkMetadata() {
|
193 | return this._options._metadata;
|
194 | }
|
195 |
|
196 | |
197 |
|
198 |
|
199 | getTransport() {
|
200 | return this._transport;
|
201 | }
|
202 |
|
203 | |
204 |
|
205 |
|
206 | flush(timeout) {
|
207 | const transport = this._transport;
|
208 | if (transport) {
|
209 | return this._isClientDoneProcessing(timeout).then(clientFinished => {
|
210 | return transport.flush(timeout).then(transportFlushed => clientFinished && transportFlushed);
|
211 | });
|
212 | } else {
|
213 | return utils.resolvedSyncPromise(true);
|
214 | }
|
215 | }
|
216 |
|
217 | |
218 |
|
219 |
|
220 | close(timeout) {
|
221 | return this.flush(timeout).then(result => {
|
222 | this.getOptions().enabled = false;
|
223 | return result;
|
224 | });
|
225 | }
|
226 |
|
227 | |
228 |
|
229 |
|
230 | setupIntegrations() {
|
231 | if (this._isEnabled() && !this._integrationsInitialized) {
|
232 | this._integrations = integration.setupIntegrations(this._options.integrations);
|
233 | this._integrationsInitialized = true;
|
234 | }
|
235 | }
|
236 |
|
237 | |
238 |
|
239 |
|
240 |
|
241 |
|
242 | getIntegrationById(integrationId) {
|
243 | return this._integrations[integrationId];
|
244 | }
|
245 |
|
246 | |
247 |
|
248 |
|
249 | getIntegration(integration) {
|
250 | try {
|
251 | return (this._integrations[integration.id] ) || null;
|
252 | } catch (_oO) {
|
253 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.warn(`Cannot retrieve integration ${integration.id} from the current Client`);
|
254 | return null;
|
255 | }
|
256 | }
|
257 |
|
258 | |
259 |
|
260 |
|
261 | addIntegration(integration$1) {
|
262 | integration.setupIntegration(integration$1, this._integrations);
|
263 | }
|
264 |
|
265 | |
266 |
|
267 |
|
268 | sendEvent(event, hint = {}) {
|
269 | if (this._dsn) {
|
270 | let env = envelope.createEventEnvelope(event, this._dsn, this._options._metadata, this._options.tunnel);
|
271 |
|
272 | for (const attachment of hint.attachments || []) {
|
273 | env = utils.addItemToEnvelope(
|
274 | env,
|
275 | utils.createAttachmentEnvelopeItem(
|
276 | attachment,
|
277 | this._options.transportOptions && this._options.transportOptions.textEncoder,
|
278 | ),
|
279 | );
|
280 | }
|
281 |
|
282 | const promise = this._sendEnvelope(env);
|
283 | if (promise) {
|
284 | promise.then(sendResponse => this.emit('afterSendEvent', event, sendResponse), null);
|
285 | }
|
286 | }
|
287 | }
|
288 |
|
289 | |
290 |
|
291 |
|
292 | sendSession(session) {
|
293 | if (this._dsn) {
|
294 | const env = envelope.createSessionEnvelope(session, this._dsn, this._options._metadata, this._options.tunnel);
|
295 | void this._sendEnvelope(env);
|
296 | }
|
297 | }
|
298 |
|
299 | |
300 |
|
301 |
|
302 | recordDroppedEvent(reason, category, _event) {
|
303 |
|
304 |
|
305 | if (this._options.sendClientReports) {
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | const key = `${reason}:${category}`;
|
313 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.log(`Adding outcome: "${key}"`);
|
314 |
|
315 |
|
316 | this._outcomes[key] = this._outcomes[key] + 1 || 1;
|
317 | }
|
318 | }
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 | on(hook, callback) {
|
326 | if (!this._hooks[hook]) {
|
327 | this._hooks[hook] = [];
|
328 | }
|
329 |
|
330 |
|
331 | this._hooks[hook].push(callback);
|
332 | }
|
333 |
|
334 |
|
335 |
|
336 |
|
337 | emit(hook, ...rest) {
|
338 | if (this._hooks[hook]) {
|
339 |
|
340 | this._hooks[hook].forEach(callback => callback(...rest));
|
341 | }
|
342 | }
|
343 |
|
344 |
|
345 | _updateSessionFromEvent(session$1, event) {
|
346 | let crashed = false;
|
347 | let errored = false;
|
348 | const exceptions = event.exception && event.exception.values;
|
349 |
|
350 | if (exceptions) {
|
351 | errored = true;
|
352 |
|
353 | for (const ex of exceptions) {
|
354 | const mechanism = ex.mechanism;
|
355 | if (mechanism && mechanism.handled === false) {
|
356 | crashed = true;
|
357 | break;
|
358 | }
|
359 | }
|
360 | }
|
361 |
|
362 |
|
363 |
|
364 |
|
365 | const sessionNonTerminal = session$1.status === 'ok';
|
366 | const shouldUpdateAndSend = (sessionNonTerminal && session$1.errors === 0) || (sessionNonTerminal && crashed);
|
367 |
|
368 | if (shouldUpdateAndSend) {
|
369 | session.updateSession(session$1, {
|
370 | ...(crashed && { status: 'crashed' }),
|
371 | errors: session$1.errors || Number(errored || crashed),
|
372 | });
|
373 | this.captureSession(session$1);
|
374 | }
|
375 | }
|
376 |
|
377 | |
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 | _isClientDoneProcessing(timeout) {
|
388 | return new utils.SyncPromise(resolve => {
|
389 | let ticked = 0;
|
390 | const tick = 1;
|
391 |
|
392 | const interval = setInterval(() => {
|
393 | if (this._numProcessing == 0) {
|
394 | clearInterval(interval);
|
395 | resolve(true);
|
396 | } else {
|
397 | ticked += tick;
|
398 | if (timeout && ticked >= timeout) {
|
399 | clearInterval(interval);
|
400 | resolve(false);
|
401 | }
|
402 | }
|
403 | }, tick);
|
404 | });
|
405 | }
|
406 |
|
407 |
|
408 | _isEnabled() {
|
409 | return this.getOptions().enabled !== false && this._dsn !== undefined;
|
410 | }
|
411 |
|
412 | |
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 | _prepareEvent(event, hint, scope) {
|
427 | const options = this.getOptions();
|
428 | const integrations = Object.keys(this._integrations);
|
429 | if (!hint.integrations && integrations.length > 0) {
|
430 | hint.integrations = integrations;
|
431 | }
|
432 | return prepareEvent.prepareEvent(options, event, hint, scope);
|
433 | }
|
434 |
|
435 | |
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 | _captureEvent(event, hint = {}, scope) {
|
442 | return this._processEvent(event, hint, scope).then(
|
443 | finalEvent => {
|
444 | return finalEvent.event_id;
|
445 | },
|
446 | reason => {
|
447 | if ((typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__)) {
|
448 |
|
449 |
|
450 | const sentryError = reason ;
|
451 | if (sentryError.logLevel === 'log') {
|
452 | utils.logger.log(sentryError.message);
|
453 | } else {
|
454 | utils.logger.warn(sentryError);
|
455 | }
|
456 | }
|
457 | return undefined;
|
458 | },
|
459 | );
|
460 | }
|
461 |
|
462 | |
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 | _processEvent(event, hint, scope) {
|
476 | const options = this.getOptions();
|
477 | const { sampleRate } = options;
|
478 |
|
479 | if (!this._isEnabled()) {
|
480 | return utils.rejectedSyncPromise(new utils.SentryError('SDK not enabled, will not capture event.', 'log'));
|
481 | }
|
482 |
|
483 | const isTransaction = isTransactionEvent(event);
|
484 | const isError = isErrorEvent(event);
|
485 | const eventType = event.type || 'error';
|
486 | const beforeSendLabel = `before send for type \`${eventType}\``;
|
487 |
|
488 |
|
489 |
|
490 |
|
491 | if (isError && typeof sampleRate === 'number' && Math.random() > sampleRate) {
|
492 | this.recordDroppedEvent('sample_rate', 'error', event);
|
493 | return utils.rejectedSyncPromise(
|
494 | new utils.SentryError(
|
495 | `Discarding event because it's not included in the random sample (sampling rate = ${sampleRate})`,
|
496 | 'log',
|
497 | ),
|
498 | );
|
499 | }
|
500 |
|
501 | const dataCategory = eventType === 'replay_event' ? 'replay' : eventType;
|
502 |
|
503 | return this._prepareEvent(event, hint, scope)
|
504 | .then(prepared => {
|
505 | if (prepared === null) {
|
506 | this.recordDroppedEvent('event_processor', dataCategory, event);
|
507 | throw new utils.SentryError('An event processor returned `null`, will not send event.', 'log');
|
508 | }
|
509 |
|
510 | const isInternalException = hint.data && (hint.data ).__sentry__ === true;
|
511 | if (isInternalException) {
|
512 | return prepared;
|
513 | }
|
514 |
|
515 | const result = processBeforeSend(options, prepared, hint);
|
516 | return _validateBeforeSendResult(result, beforeSendLabel);
|
517 | })
|
518 | .then(processedEvent => {
|
519 | if (processedEvent === null) {
|
520 | this.recordDroppedEvent('before_send', dataCategory, event);
|
521 | throw new utils.SentryError(`${beforeSendLabel} returned \`null\`, will not send event.`, 'log');
|
522 | }
|
523 |
|
524 | const session = scope && scope.getSession();
|
525 | if (!isTransaction && session) {
|
526 | this._updateSessionFromEvent(session, processedEvent);
|
527 | }
|
528 |
|
529 |
|
530 |
|
531 |
|
532 | const transactionInfo = processedEvent.transaction_info;
|
533 | if (isTransaction && transactionInfo && processedEvent.transaction !== event.transaction) {
|
534 | const source = 'custom';
|
535 | processedEvent.transaction_info = {
|
536 | ...transactionInfo,
|
537 | source,
|
538 | };
|
539 | }
|
540 |
|
541 | this.sendEvent(processedEvent, hint);
|
542 | return processedEvent;
|
543 | })
|
544 | .then(null, reason => {
|
545 | if (reason instanceof utils.SentryError) {
|
546 | throw reason;
|
547 | }
|
548 |
|
549 | this.captureException(reason, {
|
550 | data: {
|
551 | __sentry__: true,
|
552 | },
|
553 | originalException: reason,
|
554 | });
|
555 | throw new utils.SentryError(
|
556 | `Event processing pipeline threw an error, original event will not be sent. Details have been sent as a new event.\nReason: ${reason}`,
|
557 | );
|
558 | });
|
559 | }
|
560 |
|
561 | |
562 |
|
563 |
|
564 | _process(promise) {
|
565 | this._numProcessing++;
|
566 | void promise.then(
|
567 | value => {
|
568 | this._numProcessing--;
|
569 | return value;
|
570 | },
|
571 | reason => {
|
572 | this._numProcessing--;
|
573 | return reason;
|
574 | },
|
575 | );
|
576 | }
|
577 |
|
578 | |
579 |
|
580 |
|
581 | _sendEnvelope(envelope) {
|
582 | if (this._transport && this._dsn) {
|
583 | this.emit('beforeEnvelope', envelope);
|
584 |
|
585 | return this._transport.send(envelope).then(null, reason => {
|
586 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.error('Error while sending event:', reason);
|
587 | });
|
588 | } else {
|
589 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.error('Transport disabled');
|
590 | }
|
591 | }
|
592 |
|
593 | |
594 |
|
595 |
|
596 | _clearOutcomes() {
|
597 | const outcomes = this._outcomes;
|
598 | this._outcomes = {};
|
599 | return Object.keys(outcomes).map(key => {
|
600 | const [reason, category] = key.split(':') ;
|
601 | return {
|
602 | reason,
|
603 | category,
|
604 | quantity: outcomes[key],
|
605 | };
|
606 | });
|
607 | }
|
608 |
|
609 | |
610 |
|
611 |
|
612 |
|
613 |
|
614 | }
|
615 |
|
616 |
|
617 |
|
618 |
|
619 | function _validateBeforeSendResult(
|
620 | beforeSendResult,
|
621 | beforeSendLabel,
|
622 | ) {
|
623 | const invalidValueError = `${beforeSendLabel} must return \`null\` or a valid event.`;
|
624 | if (utils.isThenable(beforeSendResult)) {
|
625 | return beforeSendResult.then(
|
626 | event => {
|
627 | if (!utils.isPlainObject(event) && event !== null) {
|
628 | throw new utils.SentryError(invalidValueError);
|
629 | }
|
630 | return event;
|
631 | },
|
632 | e => {
|
633 | throw new utils.SentryError(`${beforeSendLabel} rejected with ${e}`);
|
634 | },
|
635 | );
|
636 | } else if (!utils.isPlainObject(beforeSendResult) && beforeSendResult !== null) {
|
637 | throw new utils.SentryError(invalidValueError);
|
638 | }
|
639 | return beforeSendResult;
|
640 | }
|
641 |
|
642 |
|
643 |
|
644 |
|
645 | function processBeforeSend(
|
646 | options,
|
647 | event,
|
648 | hint,
|
649 | ) {
|
650 | const { beforeSend, beforeSendTransaction } = options;
|
651 |
|
652 | if (isErrorEvent(event) && beforeSend) {
|
653 | return beforeSend(event, hint);
|
654 | }
|
655 |
|
656 | if (isTransactionEvent(event) && beforeSendTransaction) {
|
657 | return beforeSendTransaction(event, hint);
|
658 | }
|
659 |
|
660 | return event;
|
661 | }
|
662 |
|
663 | function isErrorEvent(event) {
|
664 | return event.type === undefined;
|
665 | }
|
666 |
|
667 | function isTransactionEvent(event) {
|
668 | return event.type === 'transaction';
|
669 | }
|
670 |
|
671 | exports.BaseClient = BaseClient;
|
672 |
|