Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
Use HTTP code of underlying errors (#715)
Browse files Browse the repository at this point in the history
Fixes #702
  • Loading branch information
IvanGoncharov committed Nov 9, 2020
1 parent 5a4192d commit b462653
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/http-test.ts
Expand Up @@ -1462,7 +1462,7 @@ function runTests(server: Server) {
.set('Content-Type', 'application/json')
.send(`{ "query": "{ ${new Array(102400).fill('test').join('')} }" }`);

expect(response.status).to.equal(400);
expect(response.status).to.equal(413);
expect(JSON.parse(response.text)).to.deep.equal({
errors: [{ message: 'Invalid body: request entity too large.' }],
});
Expand Down
16 changes: 12 additions & 4 deletions src/parseBody.ts
Expand Up @@ -100,10 +100,18 @@ async function readBody(
// Read body from stream.
try {
return await getBody(stream, { encoding: charset, length, limit });
} catch (err) {
throw err.type === 'encoding.unsupported'
? httpError(415, `Unsupported charset "${charset.toUpperCase()}".`)
: httpError(400, `Invalid body: ${String(err.message)}.`);
} catch (rawError: unknown) {
const error = httpError(
400,
/* istanbul ignore next: Thrown by underlying library. */
rawError instanceof Error ? rawError : String(rawError),
);

error.message =
error.type === 'encoding.unsupported'
? `Unsupported charset "${charset.toUpperCase()}".`
: `Invalid body: ${error.message}.`;
throw error;
}
}

Expand Down

0 comments on commit b462653

Please sign in to comment.