Skip to content

Commit

Permalink
Log unhandled errors from next but pass on the error
Browse files Browse the repository at this point in the history
  • Loading branch information
pablosichert committed Oct 26, 2016
1 parent 5ec3e28 commit 1aa4355
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
13 changes: 11 additions & 2 deletions src/index.js
Expand Up @@ -313,11 +313,14 @@ export default function createLogger(options = {}) {
print(req(context));
}

let exception;

try {
await next();
} catch(error) {
context.status = 500;
log.error(error);

exception = error;
}

const end = new Date;
Expand Down Expand Up @@ -353,7 +356,9 @@ export default function createLogger(options = {}) {

let $status;

if (status >= 100 && status < 200) {
if (exception) {
$status = colorize('fatal')('ERR');
} else if (status >= 100 && status < 200) {
$status = colorize('info')(status);
} else if (status < 300) {
$status = status;
Expand All @@ -377,5 +382,9 @@ export default function createLogger(options = {}) {
}

slots[slot] = null;

if (exception) {
throw exception;
}
};
}
@@ -1,4 +1,4 @@
──‣ ┈┈┈┈┈ GET┈ ┬ /
╎ Error
╎  at stack
500 ┈┈0ms GET┈ ┴ /
ERR ┈┈0ms GET┈ ┴ /
10 changes: 5 additions & 5 deletions test/index.js
Expand Up @@ -257,7 +257,7 @@ describe('logger', () => {
expect(this.output, 'to equal', fixtures[title]);
});

it('should log an unhandled error and set status to 500', async function () {
it('should log an unhandled error', async function () {
const title = this.test.fullTitle();
const createLogger = this.createLogger(title);

Expand All @@ -268,14 +268,14 @@ describe('logger', () => {
originalUrl: '/'
};

const next = () => {
const error = new Error();
error.stack = 'Error\n at stack';
const error = new Error();
error.stack = 'Error\n at stack';

const next = () => {
throw error;
};

await logger(context, next);
await expect(logger(context, next), 'to be rejected with', error);

expect(this.output, 'to equal', fixtures[title]);
});
Expand Down

0 comments on commit 1aa4355

Please sign in to comment.