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

Commit

Permalink
Resolve timing.end on errored requests (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
ganemone authored Oct 29, 2018
1 parent 83f7a0f commit 1bf1469
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
40 changes: 40 additions & 0 deletions src/__tests__/timing.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,43 @@ test('timing plugin', async t => {
t.end();
});
});

test('timing plugin on error middleware', async t => {
const element = 'hi';
const renderFn = el => {
return el;
};
const app = new App(element, renderFn);
let resolved = {
downstream: false,
upstream: false,
render: false,
};
app.middleware((ctx, next) => {
ctx.timing.downstream.then(result => {
resolved.downstream = true;
});
ctx.timing.render.then(result => {
resolved.render = true;
});
ctx.timing.upstream.then(result => {
resolved.upstream = true;
});
ctx.timing.end.then(result => {
t.equal(typeof result, 'number', 'sets end timing result');
t.equal(resolved.downstream, false, 'does not resolve downstream');
t.equal(resolved.render, false, 'does not resolve render');
t.equal(resolved.upstream, false, 'does not resolve upstream');
t.equal(ctx.status, 500, 'sets ctx.status');
t.end();
});
return next();
});
app.middleware((ctx, next) => {
const e = new Error('fail request');
// $FlowFixMe
e.status = 500;
throw e;
});
await run(app).catch(e => {});
});
24 changes: 18 additions & 6 deletions src/plugins/timing.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,24 @@ function middleware(ctx, next) {
downstream: downstream.promise,
upstream: upstream.promise,
};
return next().then(() => {
const upstreamTime = now() - timing.from(ctx).upstreamStart;
upstream.resolve(upstreamTime);
const endTime = now() - ctx.timing.start;
end.resolve(endTime);
});
return next()
.then(() => {
const upstreamTime = now() - timing.from(ctx).upstreamStart;
upstream.resolve(upstreamTime);
const endTime = now() - ctx.timing.start;
end.resolve(endTime);
})
.catch(e => {
// currently we only resolve upstream and downstream when the request does not error
// we should however always resolve the request end timing
if (e && e.status) {
// this ensures any logging / metrics based on ctx.status will recieve the correct status code
ctx.status = e.status;
}
const endTime = now() - ctx.timing.start;
end.resolve(endTime);
throw e;
});
}

export default createPlugin({
Expand Down

0 comments on commit 1bf1469

Please sign in to comment.