-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
61 lines (48 loc) · 1.46 KB
/
index.ts
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
// @ts-ignore
import { Meteor } from 'meteor/meteor';
interface ErrorCatcherErrorHandleParams {
errorSource: 'meteor.debug' | 'node';
error: object;
}
interface ErrorCatcherParams {
handleError: (params: ErrorCatcherErrorHandleParams) => void;
}
/**
* Enables universal error catcher for Node and Meteor
* uncaught exceptions
*/
const enableErrorCatcher = async ({ handleError }: ErrorCatcherParams) => {
if (Meteor.isServer) {
console.log('Error Catcher Enabled');
/**
* Exit code 7
* 7 - Internal Exception Handler Run-Time Failure:
* There was an uncaught exception, and the internal fatal exception handler function itself
* threw an error while attempting to handle it.
*/
process.on('uncaughtException', async (error) => {
console.error(error);
await handleError({
errorSource: 'node',
error,
});
/** Make sure this is called last */
setTimeout(() => process.exit(7), 0);
});
/** Catch all meteor's errors by hijacking Meteor._debug */
const originalMeteorDebug = Meteor._debug;
Meteor._debug = async function (message, stack) {
console.error(message, stack);
const error = new Error(message, stack || []);
await handleError({
errorSource: 'meteor.debug',
error,
});
return originalMeteorDebug.apply(
this,
Array.prototype.slice.call(arguments)
);
};
}
};
export { enableErrorCatcher };