Emit request errors after aborted requests#4580
Conversation
0014f64 to
6964cf6
Compare
|
@puneetdixit200 Thanks for the contribution ... something I picked up doing some testing as an edge case: double The fix works correctly with the default server.route({
method: 'GET',
path: '/',
options: {
response: { disconnectStatusCode: 500 }
},
handler: async (request) => {
const result = await someSlowOperation(); // client disconnects during this
return result; // then this throws
}
});What happens:
With the default 499, step 1 skips logging (not a 500), so only step 2 fires. But Reproduction test (currently fails against this PR — it('does not double-emit request-error when disconnectStatusCode is 500', { retry: true }, async (flags) => {
const server = Hapi.server({ debug: false });
const team = new Teamwork.Team();
let emitCount = 0;
const errors = [];
server.events.on({ name: 'request', channels: 'error' }, (request, event) => {
emitCount++;
errors.push(event.error.message);
});
server.route({
method: 'GET',
path: '/',
handler: async (request) => {
clientRequest.destroy();
await Hoek.wait(10);
team.attend();
throw new Error('late failure');
},
options: {
response: {
disconnectStatusCode: 500
}
}
});
await server.start();
flags.onCleanup = () => server.stop();
const clientRequest = Http.request({
hostname: 'localhost',
port: server.info.port,
method: 'GET'
});
clientRequest.on('error', Hoek.ignore);
clientRequest.end();
await team.work;
await Hoek.wait(50);
expect(emitCount).to.equal(1);
expect(errors[0]).to.equal('late failure');
await server.stop();
});Perhaps, if we know aborted requests as 499, we could specifically check for it to prevent the double log? This is a very niche case and I have no idea who configures their routes this way. |
Skip hapi-generated disconnect Boom responses when logging internal 500 request errors, so late handler failures after an abort are still emitted once without also emitting the configured disconnect response. Generated-by: OpenAI Codex Signed-off-by: Puneet Dixit <236133619+puneetdixit200@users.noreply.github.com>
|
Pushed What changed:
Verified locally:
|
Summary:
Tests:
npm_config_cache=/tmp/hapi-npm-cache npx lab -a @hapi/code -m 5000 test/request.js -g "emits request-error when handler fails after abort|does not fail on abort|emits request-error once"source ~/.nvm/nvm.sh && nvm use 18.20.8 >/dev/null && npm_config_cache=/tmp/hapi-npm-cache npx lab -a @hapi/code -m 5000 test/request.js -g "emits request-error when handler fails after abort|does not fail on abort|emits request-error once"git diff --checkFixes #4567