FsToolkit.ErrorHandling 5.1.0

dotnet add package FsToolkit.ErrorHandling --version 5.1.0
                    
NuGet\Install-Package FsToolkit.ErrorHandling -Version 5.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FsToolkit.ErrorHandling" Version="5.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FsToolkit.ErrorHandling" Version="5.1.0" />
                    
Directory.Packages.props
<PackageReference Include="FsToolkit.ErrorHandling" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FsToolkit.ErrorHandling --version 5.1.0
                    
#r "nuget: FsToolkit.ErrorHandling, 5.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FsToolkit.ErrorHandling@5.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FsToolkit.ErrorHandling&version=5.1.0
                    
Install as a Cake Addin
#tool nuget:?package=FsToolkit.ErrorHandling&version=5.1.0
                    
Install as a Cake Tool

FsToolkit.ErrorHandling

FsToolkit.ErrorHandling is a utility library to work with the Result type in F#, and allows you to do clear, simple and powerful error handling.

The library provides utility functions like map, bind, apply, traverse, sequence as well as computation expressions and infix operators to work with Result<'a, 'b>, Result<'a option, 'b>, Async<Result<'a, 'b>>, Async<Result<'a option, 'b>>, and Result<'a, 'b list>.

It was inspired by Chessie and Cvdm.ErrorHandling (the latter has now been merged into FsToolkit.ErrorHandling).

FsToolkit.ErrorHandling targets .NET Standard 2.0, .NET Standard2.1 and supports Fable.

Documentation

The documentation is available here.

Further material

Builds

GitHub Actions
GitHub Actions
Build History

NuGet

Package name Release Prelease
FsToolkit.ErrorHandling NuGet NuGet
FsToolkit.ErrorHandling.TaskResult NuGet NuGet
FsToolkit.ErrorHandling.JobResult NuGet NuGet
FsToolkit.ErrorHandling.AsyncSeq NuGet NuGet
FsToolkit.ErrorHandling.IcedTasks NuGet NuGet

Developing locally

Devcontainer

This repository has a devcontainer setup for VSCode. For more information see:

Local Setup

Optional

To test fable builds locally you will need:

  • Node
    • v18.0.0 or Higher
    • Not required but recommend that you use NVM to easily manage multiple versions of Node
  • Python
    • v3.10.0 or higher
    • Required for Fable-Python
Compiling
> build.cmd <optional buildtarget> // on windows
$ ./build.sh  <optional buildtarget>// on unix

Without specifying a build target, the default target is DotnetPack, which will run tests for all projects on dotnet and then pack the projects into nuget packages. For additional notable targets see below.

Build Targets
  • Clean - Will clean all projects bin and obj folders
  • DotnetTest - Will run tests for dotnet projects
  • NpmTest - Will run tests for fable-javascript projects
  • PythonTest - Will run tests for fable-python projects
  • RunTests - Will run tests for dotnet, fable-javascript and fable-python projects
  • FormatCode - Will run fantomas to format the codebase

This is not an exhaustive list. Additional targets can be found in the ./build/build.fs file.

A motivating example

This example of composing a login flow shows one example of how this library can aid in clear, simple, and powerful error handling, using just a computation expression and a few helper functions. (The library has many more helper functions and computation expressions as well as infix operators; see the documentation for details.)

// Given the following functions:
//   tryGetUser: string -> Async<User option>
//   isPwdValid: string -> User -> bool
//   authorize: User -> Async<Result<unit, AuthError>>
//   createAuthToken: User -> Result<AuthToken, TokenError>

type LoginError = InvalidUser | InvalidPwd | Unauthorized of AuthError | TokenErr of TokenError

let login (username: string) (password: string) : Async<Result<AuthToken, LoginError>> =
  asyncResult {
    // requireSome unwraps a Some value or gives the specified error if None
    let! user = username |> tryGetUser |> AsyncResult.requireSome InvalidUser

    // requireTrue gives the specified error if false
    do! user |> isPwdValid password |> Result.requireTrue InvalidPwd

    // Error value is wrapped/transformed (Unauthorized has signature AuthError -> LoginError)
    do! user |> authorize |> AsyncResult.mapError Unauthorized

    // Same as above, but synchronous, so we use the built-in mapError
    return! user |> createAuthToken |> Result.mapError TokenErr
  }

