1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(ext/http): gracefully handle Response.error responses (#25712)

Fixes #14371
This commit is contained in:
Luca Casonato 2024-09-18 21:14:40 +02:00 committed by GitHub
parent ab1e391e1d
commit 7a41a93997
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 1 deletions

View file

@ -511,9 +511,15 @@ function mapToCallback(context, callback, onError) {
);
}
if (response.type === "error") {
throw new TypeError(
"Return value from serve handler must not be an error response (like Response.error())",
);
}
if (response.bodyUsed) {
throw new TypeError(
"The body of the Response returned from the serve handler has already been consumed.",
"The body of the Response returned from the serve handler has already been consumed",
);
}
} catch (error) {

View file

@ -678,6 +678,40 @@ Deno.test(
},
);
Deno.test(
{ permissions: { net: true } },
async function httpServerReturnErrorResponse() {
const ac = new AbortController();
const { promise, resolve } = Promise.withResolvers<void>();
let hadError = false;
const server = Deno.serve({
handler: () => {
return Response.error();
},
port: servePort,
signal: ac.signal,
onListen: onListen(resolve),
onError: () => {
hadError = true;
return new Response("Internal Server Error", { status: 500 });
},
});
await promise;
const resp = await fetch(`http://127.0.0.1:${servePort}/`, {
headers: { "connection": "close" },
});
assertEquals(resp.status, 500);
const text = await resp.text();
assertEquals(text, "Internal Server Error");
assert(hadError);
ac.abort();
await server.finished;
},
);
Deno.test({ permissions: { net: true } }, async function httpServerOverload1() {
const ac = new AbortController();
const deferred = Promise.withResolvers<void>();