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

Function causeResponse

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

Source from the content-addressed store, hash-verified

281 * @since 4.0.0
282 */
283export const causeResponse = <E>(
284 cause: Cause.Cause<E>
285): Effect.Effect<readonly [Response.HttpServerResponse, Cause.Cause<E>]> => {
286 let response: Response.HttpServerResponse | undefined
287 let effect = succeedInternalServerError
288 const failures: Array<Cause.Reason<E>> = []
289 let interrupts: Array<Cause.Interrupt> = []
290 let isClientInterrupt = false
291 for (let i = 0; i < cause.reasons.length; i++) {
292 const reason = cause.reasons[i]
293 switch (reason._tag) {
294 case "Fail": {
295 effect = Respondable.toResponseOrElse(reason.error, internalServerError)
296 failures.push(reason)
297 break
298 }
299 case "Die": {
300 if (Response.isHttpServerResponse(reason.defect)) {
301 response = reason.defect
302 } else {
303 effect = Respondable.toResponseOrElseDefect(reason.defect, internalServerError)
304 failures.push(reason)
305 }
306 break
307 }
308 case "Interrupt": {
309 isClientInterrupt = reason.annotations.has(ClientAbort.key)
310 if (failures.length > 0) break
311 interrupts.push(reason)
312 break
313 }
314 }
315 }
316 if (response) {
317 return Effect.succeed([response, Cause.fromReasons(failures)] as const)
318 } else if (interrupts.length > 0 && failures.length === 0) {
319 failures.push(...interrupts)
320 effect = isClientInterrupt ? clientAbortError : serverAbortError
321 }
322 return Effect.mapEager(effect, (response) => {
323 failures.push(Cause.makeDieReason(response))
324 return [response, Cause.fromReasons(failures)] as const
325 })
326}
327
328/**
329 * Derives an HTTP response from a failed handler cause synchronously.

Callers 3

handleCauseFunction · 0.90
handleCauseFunction · 0.90
onFailureFunction · 0.90

Calls 3

pushMethod · 0.80
hasMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected