Skip to content

Commit

Permalink
Raise errors by default
Browse files Browse the repository at this point in the history
Disable by passing `catchErrors: true` to Robot
  • Loading branch information
bkeepers committed Jun 30, 2017
1 parent 1cc4505 commit 6d574de
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -31,7 +31,7 @@ module.exports = (options = {}) => {
debug: process.env.LOG_LEVEL === 'trace'
});
const server = createServer(webhook);
const robot = createRobot({app, webhook, cache, logger});
const robot = createRobot({app, webhook, cache, logger, catchErrors: true});

// Forward webhooks to robot
webhook.on('*', event => {
Expand Down
9 changes: 4 additions & 5 deletions lib/robot.js
Expand Up @@ -9,12 +9,12 @@ const Context = require('./context');
* @property {logger} log - A logger
*/
class Robot {
constructor({app, cache, logger, throwErrors} = {}) {
constructor({app, cache, logger, catchErrors} = {}) {
this.events = new EventEmitter();
this.app = app;
this.cache = cache;
this.log = wrapLogger(logger);
this.throwErrors = throwErrors;
this.catchErrors = catchErrors;
}

async receive(event) {
Expand Down Expand Up @@ -66,10 +66,9 @@ class Robot {
const context = new Context(event, github);
return callback(context, context /* DEPRECATED: for backward compat */);
} catch (err) {
if (this.throwErrors) {
this.log.error(err);
if (!this.catchErrors) {
throw err;
} else {
this.log.error(err);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Expand Up @@ -6,7 +6,7 @@ describe('Robot', () => {
let event;

beforeEach(() => {
robot = createRobot({throwErrors: true});
robot = createRobot();
robot.auth = () => Promise.resolve({});

event = {
Expand Down
6 changes: 5 additions & 1 deletion test/robot.js
Expand Up @@ -100,7 +100,11 @@ describe('Robot', function () {
throw error;
});

await robot.receive(event);
try {
await robot.receive(event);
} catch (err) {
// Expected
}

expect(robot.log.error).toHaveBeenCalledWith(error);
});
Expand Down

0 comments on commit 6d574de

Please sign in to comment.