MCPcopy Create free account
hub / github.com/Effect-TS/effect / handleCause

Function handleCause

packages/platform/node/src/NodeHttpServer.ts:1301–1336  ·  view source on GitHub ↗
(
  nodeResponse: Http.ServerResponse,
  originalResponse: HttpServerResponse
)

Source from the content-addressed store, hash-verified

1299}
1300
1301const handleCause = (
1302 nodeResponse: Http.ServerResponse,
1303 originalResponse: HttpServerResponse
1304) =>
1305<E>(originalCause: Cause.Cause<E>) =>
1306 Effect.flatMap(causeResponse(originalCause), ([response, cause]) => {
1307 const headersSent = nodeResponse.headersSent
1308 let abort = headersSent
1309 if (!headersSent) {
1310 if (!isFinalStatus(response.status)) {
1311 cause = Cause.combine(cause, Cause.die(invalidStatusError(response.status)))
1312 response = internalServerError
1313 }
1314 try {
1315 writeHead(nodeResponse, response, {})
1316 } catch {
1317 // HTTP/2 keeps the headers of a rejected `writeHead`, so the fallback can throw too
1318 abort = true
1319 }
1320 }
1321 if (!nodeResponse.writableEnded) {
1322 // Ending a started response normally would make a partial body look complete
1323 if (abort) {
1324 // An error resets an HTTP/2 stream with INTERNAL_ERROR rather than NO_ERROR. On
1325 // HTTP/1.1 it could reach `clientError` listeners, and the socket closes either way.
1326 nodeResponse.destroy(isHttp2(nodeResponse.req) ? new Error("The response was aborted") : undefined)
1327 } else {
1328 nodeResponse.end()
1329 }
1330 }
1331 return Effect.failCause(
1332 headersSent
1333 ? Cause.combine(originalCause, Cause.die(originalResponse))
1334 : cause
1335 )
1336 })

Callers 3

handleResponseFunction · 0.70
writeResponseFunction · 0.70
endFunction · 0.70

Calls 8

causeResponseFunction · 0.90
isFinalStatusFunction · 0.85
invalidStatusErrorFunction · 0.85
writeHeadFunction · 0.85
isHttp2Function · 0.85
combineMethod · 0.65
endMethod · 0.65
destroyMethod · 0.45

Tested by

no test coverage detected