catch

TaskResult.catch

Namespace: FsToolkit.ErrorHandling

Catches exceptions and maps them to the Error case using the provided function.

Function Signature:

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

Examples

Given the function:

let taskThrow () =
  task {
    failwith "an exception happened"
    return Error ""
  }

Example 1

let result : Task<Result<int, string>> = TaskResult.catch (_.Message) (TaskResult.ok 42)
// task { Ok 42 }

Example 2

let result : Task<Result<int, string>> = TaskResult.catch (_.Message) (TaskResult.error "something bad happened")
// task { Error "something bad happened" }

Example 3

let result : Task<Result<int, string>> = TaskResult.catch (_.Message) (taskThrow ())
// task { Error "an exception happened" }

Last updated