Sponsor(s)

<a href="https://www.ajira.tech"><img src="./Ajira-logo.png" alt="Ajira Technologies, India" width="200" /></a>

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (47)

Showing the top 5 NuGet packages that depend on FsToolkit.ErrorHandling:

Package Downloads
FsToolkit.ErrorHandling.TaskResult

FsToolkit.ErrorHandling is an extensive utility library based around the F# Result type, enabling consistent and powerful error handling.

FsToolkit.ErrorHandling.IcedTasks

FsToolkit.ErrorHandling is an extensive utility library based around the F# Result type, enabling consistent and powerful error handling.

Oryx

.NET SDK for writing HTTP clients

FsToolkit.ErrorHandling.AsyncSeq

FsToolkit.ErrorHandling is an extensive utility library based around the F# Result type, enabling consistent and powerful error handling.

FSharp.Data.GraphQL.Server

FSharp implementation of Facebook GraphQL query language (Server)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.1.0 51,375 9/26/2025
5.1.0-beta001 261 9/26/2025
5.0.1 41,817 7/12/2025
5.0.0 23,871 6/2/2025
5.0.0-beta014 354 6/2/2025
5.0.0-beta013 541 5/29/2025
5.0.0-beta012 2,214 5/1/2025
5.0.0-beta011 689 3/30/2025
5.0.0-beta010 383 3/21/2025
5.0.0-beta009 1,947 3/5/2025
5.0.0-beta008 573 3/2/2025
5.0.0-beta007 1,765 2/23/2025
5.0.0-beta006 308 2/21/2025
5.0.0-beta005 300 2/16/2025
5.0.0-beta004 500 2/4/2025
5.0.0-beta003 571 1/6/2025
5.0.0-beta002 342 12/21/2024
5.0.0-beta001 5,075 12/7/2024
4.18.0 909,694 10/23/2024
4.18.0-beta001 335 10/23/2024
4.17.0 233,653 9/23/2024
4.17.0-beta002 342 9/23/2024
4.17.0-beta001 350 9/23/2024
4.16.0 122,568 7/15/2024
4.15.3 5,510 7/14/2024
4.15.3-beta001 376 7/14/2024
4.15.2 217,456 5/2/2024
4.15.2-beta001 416 5/2/2024
4.15.1 216,783 1/15/2024
4.15.0 58,568 1/10/2024
4.14.0 5,578 1/2/2024
4.13.0 16,058 12/10/2023
4.13.0-beta002 1,419 12/10/2023
4.13.0-beta001 1,397 12/10/2023
4.12.0 13,502 11/27/2023
4.11.1 5,254 11/23/2023
4.11.0 34,280 11/11/2023
4.10.0 26,184 10/16/2023
4.9.0 603,553 7/10/2023
4.8.0 3,662 7/7/2023
4.7.0 20,046 6/21/2023
4.7.0-beta001 1,938 6/21/2023
4.6.0 133,906 4/20/2023
4.6.0-beta001 2,049 4/20/2023
4.5.0 35,380 3/27/2023
4.5.0-beta002 2,035 3/19/2023
4.5.0-beta001 3,475 3/6/2023
4.4.0 198,910 2/21/2023
4.3.0 55,945 1/20/2023
4.2.1 23,871 12/19/2022
4.2.0 2,912 12/17/2022
4.1.0 10,921 12/13/2022
4.1.0-beta001 1,955 12/13/2022
4.0.0 103,620 11/19/2022
4.0.0-beta001 1,937 11/19/2022
3.3.1 70,813 11/19/2022
3.3.1-beta004 2,039 11/19/2022
3.3.1-beta003 1,918 11/18/2022
3.3.1-beta002 2,009 11/18/2022
3.3.1-beta001 1,902 11/18/2022
3.3.0-beta001 1,951 11/9/2022
3.2.0 139,804 10/31/2022
3.2.0-beta001 1,811 10/31/2022
3.1.0 27,999 10/19/2022
3.0.1 3,931 10/18/2022
3.0.0 9,284 10/14/2022
3.0.0-beta008 2,014 10/14/2022
3.0.0-beta007 34,125 4/15/2022
3.0.0-beta006 2,089 4/5/2022
3.0.0-beta005 2,162 4/5/2022
3.0.0-beta004 2,110 4/5/2022
3.0.0-beta003 2,045 3/30/2022
3.0.0-beta002 2,721 2/21/2022
3.0.0-beta001 2,015 2/21/2022
2.13.0 1,777,605 1/11/2022
2.13.0-beta001 1,992 1/11/2022
2.12.0 4,438 1/6/2022
2.12.0-beta001 1,992 1/6/2022
2.11.1 41,237 12/1/2021
2.11.1-beta001 2,581 12/1/2021
2.11.0 9,751 11/24/2021
2.11.0-beta004 5,072 11/24/2021
2.11.0-beta002 4,986 11/24/2021
2.11.0-beta001 4,900 11/24/2021
2.10.0 37,809 11/13/2021
2.9.0 8,309 11/10/2021
2.9.0-beta001 2,054 11/10/2021
2.8.1 9,369 11/7/2021
2.8.0 2,600 11/7/2021
2.8.0-beta001 2,232 11/7/2021
2.7.1 11,080 10/19/2021
2.7.1-beta001 2,122 10/19/2021
2.7.0 235,924 8/2/2021
2.7.0-beta001 2,236 8/2/2021
2.6.0 50,392 7/7/2021
2.6.0-beta001 2,213 7/7/2021
2.5.0 65,041 5/26/2021
2.5.0-beta001 2,293 5/26/2021
2.4.0 10,410 5/23/2021
2.4.0-beta001 2,163 5/23/2021
2.3.0 21,542 5/14/2021
2.3.0-beta002 2,081 5/14/2021
2.2.0 15,569 4/21/2021
2.1.2 92,157 2/27/2021
2.1.1 3,559 2/26/2021
2.1.1-beta001 2,236 2/26/2021
2.1.0 2,804 2/26/2021
2.1.0-beta003 2,317 2/26/2021
2.1.0-beta002 2,118 2/25/2021
2.1.0-beta001 2,264 2/25/2021
2.0.0 702,435 11/20/2020
2.0.0-beta002 2,498 11/18/2020
2.0.0-beta001 2,902 8/29/2020
1.4.3 276,785 7/22/2020
1.4.0 10,879 6/5/2020
1.3.2 3,072 6/5/2020
1.3.1 5,107 5/29/2020
1.3.1-beta001 2,523 5/25/2020
1.3.0 7,588 5/25/2020
1.3.0-beta006 2,473 5/25/2020
1.3.0-beta005 2,561 5/25/2020
1.3.0-beta004 2,594 5/12/2020
1.2.6 39,958 2/15/2020
1.2.5 31,315 10/18/2019
1.2.4 3,514 10/10/2019
1.2.3 127,929 7/22/2019
1.2.2 7,264 5/31/2019
1.2.1 29,253 5/22/2019
1.1.1 3,492 5/9/2019
1.1.0 3,233 5/7/2019
1.0.0 5,692 4/10/2019
0.0.14 3,093 1/27/2019
0.0.13 1,745 1/21/2019
0.0.12 2,063 1/4/2019
0.0.11 1,806 1/3/2019
0.0.10 1,791 12/30/2018
0.0.9 2,003 10/5/2018
0.0.8 1,913 10/5/2018
0.0.7 1,931 10/5/2018
0.0.6 1,860 10/4/2018
0.0.5 1,843 9/16/2018
0.0.4 1,854 9/16/2018
0.0.3 1,929 9/8/2018
0.0.2 1,929 9/8/2018
0.0.1 2,939 9/8/2018

- [Adds getOrReraise and tests](https://github.com/demystifyfp/FsToolkit.ErrorHandling/pull/342) Credits @njlr