Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion ext/http/00_serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/serve_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,41 @@ 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>();
Expand Down
Loading