Repository F# setup
open Reified
open Reified.Refinements
open Reified.Result
open Reified.Schema.JsonHandling errors
The error channel gets the same treatment as the success channel: map it, replace it, or use it to produce a fallback.
open Reified.Result
type SignupError =
| NameMissing
| AgeMissing
ReifiedResult03-result-handling_30-handling-errors.md_page.SignupErrorNameMissingAgeMissingChanging the error
mapError transforms whatever the error carries. Use it when the existing error holds something worth keeping.
Error NameMissing |> Result.mapError (fun _ -> "name is required")
// Error "name is required"
ErrorRepresents an Error or a Failure. The code failed with a value of 'TError representing what went wrong.
NameMissing(|>): 'T1 -> ('T1 -> 'U) -> 'UApply a function to a value, the value being on the left, the function on the right The argument. The function. The function result. let doubleIt x = x * 2 3 |> doubleIt // Evaluates to 6
Reified.Result.ResultModuleFail-fast helpers over the standard F# Result type.
mapError: ('a -> 'b) -> Result<'c,'a> -> Result<'c,'b>Maps the error value of a result.
orError discards the existing error and substitutes yours. Use it when the incoming error carries nothing — the
unit from okIf, failIf, or fromTry, or a low-level detail the caller should not see.
Error "index out of range" |> Result.orError NameMissing
// Error NameMissing
ErrorRepresents an Error or a Failure. The code failed with a value of 'TError representing what went wrong.
(|>): 'T1 -> ('T1 -> 'U) -> 'UApply a function to a value, the value being on the left, the function on the right The argument. The function. The function result. let doubleIt x = x * 2 3 |> doubleIt // Evaluates to 6
Reified.Result.ResultModuleFail-fast helpers over the standard F# Result type.
orError: 'error -> Result<'value,'discardedError> -> Result<'value,'error>Replaces whatever error a result carries with the supplied typed error. Ok passes through unchanged. The natural follow-up to okIf/failIf, which fail with unit precisely so the reason is chosen here: value |> Result.okIf isValid |> Result.orError MyError. Use Result.mapError instead when the existing error carries something worth keeping, as a Violation does.
NameMissingRecovering
orElse supplies a fallback result when the first one fails. The fallback is an already-computed value.
(Error AgeMissing: Result<int, SignupError>) |> Result.orElse (Ok 0)
// Ok 0
Ok 36 |> Result.orElse (Ok 0)
// Ok 36
ErrorRepresents an Error or a Failure. The code failed with a value of 'TError representing what went wrong.
AgeMissingMicrosoft.FSharp.Core.FSharpResult`2Helper type for error handling without exceptions. Choices and Results
intAn abbreviation for the CLI type . Basic Types
03-result-handling_30-handling-errors.md_page.SignupError(|>): 'T1 -> ('T1 -> 'U) -> 'UApply a function to a value, the value being on the left, the function on the right The argument. The function. The function result. let doubleIt x = x * 2 3 |> doubleIt // Evaluates to 6
Reified.Result.ResultModuleFail-fast helpers over the standard F# Result type.
orElse: Result<'value,'error> -> Result<'value,'error> -> Result<'value,'error>Falls back to another result when the source result fails. let result = Error "boom" result |> Result.orElse (Ok 5) // Ok 5
OkRepresents an OK or a Successful result. The code succeeded with a value of 'T.
orElseWith computes the fallback from the error, and only on failure. Use it when the fallback is expensive, or when
which fallback to use depends on why the first attempt failed.
(Error AgeMissing: Result<int, SignupError>) |> Result.orElseWith (fun _ -> Ok 0)
// Ok 0
ErrorRepresents an Error or a Failure. The code failed with a value of 'TError representing what went wrong.
AgeMissingMicrosoft.FSharp.Core.FSharpResult`2Helper type for error handling without exceptions. Choices and Results
intAn abbreviation for the CLI type . Basic Types
03-result-handling_30-handling-errors.md_page.SignupError(|>): 'T1 -> ('T1 -> 'U) -> 'UApply a function to a value, the value being on the left, the function on the right The argument. The function. The function result. let doubleIt x = x * 2 3 |> doubleIt // Evaluates to 6
Reified.Result.ResultModuleFail-fast helpers over the standard F# Result type.
orElseWith: ('error -> Result<'value,'error>) -> Result<'value,'error> -> Result<'value,'error>Computes a fallback result from the source error when the result fails. The lazy counterpart to orElse, matching the Flow.orElseWith naming and shape: the fallback runs only on failure and can inspect the error that caused it. let result = Error "boom" result |> Result.orElseWith (fun error -> Ok (String.length error)) // Ok 4
OkRepresents an OK or a Successful result. The code succeeded with a value of 'T.
Both keep the error type the same. A recovery step that fails with a different error type is not covered here —
convert with mapError first so both sides agree.
Choosing between them
| Situation | Function |
|---|---|
| the error carries something worth keeping | mapError |
the error is unit or an internal detail |
orError |
| a fallback value is already to hand | orElse |
| the fallback is expensive or depends on the error | orElseWith |