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

Use HTTP code of underlying errors #715

Merged
merged 1 commit into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/__tests__/http-test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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