forked from chrisleekr/binance-trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error-handler.js
77 lines (71 loc) · 2.23 KB
/
error-handler.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
65
66
67
68
69
70
71
72
73
74
75
76
77
const config = require('config');
const { slack } = require('./helpers');
const { getAPILimit } = require('./cronjob/trailingTradeHelper/common');
const runErrorHandler = logger => {
// This will catch the unhandled rejected Promise
process.on('unhandledRejection', err => {
// we will throw it to handle it in the exception
throw err;
});
// This will catch any unexpected errors that we don't handled them
// and report them to slack
// https://nodejs.org/api/process.html#process_event_uncaughtexception
process.on('uncaughtException', async err => {
logger.error({ err });
const githubIssuesLink =
'https://github.com/chrisleekr/binance-trading-bot/issues/new' +
'?assignees=&labels=bug&template=bug_report.md&title=';
slack.sendMessage(
`Uncaught Exception:\n` +
`If you see this, kindly report it to: ${githubIssuesLink}\n` +
`Code: ${err.code}\n` +
`Message:\`\`\`${err.message}\`\`\`\n` +
`${
config.get('featureToggle.notifyDebug')
? `Stack:\`\`\`${err.stack}\`\`\`\n`
: ''
}`,
{ apiLimit: getAPILimit(logger) }
);
});
};
const handleError = (logger, job, err) => {
// For the redlock fail
if (err.message.includes('redlock')) {
// Simply ignore
return;
}
logger.error(
{ err, errorCode: err.code, debug: true, saveLog: true },
`⚠ Execution failed.`
);
if (
err.code === -1001 ||
err.code === -1021 || // Timestamp for this request is outside the recvWindow
err.code === 'ECONNRESET' ||
err.code === 'ECONNREFUSED'
) {
// Let's silent for internal server error or assumed temporary errors
} else {
slack.sendMessage(
`Execution failed:\n` +
`Job: ${job}\n` +
`Code: ${err.code}\n` +
`Message:\`\`\`${err.message}\`\`\`\n` +
`${
config.get('featureToggle.notifyDebug')
? `Stack:\`\`\`${err.stack}\`\`\`\n`
: ''
}`,
{ apiLimit: getAPILimit(logger) }
);
}
};
const errorHandlerWrapper = async (logger, job, callback) => {
try {
await callback();
} catch (err) {
handleError(logger, job, err);
}
};
module.exports = { runErrorHandler, errorHandlerWrapper, handleError };