(u: unknown): numberReads 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.
export const 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 = (u: unknownu: unknown): number => {
if (typeof u: unknownu === "object" && u: object | nullu !== null && const errorExitCode: errorExitCodeType-level key for the Runtime.errorExitCode marker.
When to use
Use to type properties keyed by Runtime.errorExitCode on custom error
values.
Allows associating an exit code with an error for determining the process
exit code on failure.
When to use
Use when error classes should map failures to a specific process exit code
when handled by
defaultTeardown
.
Details
Attach this marker as a readonly property on an error object. When the main
program fails,
defaultTeardown
squashes the Cause and reads the marker
from the resulting error value.
Gotchas
The marker is read from the squashed failure value. If a Cause contains
multiple failures, the selected squashed error determines the exit code.
Example (Setting a process exit code)
import { Data, Effect, Runtime } from "effect"
import { NodeRuntime } from "@effect/platform-node"
class MyError extends Data.TaggedError("MyError") {
readonly [Runtime.errorExitCode] = 42
}
// If the program fails with MyError, the process will exit with code 42
NodeRuntime.runMain(Effect.fail(new MyError()))
errorExitCode in u: objectu) {
const const code: unknowncode = u: object &
Record<"~effect/Runtime/errorExitCode", unknown>
u[const errorExitCode: errorExitCodeType-level key for the Runtime.errorExitCode marker.
When to use
Use to type properties keyed by Runtime.errorExitCode on custom error
values.
Allows associating an exit code with an error for determining the process
exit code on failure.
When to use
Use when error classes should map failures to a specific process exit code
when handled by
defaultTeardown
.
Details
Attach this marker as a readonly property on an error object. When the main
program fails,
defaultTeardown
squashes the Cause and reads the marker
from the resulting error value.
Gotchas
The marker is read from the squashed failure value. If a Cause contains
multiple failures, the selected squashed error determines the exit code.
Example (Setting a process exit code)
import { Data, Effect, Runtime } from "effect"
import { NodeRuntime } from "@effect/platform-node"
class MyError extends Data.TaggedError("MyError") {
readonly [Runtime.errorExitCode] = 42
}
// If the program fails with MyError, the process will exit with code 42
NodeRuntime.runMain(Effect.fail(new MyError()))
errorExitCode]
if (typeof const code: unknowncode === "number") {
return const code: numbercode
}
}
return 1
}