-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnhandledErrorStoreModule.ts
More file actions
169 lines (157 loc) · 4.76 KB
/
UnhandledErrorStoreModule.ts
File metadata and controls
169 lines (157 loc) · 4.76 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { Action } from '../Actions';
import { ActionsObservable, StateObservable } from 'redux-observable';
import { Observable, EMPTY, fromEvent, merge } from 'rxjs';
import { filter, tap, mergeMapTo, map, mapTo } from 'rxjs/operators';
import {
notifyUnhandledError,
clearUnhandledErrors,
UnhandledError,
} from '../Actions/UnhandledErrorActions';
import { RootState } from '../Core/State/Types';
import { EpicDependencies } from '../Core/Store';
import { updateLocation } from '../Actions/RouterActions';
export const key = 'unhandledErrors';
export interface State {
errors: UnhandledError[];
}
const initialState: State = {
errors: [],
};
export function reduce(state: State = initialState, action: Action): State {
switch (action.type) {
case notifyUnhandledError.type:
return {
...state,
errors: [...state.errors, action.payload.unhandledError],
};
case clearUnhandledErrors.type:
return {
errors: [],
};
default:
return state;
}
}
// TODO Allow this to be configured by .. consumer
function ignoreError(message: string): boolean {
const messagesToIgnore = [
'ResizeObserver loop limit exceeded',
'ResizeObserver loop completed with undelivered notifications',
'empty textures are not allowed',
];
return messagesToIgnore.some((messageToIgnore) =>
message.includes(messageToIgnore)
);
}
/**
* Checks if an error originates from a browser extension or third-party script.
* Returns true if the error should be filtered out (not reported).
*/
function isThirdPartyError(error: Error | string, filename?: string): boolean {
// Extension URL patterns
const extensionProtocols = [
'chrome-extension://',
'moz-extension://',
'safari-extension://',
'safari-web-extension://',
];
// Check if filename contains extension protocol
if (filename) {
if (extensionProtocols.some((protocol) => filename.includes(protocol))) {
return true;
}
// Filter errors from non-same-origin sources
// Errors from our app should have relative paths or our domain
if (
filename.startsWith('http') &&
!filename.includes(window.location.hostname)
) {
return true;
}
}
// Check error stack trace for extension URLs
if (error instanceof Error && error.stack) {
if (
extensionProtocols.some((protocol) => error.stack!.includes(protocol))
) {
return true;
}
}
// Check error message for extension-related patterns
const errorMessage = error instanceof Error ? error.message : String(error);
if (extensionProtocols.some((protocol) => errorMessage.includes(protocol))) {
return true;
}
// Common patterns from browser extensions
const extensionPatterns = [
'__firefox__',
'__chrome__',
'__safari__',
'webkit-masked-url',
];
if (extensionPatterns.some((pattern) => errorMessage.includes(pattern))) {
return true;
}
return false;
}
export function observe(
action$: ActionsObservable<Action>,
state$: StateObservable<RootState>,
{ wdkService }: EpicDependencies
): Observable<Action> {
// map unhandled promise rejections to unhandledError action
const rejection$: Observable<Action> = fromEvent<PromiseRejectionEvent>(
window,
'unhandledrejection'
).pipe(
filter((event: PromiseRejectionEvent) => {
return (
!ignoreError(String(event.reason)) &&
!isThirdPartyError(event.reason) &&
!wdkService.getIsInvalidating()
);
}),
map((event: PromiseRejectionEvent) => notifyUnhandledError(event.reason))
);
// map unhandled errors to unhandledError action
const error$: Observable<Action> = fromEvent<ErrorEvent>(
window,
'error'
).pipe(
filter((event: ErrorEvent) => {
return (
!ignoreError(event.message) &&
!isThirdPartyError(event.error ?? event.message, event.filename) &&
!wdkService.getIsInvalidating()
);
}),
map((event: ErrorEvent) => {
return notifyUnhandledError(event.error ?? event.message);
})
);
// clear errors when route changes
const clear$: Observable<Action> = action$.pipe(
filter(updateLocation.isOfType),
mapTo(clearUnhandledErrors())
);
// log errors as they come in
const notify$: Observable<never> = action$.pipe(
filter(notifyUnhandledError.isOfType),
tap(async (action) => {
try {
const {
unhandledError: { error, id, info },
} = action.payload;
console.error(error);
await wdkService.submitErrorIfNot500(
error instanceof Error ? error : new Error(String(error)),
{ id, info }
);
} catch (error) {
console.error('Error logging request failed:', error);
}
}),
mergeMapTo(EMPTY)
);
return merge(rejection$, error$, clear$, notify$);
}