UNPKG

5.62 kBJavaScriptView Raw
1import { logger, getEventDescription, isMatchingPattern } from '@sentry/utils';
2
3// "Script error." is hard coded into browsers for errors that it can't read.
4// this is the result of a script being pulled in from an external domain and CORS.
5var DEFAULT_IGNORE_ERRORS = [/^Script error\.?$/, /^Javascript error: Script error\.? on line 0$/];
6
7/** Options for the InboundFilters integration */
8
9/** Inbound filters configurable by the user */
10class InboundFilters {
11 /**
12 * @inheritDoc
13 */
14 static __initStatic() {this.id = 'InboundFilters';}
15
16 /**
17 * @inheritDoc
18 */
19 __init() {this.name = InboundFilters.id;}
20
21 constructor( _options = {}) {;this._options = _options;InboundFilters.prototype.__init.call(this);}
22
23 /**
24 * @inheritDoc
25 */
26 setupOnce(addGlobalEventProcessor, getCurrentHub) {
27 var eventProcess = (event) => {
28 var hub = getCurrentHub();
29 if (hub) {
30 var self = hub.getIntegration(InboundFilters);
31 if (self) {
32 var client = hub.getClient();
33 var clientOptions = client ? client.getOptions() : {};
34 var options = _mergeOptions(self._options, clientOptions);
35 return _shouldDropEvent(event, options) ? null : event;
36 }
37 }
38 return event;
39 };
40
41 eventProcess.id = this.name;
42 addGlobalEventProcessor(eventProcess);
43 }
44} InboundFilters.__initStatic();
45
46/** JSDoc */
47function _mergeOptions(
48 internalOptions = {},
49 clientOptions = {},
50) {
51 return {
52 allowUrls: [...(internalOptions.allowUrls || []), ...(clientOptions.allowUrls || [])],
53 denyUrls: [...(internalOptions.denyUrls || []), ...(clientOptions.denyUrls || [])],
54 ignoreErrors: [
55 ...(internalOptions.ignoreErrors || []),
56 ...(clientOptions.ignoreErrors || []),
57 ...DEFAULT_IGNORE_ERRORS,
58 ],
59 ignoreInternal: internalOptions.ignoreInternal !== undefined ? internalOptions.ignoreInternal : true,
60 };
61}
62
63/** JSDoc */
64function _shouldDropEvent(event, options) {
65 if (options.ignoreInternal && _isSentryError(event)) {
66 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) &&
67 logger.warn(`Event dropped due to being internal Sentry Error.\nEvent: ${getEventDescription(event)}`);
68 return true;
69 }
70 if (_isIgnoredError(event, options.ignoreErrors)) {
71 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) &&
72 logger.warn(
73 `Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`,
74 );
75 return true;
76 }
77 if (_isDeniedUrl(event, options.denyUrls)) {
78 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) &&
79 logger.warn(
80 `Event dropped due to being matched by \`denyUrls\` option.\nEvent: ${getEventDescription(
81 event,
82 )}.\nUrl: ${_getEventFilterUrl(event)}`,
83 );
84 return true;
85 }
86 if (!_isAllowedUrl(event, options.allowUrls)) {
87 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) &&
88 logger.warn(
89 `Event dropped due to not being matched by \`allowUrls\` option.\nEvent: ${getEventDescription(
90 event,
91 )}.\nUrl: ${_getEventFilterUrl(event)}`,
92 );
93 return true;
94 }
95 return false;
96}
97
98function _isIgnoredError(event, ignoreErrors) {
99 if (!ignoreErrors || !ignoreErrors.length) {
100 return false;
101 }
102
103 return _getPossibleEventMessages(event).some(message =>
104 ignoreErrors.some(pattern => isMatchingPattern(message, pattern)),
105 );
106}
107
108function _isDeniedUrl(event, denyUrls) {
109 // TODO: Use Glob instead?
110 if (!denyUrls || !denyUrls.length) {
111 return false;
112 }
113 var url = _getEventFilterUrl(event);
114 return !url ? false : denyUrls.some(pattern => isMatchingPattern(url, pattern));
115}
116
117function _isAllowedUrl(event, allowUrls) {
118 // TODO: Use Glob instead?
119 if (!allowUrls || !allowUrls.length) {
120 return true;
121 }
122 var url = _getEventFilterUrl(event);
123 return !url ? true : allowUrls.some(pattern => isMatchingPattern(url, pattern));
124}
125
126function _getPossibleEventMessages(event) {
127 if (event.message) {
128 return [event.message];
129 }
130 if (event.exception) {
131 try {
132 const { type = '', value = '' } = (event.exception.values && event.exception.values[0]) || {};
133 return [`${value}`, `${type}: ${value}`];
134 } catch (oO) {
135 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error(`Cannot extract message for event ${getEventDescription(event)}`);
136 return [];
137 }
138 }
139 return [];
140}
141
142function _isSentryError(event) {
143 try {
144 // @ts-ignore can't be a sentry error if undefined
145 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
146 return event.exception.values[0].type === 'SentryError';
147 } catch (e) {
148 // ignore
149 }
150 return false;
151}
152
153function _getLastValidUrl(frames = []) {
154 for (let i = frames.length - 1; i >= 0; i--) {
155 var frame = frames[i];
156
157 if (frame && frame.filename !== '<anonymous>' && frame.filename !== '[native code]') {
158 return frame.filename || null;
159 }
160 }
161
162 return null;
163}
164
165function _getEventFilterUrl(event) {
166 try {
167 let frames;
168 try {
169 // @ts-ignore we only care about frames if the whole thing here is defined
170 frames = event.exception.values[0].stacktrace.frames;
171 } catch (e) {
172 // ignore
173 }
174 return frames ? _getLastValidUrl(frames) : null;
175 } catch (oO) {
176 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error(`Cannot extract url for event ${getEventDescription(event)}`);
177 return null;
178 }
179}
180
181export { InboundFilters, _mergeOptions, _shouldDropEvent };
182//# sourceMappingURL=inboundfilters.js.map