The ok value type.
Let yield* bind this Except inside a do block: yields this Except and
returns the ok value the block is resumed with.
It exists for yield*, not for spreading: [...except] is a one-element
array holding this Except itself.
A generator that yields this Except once.
Chain a step that may itself fail; the first failure short-circuits.
If f throws, the error propagates from this call; it is not captured as a
failure (use tryCatch for that).
This is Haskell's >>=.
The new ok value type.
The Except that f returns, or this failure.
Recover from a failure: handler gets the error and its Except replaces
this one. An ok value passes through without calling handler.
If handler throws, the error propagates from this call; it is not
captured as a failure.
This is Haskell's catchError (MonadError). The handler must keep the
value type: it returns Except<A>.
The Except handler returns, or this ok value.
Collapse the Except into one value by handling both cases.
Plays the part of Haskell's either, with the failure handler first.
If onError or onSuccess throws, the error propagates from this call.
The type both handlers return.
What the handler for this case returns.
Extract the ok value, or compute a default from the error when failed.
The fallback receives the error so the default can depend on why the
computation failed.
If fallback throws, the error propagates from this call.
Builds the value to return from the error.
The ok value, or what fallback returns.
Apply f to the error; an ok value passes through unchanged.
If f throws, the error propagates from this call; it is not captured as a
failure.
This is Haskell's withExcept, renamed to match Either#mapLeft. f
must return an Error, because an Except always fails with one; to
leave Error behind, use toEither().mapLeft(…).
The function to apply to the error.
Failed holding f(error), or this ok value.
Pattern-match on the result, calling failed with the error or ok with
the value.
Haskell has no counterpart; it is fold with named handlers in a
{ failed, ok } object, so the call site cannot swap them by position.
If a handler throws, the error propagates from this call.
The type both handlers return.
What the handler for this case returns.
Run a side effect with the ok value, keeping this Except as it is; a
failure passes through without calling f.
If f throws, the error propagates from this call; it is not captured as a
failure.
Haskell has no counterpart; the name matches IO#tap and AsyncIO#tap.
The side effect, given the ok value; its return is ignored.
This Except.
Turn this Except into an Either: ok becomes a Right, a failure
becomes a Left holding the same Error.
This is Haskell's runExcept. From there mapLeft can change the error
type, which mapError can't. Nothing in it calls caller code, so it never
throws.
A Right holding the ok value, or a Left holding the error.
StaticdoRun a do block over Except: Haskell's do notation, with x <- m
written const x = yield* m.
Each yield* m binds the ok value of m, typed with no annotation. The
first failure stops the block there, and the result is that failure. The
block runs at once, so a throw in it propagates from this call; it is not
captured as a failure. A plain yield (without *) binds unknown; use
yield*.
The value the block returns.
An ok Except holding what the block returns, or the first
failure.
import { Except } from '@b-jones-rfd/func-help'
const parse = (s: string): Except<number> =>
Number.isNaN(Number(s))
? Except.failed(new Error(`'${s}' is not a number`))
: Except.ok(Number(s))
const sum = Except.do(function* () {
const a = yield* parse('1') // number
const b = yield* parse('2') // number
return a + b
}) // Except<number>, ok 3
StaticfailedCreate a failed Except holding error.
Stands in for Haskell's throwError; the name pairs with ok.
A defaults to never so the result is assignable to any Except<A>.
The ok value type the result is used as.
The error the computation failed with.
A failed Except holding error.
StaticfromTurn an Either whose Left is an Error into an Except: a Right
becomes ok, a Left becomes a failure holding the same Error.
This is Haskell's except, named after the package's from*
constructors. Any Left type that is an Error (or a subclass) is
accepted; for another Left, mapLeft it to an Error first. Nothing in
it calls caller code, so it never throws.
The Right (ok) value type.
Ok holding the Right value, or failed holding the Left Error.
import { Either, Except } from '@b-jones-rfd/func-help'
const parsePort = (s: string): Either<string, number> =>
/^\d+$/.test(s) ? Either.right(Number(s)) : Either.left(`bad port '${s}'`)
Except.fromEither(parsePort('80').mapLeft((m) => new Error(m))) // ok 80
Except.fromEither(parsePort('x').mapLeft((m) => new Error(m)))
// failed: bad port 'x'
StaticfromTurn a possibly absent value into an Except, failing with the error
from onNull when the value is null or undefined.
onNull is a thunk so the error, and its stack trace, is built only when
the value is absent.
If onNull throws, the error propagates from this call.
The value type when present.
The value that may be null or undefined.
Builds the error for an absent value.
Ok holding value, or failed holding the error from onNull.
StaticofStaticokStaticsequenceTurn an array of Excepts into an Except of an array: ok holding every
value in input order, or the first failure.
An empty array gives ok []. Nothing in it calls caller code, so it
never throws.
This is Haskell's sequence (Data.Traversable). A tuple gives a tuple:
[Except<number>, Except<string>] gives Except<[number, string]>, with
no as const. An array gives an array.
The array or tuple of Excepts.
The Excepts, in order.
Ok holding the values in input order (a tuple for a tuple), or the first failure.
StatictraverseApply f to each item in order and collect the values into one array;
the first failure stops it, and f is not called for the items after.
An empty array gives ok [].
If f throws, the error propagates from this call; it is not captured as a
failure.
This is Haskell's traverse (also mapM). Like Array#map, f also
gets the item's index.
The item type.
The value type f returns.
Ok holding every value in input order, or the first failure.
import { Except } from '@b-jones-rfd/func-help'
const parsePort = (s: string): Except<number> =>
/^\d+$/.test(s)
? Except.ok(Number(s))
: Except.failed(new Error(`'${s}' is not a port`))
Except.traverse(['80', '443'], parsePort) // ok [80, 443]
Except.traverse(['80', 'http'], parsePort) // failed: 'http' is not a port
StatictryRun effect and capture a throw from it as a failed Except.
This is the bridge from throwing code; no other member captures throws.
Without onError, a thrown value that is not an Error is wrapped in one,
with the original kept as its cause; if the value can't be turned into
a string, the message is 'Non-Error value thrown'.
Only a throw from effect is captured; if onError throws, the error
propagates from this call.
The value type effect returns.
The code that may throw.
OptionalonError: (e: unknown) => Error
Optional; turns the thrown value into the failure's
Error.
Ok holding the result of effect, or failed holding the error.
A value that is either failed, holding an
Error, or ok, holding anA.mapandbindrun only on an ok value, so the first failure short-circuits the rest of the chain.Haskell's
Except e atakes any error type; thisExcept<A>fixes the error toError, so every failure carries a message and a stack.A throw in a caller-supplied function propagates synchronously from the call that ran it and is not captured as a failure; only
tryCatchcaptures throws, and only fromeffect.Example