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

Function causeResponse

packages/effect/src/http/HttpServerError.ts:304–347  ·  view source on GitHub ↗
(
  cause: Cause.Cause<E>
)

Source from the content-addressed store, hash-verified

302 * @since 4.0.0
303 */
304export const causeResponse = <E>(
305 cause: Cause.Cause<E>
306): Effect.Effect<readonly [Response.HttpServerResponse, Cause.Cause<E>]> => {
307 let response: Response.HttpServerResponse | undefined
308 let effect = succeedInternalServerError
309 const failures: Array<Cause.Reason<E>> = []
310 let interrupts: Array<Cause.Interrupt> = []
311 let isClientInterrupt = false
312 for (let i = 0; i < cause.reasons.length; i++) {
313 const reason = cause.reasons[i]
314 switch (reason._tag) {
315 case "Fail": {
316 effect = Respondable.toResponseOrElse(reason.error, internalServerError)
317 failures.push(reason)
318 break
319 }
320 case "Die": {
321 if (Response.isHttpServerResponse(reason.defect)) {
322 response = reason.defect
323 } else {
324 effect = Respondable.toResponseOrElseDefect(reason.defect, internalServerError)
325 failures.push(reason)
326 }
327 break
328 }
329 case "Interrupt": {
330 isClientInterrupt = reason.annotations.has(ClientAbort.key)
331 if (failures.length > 0) break
332 interrupts.push(reason)
333 break
334 }
335 }
336 }
337 if (response) {
338 return Effect.succeed([response, Cause.fromReasons(failures)] as const)
339 } else if (interrupts.length > 0 && failures.length === 0) {
340 failures.push(...interrupts)
341 effect = isClientInterrupt ? clientAbortError : serverAbortError
342 }
343 return Effect.mapEager(effect, (response) => {
344 failures.push(Cause.makeDieReason(response))
345 return [response, Cause.fromReasons(failures)] as const
346 })
347}
348
349/**
350 * Derives an HTTP response from a failed handler cause synchronously.

Callers 3

handleCauseFunction · 0.90
handleCauseFunction · 0.90
sendCauseFunction · 0.90

Calls 3

pushMethod · 0.65
hasMethod · 0.65
succeedMethod · 0.45

Tested by

no test coverage detected