Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Update error handling logic
Browse files Browse the repository at this point in the history
#188

Co-authored-by: Giancarlo Anemone <ganemone@uber.com>
  • Loading branch information
2 people authored and fusion-bot[bot] committed Mar 27, 2019
1 parent 72b8821 commit 728481c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/__tests__/test.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,34 @@ test('Server EventEmitter - mapping', async t => {
t.end();
});

test('Server EventEmitter error handling', async t => {
t.plan(1);
const app = new App('fake-element', el => el);
app.register(UniversalEventsToken, UniversalEventsPlugin);
app.middleware({events: UniversalEventsToken}, ({events}) => {
return async (ctx, next) => {
const emitter = events.from(ctx);
emitter.on('test-pre-await', ({x}) => {
t.equals(x, 1, 'payload is correct');
});
emitter.emit('test-pre-await', {x: 1});
ctx.throw(403, 'error');
return next();
};
});
app.middleware((ctx, next) => {
t.fail('should not reach this middleware');
return next();
});
const simulator = getSimulator(app);
await simulator
.request('/lol', {method: 'POST'})
.then(() => {
t.fail('should throw');
})
.catch(() => {});
});

test('Server EventEmitter batching', async t => {
const app = new App('fake-element', el => el);
const flags = {
Expand Down
12 changes: 8 additions & 4 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,14 @@ const plugin =
}
// awaiting next before registering `then` on ctx.timing.end to try and get as much as possible
// into the event batch flush.
await next();
ctx.timing.end.then(() => {
emitter.flush();
});
try {
await next();
} finally {
// handle flushing in the case of an error
ctx.timing.end.then(() => {
emitter.flush();
});
}
};
},
});
Expand Down

0 comments on commit 728481c

Please sign in to comment.