Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

httpResponseErrorGuard.ts

24 lines684 Bytes

1// @effect-diagnostics nodeBuiltinImport:off
2import type * as NodeHttp from "node:http";
3​
4/**
5 * An "error" event without a listener escalates into an uncaught exception and
6 * terminates the whole server process, taking every other client and all
7 * in-flight provider work with it.
8 */
9export function guardHttpResponseWriteErrors<T extends NodeHttp.Server>(
10 server: T,
11 onError?: (error: unknown) => void,
12): T {
13 server.on("request", (_request, response) => {
14 response.on("error", (error) => {
15 onError?.(error);
16 });
17 });
18 server.on("upgrade", (_request, socket) => {
19 socket.on("error", (error) => {
20 onError?.(error);
21 });
22 });
23 return server;