UNPKG

2.97 kBJavaScriptView Raw
1import { relative, basename } from '@sentry/utils';
2
3/** Rewrite event frames paths */
4class RewriteFrames {
5 /**
6 * @inheritDoc
7 */
8 static __initStatic() {this.id = 'RewriteFrames';}
9
10 /**
11 * @inheritDoc
12 */
13 __init() {this.name = RewriteFrames.id;}
14
15 /**
16 * @inheritDoc
17 */
18
19 /**
20 * @inheritDoc
21 */
22 __init2() {this._prefix = 'app:///';}
23
24 /**
25 * @inheritDoc
26 */
27 constructor(options = {}) {RewriteFrames.prototype.__init.call(this);RewriteFrames.prototype.__init2.call(this);RewriteFrames.prototype.__init3.call(this);
28 if (options.root) {
29 this._root = options.root;
30 }
31 if (options.prefix) {
32 this._prefix = options.prefix;
33 }
34 if (options.iteratee) {
35 this._iteratee = options.iteratee;
36 }
37 }
38
39 /**
40 * @inheritDoc
41 */
42 setupOnce(addGlobalEventProcessor, getCurrentHub) {
43 addGlobalEventProcessor(event => {
44 const self = getCurrentHub().getIntegration(RewriteFrames);
45 if (self) {
46 return self.process(event);
47 }
48 return event;
49 });
50 }
51
52 /** JSDoc */
53 process(originalEvent) {
54 let processedEvent = originalEvent;
55
56 if (originalEvent.exception && Array.isArray(originalEvent.exception.values)) {
57 processedEvent = this._processExceptionsEvent(processedEvent);
58 }
59
60 return processedEvent;
61 }
62
63 /**
64 * @inheritDoc
65 */
66 __init3() {this._iteratee = (frame) => {
67 if (!frame.filename) {
68 return frame;
69 }
70 // Check if the frame filename begins with `/` or a Windows-style prefix such as `C:\`
71 const isWindowsFrame = /^[A-Z]:\\/.test(frame.filename);
72 const startsWithSlash = /^\//.test(frame.filename);
73 if (isWindowsFrame || startsWithSlash) {
74 const filename = isWindowsFrame
75 ? frame.filename
76 .replace(/^[A-Z]:/, '') // remove Windows-style prefix
77 .replace(/\\/g, '/') // replace all `\\` instances with `/`
78 : frame.filename;
79 const base = this._root ? relative(this._root, filename) : basename(filename);
80 frame.filename = `${this._prefix}${base}`;
81 }
82 return frame;
83 };}
84
85 /** JSDoc */
86 _processExceptionsEvent(event) {
87 try {
88 return {
89 ...event,
90 exception: {
91 ...event.exception,
92 // The check for this is performed inside `process` call itself, safe to skip here
93 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
94 values: event.exception.values.map(value => ({
95 ...value,
96 ...(value.stacktrace && { stacktrace: this._processStacktrace(value.stacktrace) }),
97 })),
98 },
99 };
100 } catch (_oO) {
101 return event;
102 }
103 }
104
105 /** JSDoc */
106 _processStacktrace(stacktrace) {
107 return {
108 ...stacktrace,
109 frames: stacktrace && stacktrace.frames && stacktrace.frames.map(f => this._iteratee(f)),
110 };
111 }
112} RewriteFrames.__initStatic();
113
114export { RewriteFrames };
115//# sourceMappingURL=rewriteframes.js.map