Legend:
Library
Module
Module type
Parameter
Class
Class type
Tezos Protocol Implementation - Error Monad
type error_category = [
| `Branch
(*
Errors that may not happen in another context
*)
| `Temporary
(*
Errors that may not happen in a later context
*)
| `Permanent
(*
Errors that will happen no matter the context
*)
]
Categories of error
Note: this is only meaningful within the protocol. It may be removed from the error monad and pushed to the protocol environment in the future. See https://gitlab.com/tezos/tezos/-/issues/1576
Assembling the different components of the error monad.
Whenever you add a variant to this type (with type Error_monad.error += …) you must also register the error with register_error_kind.
These errors are not meant to be inspected in general. Meaning that they should not be matched upon. Consequently it is acceptable to register an error in an implementation file and not mention it in the corresponding interface file.
Note that this encoding has a few peculiarities, some of which may impact your code. These peculiarities are due to the type error being an extensible variant.
Because the error type is an extensible variant, you must register an encoding for each error constructor you add to error. This is done via register_error_kind.
Because the error type is an extensible variant, with dynamically registered errors (see peculiarity above), there are no tags associated with each error. This does not affect the JSON encoding, but it does impose restrictions on the binary encoding. The chosen workaround is to encode errors as JSON and to encode the JSON to binary form. As a result, errors can be somewhat large in binary: they include field names and such.
Because the error type is an extensible variant, with dynamically registered errors (see peculiarity above), the encoding must be recomputed when a new error is registered. This is achieved by the means of a Data_encoding.delayed combinator: the encoding is recomputed on-demand. There is a caching mechanism so that, in the case where no new errors have been registered since the last use, the last result is used.
This last peculiarity imposes a limit on the use of error_encoding itself. Specifically, it is invalid to use error_encoding inside the ~json argument of a Data_encoding.splitted. This is because splitted evaluates the delayed combinator once-and-for-all to produce a json encoding. (Note that the following data-encoding combinators use splitted internally: Data_encoding.Compact.make, Data_encoding.assoc, and Data_encoding.lazy_encoding. As a result, it is invalid to use error_encoding within the arguments of these combinators as well.)
The error data type is extensible. Each module can register specialized error serializers id unique name of this error. Ex.: overflow_time_counter title more readable name. Ex.: Overflow of time counter description human readable description. Ex.: The time counter overflowed while computing delta increase pp formatter used to pretty print additional arguments. Ex.: The time counter overflowed while computing delta increase. Previous value %d. Delta: %d encoderdecoder data encoding for this error. If the error has no value, specify Data_encoding.empty
if the error's constructor has not been registered
val get_registered_errors : unit ->error_info list
Retrieves information of registered errors
WITH_WRAPPED: wrapping of errors from other instantiations within this one. Specifically, this is used to wrap errors of the economic protocol (e.g., operation is invalid) within the errors of the shell (e.g., failed to validate protocol data).
Functions from this module should only be used within the environment.
The purpose of this module is to wrap a specific error monad E into a more general error monad Eg.
val register_wrapped_error_kind :
(moduleWrapped_error_monad)->id:string ->title:string ->description:string ->
unit
Same as register_error_kind but for a wrapped error monad. The codec is defined in the module parameter. It makes the category of the error Wrapped instead of Main.
TzTrace: trace module specific to the Tezos Error monad. The trace type of this module is meant to become abstract in the medium-term (see https://gitlab.com/tezos/tezos/-/issues/1577).
record_trace err res is either res if res is Ok _, or it is Error (Trace.cons err tr) if res is Error tr.
In other words, record_trace err res enriches the trace that is carried by res (if it is carrying a trace) with the error err. It leaves res untouched if res is not carrying a trace.
You can use this to add high-level information to potential low-level errors. E.g.,
Note that record_trace takes a fully evaluated error err as argument. It means that, whatever the value of the result res, the error err is evaluated. This is not an issue if the error is a simple expression (a literal or a constructor with simple parameters). However, for any expression that is more complex (e.g., one that calls a function) you should prefer record_trace_eval.
trace is identical to record_trace but applies to a promise. More formally, trace err p is a promise that resolves to Ok v if p resolves to Ok v, or it resolves to Error (Trace.cons err tr) if res resolves to Error tr.
In other words, trace err p enriches the trace that p resolves to (if it does resolve to a trace) with the error err. It leaves the value that p resolves to untouched if it is not a trace.
You can use this to add high-level information to potential low-level errors.
Note that, like record_trace, trace takes a fully evaluated error as argument. For a similar reason as explained there, you should only use trace with simple expressions (literal or constructor with simple parameters) and prefer trace_eval for any other expression (such as ones that include functions calls).
record_trace_eval is identical to record_trace except that the error that enriches the trace is wrapped in a function that is evaluated only if it is needed. More formally record_trace_eval mkerr res is res if res is Ok _, or it is Error (Trace.cons (mkerr ()) tr) if res is Error tr.
You can achieve the same effect by hand with
match res with
| Ok _ -> res
| Error tr -> Error (Trace.cons (mkerr ()) tr)
Prefer record_trace_eval over record_trace when the enriching error is expensive to compute or heavy to allocate.
trace_eval is identical to trace except that the error that enriches the trace is wrapped in a function that is evaluated only if and when it is needed. More formally trace_eval mkerr p is a promise that resolves to Ok v if p resolves to Ok v, or it resolves to Error (Trace.cons err tr) if p resolves to Error tr and then mkerr
() resolves to err.
You can achieve the same effect by hand with
p >>= function
| Ok _ -> p
| Error tr ->
mkerr () >>= fun err ->
Lwt.return (Error (Trace.cons err tr))
Note that the evaluation of the error can be arbitrarily delayed. Avoid using references and other mutable values in the function mkerr.
Prefer trace_eval over trace when the enriching error is expensive to compute or heavy to allocate or when evaluating it requires the use of Lwt.
unless b f is f () if b is false and it is a promise already resolved to Ok () otherwise.
You can use unless to avoid having to write an if statement that you then need to populate entirely to satisfy the type-checker. E.g, you can write unless b f instead of if not b then f () else return_unit.
when_ b f is f () if b is true and it is a promise already resolved to Ok () otherwise.
You can use when_ to avoid having to write an if statement that you then need to populate entirely to satisfy the type-checker. E.g, you can write when_ b f instead of if b then f () else return_unit.
val dont_wait :
(unit ->(unit, 'trace)resultLwt.t)->('trace-> unit)->(exn -> unit)->
unit
This part of the interface groups functions that are used to interact with code that raises exceptions. Typically, you should be using these functions when calling into a library that raises exceptions.
Remember that the keyword error is for failure within the Result monad (or, more specifically, the TracedResult monad) whilst fail is for failure within the LwtResult monad (or, more specifically, the LwtTracedResult monad).
Failing: to error out and to fail
This sub-part of the interface groups functions that fail (either in the TracedResult monad or the LwtTracedResult monad) whilst carrying information provided as argument. When reading this sub-part you should read error and fail as verbs. E.g., error_with_exn errors out and carries a provided exception. The next sub-part will group noun-like, declarative functions.
error_with fmt … errors out: it fails within the TracedResult monad. The payload of the Error constructor is unspecified beyond the fact that it includes the string formatted by fmt …. E.g.,
if n < 0 then
error_with "Index (%d) is negative" n
else if n >= Array.length a then
error_with "Index (%d) is beyond maximum index (%d)" n (Array.length a - 1)
else
Ok a.(n)
Note: this is somewhat equivalent to Stdlib.failwith in that it is a generic failure mechanism with a simple error message. Like Stdlib.failwith it should be replaced by a more specific error mechanism in most cases.
failwith fmt … fails: it fails within the LwtTracedResult monad. The payload of the Error constructor is unspecified beyond the fact that it includes the string formatted by fmt …. E.g.,
match find key store with
| None ->
failwith "Key %a not found in store" pp_key key
| Some value ->
LwtResult.return value
Note: this is somewhat equivalent to Stdlib.failwith in that it is a generic failure mechanism with a simple error message. Like Stdlib.failwith it should be replaced by a more specific error mechanism in most cases.
error_with_exn exc errors out: it fails within the TracedResult monad. The payload of the Error constructor is unspecified but it includes the exception.
It is meant as a way to switch from exception-based error management to tzresult-based error management, e.g., when calling external libraries that use exceptions.
try Ok (parse_input s) with Lex_error | Parse_error as exc -> error_with_exn exc
Whilst it is useful in specific places, it is generally better to use a dedicated error.
fail_with_exn exc fails: it fails within the LwtTracedResult monad. The payload of the Error constructor is unspecified but it includes the info from the exception.
It is meant as a way to switch, inside of Lwt code, from exception-based error management to tzresult-based error management, e.g., when calling external libraries that use exceptions.
Whilst it is useful in specific places, it is generally better to use a dedicated error.
Conversions: an exception, an error, a trace, a result
This sub-part of the interface groups declarative functions that convert between different styles of error (exceptions, errors, traces, results). By themselves these functions have no effect within the Result or LwtResult monad, and they are generally used along with constructors or combinators.
error_of_exn e is an error that carries the exception e. This function is intended to be used when interacting with a part of the code (most likely an external library) which uses exceptions.
error_of_fmt … is like error_with … but the error isn't wrapped in a trace in a result. Instead, an error is returned and the caller is expected to pass it to whichever error-combinator is appropriate to the situation. E.g.,
protect is a wrapper around Lwt.catch where the error handler operates over trace instead of exn. Besides, protect ~on_error ~canceler ~f may *cancel* f via a Lwt_canceler.t.
More precisely, protect ~on_error ~canceler f runs f (). An Lwt failure triggered by f () is wrapped into an Exn. If a canceler is given and Lwt_canceler.cancellation canceler is determined before f (), a Canceled error is returned.
Errors are caught by ~on_error (if given), otherwise the previous value is returned. An Lwt failure triggered by ~on_error is wrapped into an Exn
val catch : ?catch_only:(exn -> bool)->(unit ->'a)->'atzresult
catch f executes f within a try-with block and wraps exceptions within a tzresult. catch f is equivalent to try Ok (f ()) with e -> Error (error_of_exn e).
If catch_only is set, then only exceptions e such that catch_only e is true are caught.
Whether catch_only is set or not, this function never catches non-deterministic runtime exceptions of OCaml such as Stack_overflow and Out_of_memory.
val catch_e : ?catch_only:(exn -> bool)->(unit ->'atzresult)->'atzresult
catch_e is like catch but when f returns a tzresult. I.e., catch_e f is equivalent to try f () with e -> Error (error_of_exn e).
catch_only has the same use as with catch. The same restriction on catching non-deterministic runtime exceptions applies.
val catch_f :
?catch_only:(exn -> bool)->(unit ->'a)->(exn ->error)->'atzresult
catch_f f handler is equivalent to map_error (catch f) handler. In other words, it catches exceptions in f () and either returns the value in an Ok or passes the exception to handler for the Error.
No attempt is made to catch the exceptions raised by handler.
catch_only has the same use as with catch. The same restriction on catching non-deterministic runtime exceptions applies.
val catch_s :
?catch_only:(exn -> bool)->(unit ->'aLwt.t)->'atzresultLwt.t
catch_s is like catch but when f returns a promise. It is equivalent to
Lwt.try_bind f
(fun v -> Lwt.return (Ok v))
(fun e -> Lwt.return (Error (error_of_exn e)))
catch_only has the same use as with catch. The same restriction on catching non-deterministic runtime exceptions applies.
catch_es is like catch_s but when f returns a promise of a tzresult. I.e., catch_es f is equivalent to Lwt.catch f (fun e -> Lwt.return_error (error_of_exn e)).
catch_only has the same use as with catch. The same restriction on catching non-deterministic runtime exceptions applies.