-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patherror.ts
More file actions
55 lines (49 loc) · 1.82 KB
/
error.ts
File metadata and controls
55 lines (49 loc) · 1.82 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
import Sanitizer from '../modules/sanitizer';
/**
* Extracts and normalizes error from ErrorEvent or PromiseRejectionEvent
*
* @param event - The error or promise rejection event
*/
export function getErrorFromErrorEvent(event: ErrorEvent | PromiseRejectionEvent): Error | string {
/**
* Promise rejection reason is recommended to be an Error, but it can be a string:
* - Promise.reject(new Error('Reason message')) ——— recommended
* - Promise.reject('Reason message')
*/
let error = (event as ErrorEvent).error || (event as PromiseRejectionEvent).reason;
/**
* Case when error triggered in external script
* We can't access event error object because of CORS
* Event message will be 'Script error.'
*/
if (event instanceof ErrorEvent && error === undefined) {
error = (event as ErrorEvent).message;
}
/**
* Case when error rejected with an object
* Using a string instead of wrapping in Error is more natural,
* it doesn't fake the backtrace, also prefix added for dashboard readability
*/
if (error instanceof Object && !(error instanceof Error) && event instanceof PromiseRejectionEvent) {
// Extra sanitize is needed to handle objects with circular references before JSON.stringify
error = `Promise rejected with ${JSON.stringify(Sanitizer.sanitize(error))}`;
}
return Sanitizer.sanitize(error);
}
/**
* Converts a promise rejection reason to a string message.
*
* String(obj) gives "[object Object]" and JSON.stringify("str")
* adds unwanted quotes.
*
* @param reason - The rejection reason from PromiseRejectionEvent
*/
export function stringifyRejectionReason(reason: unknown): string {
if (reason instanceof Error) {
return reason.message;
}
if (typeof reason === 'string') {
return reason;
}
return JSON.stringify(Sanitizer.sanitize(reason));
}