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

Commit b462653

Browse files
Use HTTP code of underlying errors (#715)
Fixes #702
1 parent 5a4192d commit b462653

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/__tests__/http-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@ function runTests(server: Server) {
14621462
.set('Content-Type', 'application/json')
14631463
.send(`{ "query": "{ ${new Array(102400).fill('test').join('')} }" }`);
14641464

1465-
expect(response.status).to.equal(400);
1465+
expect(response.status).to.equal(413);
14661466
expect(JSON.parse(response.text)).to.deep.equal({
14671467
errors: [{ message: 'Invalid body: request entity too large.' }],
14681468
});

src/parseBody.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,18 @@ async function readBody(
100100
// Read body from stream.
101101
try {
102102
return await getBody(stream, { encoding: charset, length, limit });
103-
} catch (err) {
104-
throw err.type === 'encoding.unsupported'
105-
? httpError(415, `Unsupported charset "${charset.toUpperCase()}".`)
106-
: httpError(400, `Invalid body: ${String(err.message)}.`);
103+
} catch (rawError: unknown) {
104+
const error = httpError(
105+
400,
106+
/* istanbul ignore next: Thrown by underlying library. */
107+
rawError instanceof Error ? rawError : String(rawError),
108+
);
109+
110+
error.message =
111+
error.type === 'encoding.unsupported'
112+
? `Unsupported charset "${charset.toUpperCase()}".`
113+
: `Invalid body: ${error.message}.`;
114+
throw error;
107115
}
108116
}
109117

0 commit comments

Comments
 (0)