mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
fix(ext/http): gracefully handle Response.error responses (#25712)
Fixes #14371
This commit is contained in:
parent
ab1e391e1d
commit
7a41a93997
2 changed files with 41 additions and 1 deletions
|
@ -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) {
|
||||
|
|
|
@ -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>();
|
||||
|
|
Loading…
Reference in a new issue