-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherrorhandler.js
More file actions
127 lines (106 loc) · 4.04 KB
/
errorhandler.js
File metadata and controls
127 lines (106 loc) · 4.04 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
// Tradity.de Server
// Copyright (C) 2016 Tradity.de Tech Team <tech@tradity.de>
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
"use strict";
const fs = require('fs');
const util = require('util');
const PSemaphore = require('promise-semaphore');
const ratelimit = require('promise-ratelimit');
const api = require('./api.js');
const debug = require('debug')('sotrade:error');
const promiseUtil = require('./lib/promise-util.js');
class ErrorHandler extends api.Component {
constructor() {
super({
identifier: 'ErrorHandler',
description: 'Provides methods for handling, logging, and notifying about errors.',
depends: ['Mailer']
});
this.sem = new PSemaphore();
this.throttle = ratelimit(10000);
}
init() {
this.load('PubSub').on('error', e => this.handleError(e));
}
/**
* Listener for <code>error</code> events.
*
* This logs the error and sends, if possible, an e-mail
* to notify about the error.
* The exact behaviour, such as the e-mail recipient, can be set
* in the server configuration.
*/
handleError(e, noemail) {
if (!e) {
return this.err(new Error('Error without Error object caught -- abort'), true);
}
debug('Error', e);
let cfg, longErrorText;
const catchstack = new Error().stack; // current stack
Promise.resolve().then(() => {
return this.load('Config').config();
}).catch(e2 => {
console.error('Could not get server config due to', e2);
return null;
}).then(cfg_ => {
cfg = cfg_;
return this.throttle();
}).then(() => {
return this.sem.add(() => {
return Promise.resolve().then(() => {
noemail = noemail || false;
longErrorText = process.pid + ': ' + (new Date().toString()) + ': ' + e + '\n';
if (e.stack) {
longErrorText += e.stack + '\n';
} else { // assume e is not actually an Error instance
longErrorText += util.inspect(e) + '\n';
}
// indicating current stack may be helpful
longErrorText += catchstack + '\n';
if (this.bus) {
longErrorText += 'Bus: ' + this.bus.id + '\n';
if (e.nonexistentType || (e.name && e.name.match(/^Assertion/i))) {
longErrorText += '\n' + JSON.stringify(this.bus.busGraph) + '\n';
}
}
if (!process.env.SOTRADE_DO_NOT_OUTPUT_ERRORS) {
console.error(longErrorText);
}
if (cfg && cfg.errorLogFile) {
return promiseUtil.ncall(fs.appendFile)(cfg.errorLogFile.replace(/\{\$pid\}/g, process.pid), longErrorText);
}
}).then(() => {
if (cfg && cfg.mail) {
const opt = Object.assign({}, cfg.mail['errorBase'], {
text: longErrorText
});
return this.load('Mailer').sendMail(opt, null, null, 'error').catch(() => {});
} else {
console.warn('Could not send error mail due to missing config!');
}
});
});
}).catch(e2 => {
console.error('Error while handling other error:\n');
console.error(e2);
console.error(e2 && e2.stack);
console.error('during handling of\n');
console.error(e);
console.error(e && e.stack);
console.trace('aborting');
process.exit(64);
});
}
}
exports.components = [
ErrorHandler
];