Skip to content

Commit 1d3143c

Browse files
authored
Merge pull request #172 from StackStorm/fix/unhandled-rejection
Exit hubot on unhandled promise rejections and other unrecoverable errors
2 parents 2f2528a + 47ef38d commit 1d3143c

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Changelog
33

44
in development
55
--------------
6+
* Exit hubot on unhandled promise rejections and uncaught exceptions, usually caused by unrecoverable errors (bug fix)
67

78
0.9.3
89
-----

scripts/stackstorm.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,29 @@ var TWOFACTOR_MESSAGE = "This action requires two-factor auth! Waiting for your
9999
module.exports = function(robot) {
100100
slack_monkey_patch.patchSendMessage(robot);
101101

102+
// Makes the script crash on unhandled rejections instead of ignoring them and keep running.
103+
// Usually happens when trying to connect to a nonexistent instances or similar unrecoverable issues.
104+
// In the future Node.js versions, promise rejections that are not handled will terminate the process with a non-zero exit code.
105+
process.on('unhandledRejection', function(err) {
106+
throw err;
107+
});
108+
109+
// Handle uncaught exceptions, log error and terminate hubot if one occurs
110+
robot.error(function(err, res) {
111+
if (err) {
112+
robot.logger.error(err.stack || JSON.stringify(err));
113+
}
114+
if (res) {
115+
res.send(JSON.stringify({
116+
"status": "failed",
117+
"msg": "An error occurred trying to post the message:\n" + err
118+
}));
119+
}
120+
121+
robot.logger.info('Hubot will shut down ...');
122+
robot.shutdown();
123+
});
124+
102125
var self = this;
103126

104127
var promise = Promise.resolve();

0 commit comments

Comments
 (0)