-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patherrors.js
64 lines (57 loc) · 1.75 KB
/
errors.js
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
import { createBrowserHistory } from 'history';
const history = createBrowserHistory();
// ERRORS
const notAFunctionError = () => {
const someArray = [{ func: function () {} }];
someArray[1].func();
};
const referenceError = () => {
throw new ReferenceError('undefinedVariable is not defined');
};
//eslint-disable-next-line
const syntaxError = () => eval('foo bar');
const rangeError = () => {
throw new RangeError('Parameter must be between 1 and 100');
};
const unhandledError = () => {
throw new UnhandledException('unhandled error');
};
const randomErrors = [
notAFunctionError,
referenceError,
syntaxError,
rangeError,
unhandledError,
];
const throwErrorNumber = (i) => {
randomErrors[i % randomErrors.length]();
};
// if n is 0.2 then this will return false 20% of the time
var probability = function (n) {
return !!n && Math.random() <= n;
};
const crasher = () => {
const queryParams = new URLSearchParams(history.location.search);
if (queryParams !== '') {
const crash = queryParams.get('crash');
if (crash) {
console.log('> crash', crash);
const errnum =
queryParams.get('errnum') ||
parseInt(Math.random() * randomErrors.length);
if (crash === 'true' || probability(parseFloat(crash))) {
throwErrorNumber(errnum);
}
}
} else {
console.log('> queryParam was', queryParams);
}
};
// Useful for fingerprinting examples and R&D. You can check `if (exception instanceof UnhandledException)`
// Based on the official example https://docs.sentry.io/platforms/javascript/usage/sdk-fingerprinting/#group-errors-with-greater-granularity
class UnhandledException extends Error {
constructor(message, functionName) {
super(message);
}
}
export { crasher, UnhandledException };