<E, A>(exit: Exit.Exit<E, A>, onExit: (code: number) => void): voidThe default teardown function that determines exit codes from an Effect exit.
When to use
Use as the standard teardown for main programs with conventional process exit codes and support for errorExitCode.
Details
This teardown follows these exit-code rules:
0for successful completion.130for interruption-only failures.- The squashed error's errorExitCode value for other failures when present.
1for other failures.
Gotchas
The 130 code is used only when the Cause contains interruptions and no
other failure reasons. Mixed causes use the squashed error path instead.
Example (Referencing default teardown)
import { Exit, Runtime } from "effect"
const logExitCode = (exit: Exit.Exit<any, any>) => {
Runtime.defaultTeardown(exit, (code) => {
console.log(`Exit code: ${code}`)
})
}
logExitCode(Exit.succeed(42))
// Output: Exit code: 0
logExitCode(Exit.fail("error"))
// Output: Exit code: 1
logExitCode(Exit.interrupt(123))
// Output: Exit code: 130export const const defaultTeardown: TeardownThe default teardown function that determines exit codes from an Effect exit.
When to use
Use as the standard teardown for main programs with conventional process
exit codes and support for
errorExitCode
.
Details
This teardown follows these exit-code rules:
0 for successful completion.
130 for interruption-only failures.
- The squashed error's
errorExitCode
value for other failures when
present.
1 for other failures.
Gotchas
The 130 code is used only when the Cause contains interruptions and no
other failure reasons. Mixed causes use the squashed error path instead.
Example (Referencing default teardown)
import { Exit, Runtime } from "effect"
const logExitCode = (exit: Exit.Exit<any, any>) => {
Runtime.defaultTeardown(exit, (code) => {
console.log(`Exit code: ${code}`)
})
}
logExitCode(Exit.succeed(42))
// Output: Exit code: 0
logExitCode(Exit.fail("error"))
// Output: Exit code: 1
logExitCode(Exit.interrupt(123))
// Output: Exit code: 130
defaultTeardown: Teardown = <function (type parameter) E in <E, A>(exit: Exit.Exit<E, A>, onExit: (code: number) => void): voidE, function (type parameter) A in <E, A>(exit: Exit.Exit<E, A>, onExit: (code: number) => void): voidA>(
exit: Exit.Exit<E, A>exit: import ExitExit.type Exit<A, E = never> = Exit.Success<A, E> | Exit.Failure<A, E>Represents the result of an Effect computation.
When to use
Use when you need to synchronously inspect whether an Effect computation
succeeded or failed.
Details
An Exit<A, E> is either Success<A, E> containing a value of type A, or
Failure<A, E> containing a Cause<E> describing why the computation
failed.
Since Exit is also an Effect, you can yield it inside Effect.gen.
Example (Pattern matching on an Exit)
import { Exit } from "effect"
const success: Exit.Exit<number> = Exit.succeed(42)
const failure: Exit.Exit<number, string> = Exit.fail("error")
const result = Exit.match(success, {
onSuccess: (value) => `Got value: ${value}`,
onFailure: (cause) => `Got error: ${cause}`
})
Namespace containing helper types shared by Exit values.
When to use
Use to reference helper types that describe the shared structure of Exit
values.
Exit<function (type parameter) E in <E, A>(exit: Exit.Exit<E, A>, onExit: (code: number) => void): voidE, function (type parameter) A in <E, A>(exit: Exit.Exit<E, A>, onExit: (code: number) => void): voidA>,
onExit: (code: number) => voidonExit: (code: numbercode: number) => void
) => {
if (import ExitExit.const isSuccess: <A, E>(
self: Exit<A, E>
) => self is Success<A, E>
Checks whether an Exit is a Success.
When to use
Use as a type guard to narrow Exit<A, E> to Success<A, E> and access the
value property.
Example (Narrowing to success)
import { Exit } from "effect"
const exit = Exit.succeed(42)
if (Exit.isSuccess(exit)) {
console.log(exit.value) // 42
}
isSuccess(exit: Exit.Exit<E, A>exit)) return onExit: (code: number) => voidonExit(0)
if (import CauseCause.const hasInterruptsOnly: <E>(
self: Cause<E>
) => boolean
Returns true if every reason in the cause is an Interrupt (and
there is at least one reason).
When to use
Use when you need to detect failures caused only by interruption.
Example (Checking interrupt-only causes)
import { Cause } from "effect"
console.log(Cause.hasInterruptsOnly(Cause.interrupt(123))) // true
console.log(Cause.hasInterruptsOnly(Cause.fail("error"))) // false
console.log(Cause.hasInterruptsOnly(Cause.empty)) // false
hasInterruptsOnly(exit: Exit.Exit<E, A>(parameter) exit: {
_tag: "Failure";
cause: Cause.Cause<E>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
exit.Failure<E, A>.cause: Cause.Cause<E>(property) Failure<E, A>.cause: {
reasons: ReadonlyArray<Reason<E>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
cause)) return onExit: (code: number) => voidonExit(130)
return onExit: (code: number) => voidonExit(const getErrorExitCode: (
u: unknown
) => number
Reads the runtime exit-code marker from an unknown error value.
When to use
Use to read a custom failure exit code from an unknown error value, falling
back to the default failure code.
Details
Returns the numeric [Runtime.errorExitCode] property when it is present on
an object. Otherwise returns 1, the default failure exit code used by
defaultTeardown.
Gotchas
Non-object values, missing markers, and non-number marker values all return
1.
getErrorExitCode(import CauseCause.const squash: <E>(
self: Cause<E>
) => unknown
Collapses a Cause into a single unknown value, picking the "most
important" failure in this order:
When to use
Use to collapse a structured cause to the single value that synchronous and
promise runners would throw.
Details
- First
Fail error (the E value)
- First
Die defect
- A generic
Error("All fibers interrupted without error") for interrupt-only causes
- A generic
Error("Empty cause") for empty
This is the function used by Effect.runPromise and Effect.runSync to
decide what to throw.
Gotchas
This function is lossy. Use
prettyErrors
or iterate cause.reasons
when you need all failures.
Example (Squashing a cause)
import { Cause } from "effect"
console.log(Cause.squash(Cause.fail("error"))) // "error"
console.log(Cause.squash(Cause.die("defect"))) // "defect"
squash(exit: Exit.Exit<E, A>(parameter) exit: {
_tag: "Failure";
cause: Cause.Cause<E>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
exit.Failure<E, A>.cause: Cause.Cause<E>(property) Failure<E, A>.cause: {
reasons: ReadonlyArray<Reason<E>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
cause)))
}