The Error Monad¶
There are multiple ways to deal with errors in OCaml: exceptions,
result
, option
, etc., each approach has strengths and
weaknesses. However, in Mavkit we settle on one, single, uniform way of
dealing with errors: the error monad.
This is a tutorial for the error monad. It makes the following assumptions
You can read English.
You have a basic familiarity with OCaml.
You have a basic familiarity with Lwt (if not, this tutorial links to Lwt resources when it becomes necessary).
Other than that, each section of this tutorial can be understood using the information of previous sections, no additional information is necessary. Note, however, that most sections also make forward references along the lines of “more details on this later” or “alternatives are presented later”. During a first read, this foreshadowing is only useful in that it lets you know that the information is coming and that you can move on with a partial understanding. On subsequent reads, it helps putting things in context.
Note that the core of this tutorial focuses on error management in Mavkit. For a short section on error management in the protocol see Part 3 Working within the protocol.
- Part 1:
result
, Lwt, and Lwt-result
- The
result
type - The binding operator
- Aside: the
Error_monad
module is opened everywhere - Recovering from errors
- Mixing different kinds of errors
- The
Result_syntax
module - Lwt
- The
Lwt_syntax
module - Promises of results: Lwt and
result
together - The
Lwt_result_syntax
module - Converting errors of promises
- Lifting
- Wait! There is too much! What module am I supposed to open locally and what operators should I use?
- What’s an error?
- Wait! It was supposed to be “one single uniform way of dealing with errors”! What is this?
- META COMMENTARY
- The