catch

JobResult.catch

Namespace: FsToolkit.ErrorHandling

Function Signature:

(exn -> 'b) -> Job<Result<'a, 'b>> -> Job<Result<'a, 'b>>

Catches exceptions that occur during the execution of a Job<Result<'a, 'b>> and maps them to the Error case using the provided function.

Examples

Example 1

let riskyJob : Job<Result<int, string>> =
    job { return failwith "unexpected failure" }

riskyJob
|> JobResult.catch (fun ex -> ex.Message)
// job { return Error "unexpected failure" }

Example 2

let safeDivide (x: int) (y: int) : Job<Result<int, string>> =
    job { return Ok (x / y) }

safeDivide 10 0
|> JobResult.catch (fun ex -> sprintf "Division error: %s" ex.Message)
// job { return Error "Division error: Attempted to divide by zero." }

Example 3

Last updated