123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* Copyright (c) 2020 Nomadic Labs <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)(** Categories of error *)typeerror_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 *)]letstring_of_category=function|`Permanent->"permanent"|`Temporary->"temporary"|`Branch->"branch"letcombine_categoryc1c2=match(c1,c2)with|(`Permanent,_)|(_,`Permanent)->`Permanent|(`Branch,_)|(_,`Branch)->`Branch|(`Temporary,`Temporary)->`TemporarymoduletypePREFIX=sig(** The identifier for parts of the code that need their own error monad. It
is expected (but not enforced) that the identifier:
is printable and easy to read once printed, and
ends with a separator (typically a dot or a dash). *)valid:stringendmoduletypeCORE=sigtypeerrorvalerror_encoding:errorData_encoding.tvalpp:Format.formatter->error->unitendmoduletypeEXT=sigtypeerror=..(** 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
[encoder] [decoder] data encoding for this error. If the error has no value, specify Data_encoding.empty
*)valregister_error_kind:error_category->id:string->title:string->description:string->?pp:(Format.formatter->'err->unit)->'errData_encoding.t->(error->'erroption)->('err->error)->unit(** Same as [register_error_kind] but allow errors to wrap other errors.
The encoding argument is a function which will be given the encoding of
errors as argument so that you can encode errors in errors using a fixpoint.
Another difference with [register_error_kind] is that [pp] is mandatory. *)valregister_recursive_error_kind:error_category->id:string->title:string->description:string->pp:(Format.formatter->'err->unit)->(errorData_encoding.t->'errData_encoding.t)->(error->'erroption)->('err->error)->unit(** Classify an error using the registered kinds *)valclassify_error:error->error_category(** Catch all error when 'serializing' an error. *)typeerror+=private|Unclassifiedofstring(** Catch all error when 'deserializing' an error. *)typeerror+=privateUnregistered_errorofData_encoding.json(** An error serializer *)valjson_of_error:error->Data_encoding.jsonvalerror_of_json:Data_encoding.json->error(** {2 Error documentation} *)(** Error information *)typeerror_info={category:error_category;id:string;title:string;description:string;schema:Data_encoding.json_schema;}valpp_info:Format.formatter->error_info->unit(**
[find_info_of_error e] retrieves the `error_info` associated with the
given error `e`.
@raise [Invalid_argument] if the error is a wrapped error from another monad
@raise [Not_found] if the error's constructor has not been registered
*)valfind_info_of_error:error->error_info(** Retrieves information of registered errors *)valget_registered_errors:unit->error_infolistendmoduletypeWITH_WRAPPED=sigtypeerrormoduletypeWrapped_error_monad=sig(**
The purpose of this module is to wrap a specific error monad [E]
into a more general error monad [Eg].
The user implementing such an interface is responsible to
maintain the following assertions
- The [Eg] error is extended locally with a specific constructor [C]
- [unwrapped] is equal to the [error] type of [E]
- [wrap] builds an [Eg] [error] value from an [E] [error] value
- [unwrap] matches on [Eg] error cases and extracts [E]
error value from [C]
As a reference implementation,
see src/lib_protocol_environment/environment_V3.ml
*)typeunwrapped=..includeCOREwithtypeerror:=unwrappedincludeEXTwithtypeerror:=unwrapped(** [unwrap e] returns [Some] when [e] matches variant constructor [C]
and [None] otherwise *)valunwrap:error->unwrappedoption(** [wrap e] returns a general [error] from a specific [unwrapped] error
[e] *)valwrap:unwrapped->errorend(** 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].
*)valregister_wrapped_error_kind:(moduleWrapped_error_monad)->id:string->title:string->description:string->unitendmoduletypeTRACE=sig(** [trace] is abstract in this interface but it is made concrete in the
instantiated error monad (see [error_monad.mli]).
The idea of abstracting the trace is so that it can evolve more easily.
Eventually, we can make the trace abstract in the instantiated error
monad, we can have different notions of traces for the protocol and the
shell, etc. *)type'errtrace(** [make e] makes a singleton trace, the simplest of traces that carries a
single error. *)valmake:'error->'errortrace(** [cons e t] (construct sequential) constructs a sequential trace. This is
for tracing events/failures/things that happen one after the other,
generally one as a consequence of the other. E.g.,
[let file_handle =
match attempt_open name with
| Ok handle -> Ok handle
| Error error ->
let trace = make error in
match attempt_create name with
| Ok handle -> Ok handle
| Error error -> Error (cons error trace)
]
When you are within the error monad itself, you should build traces using
the [record_trace], [trace], [record_trace_eval] and [trace_eval]
functions directly. You should rarely need to build traces manually using
[cons]. This here function can be useful in the case where you are at the
interface of the error monad. *)valcons:'error->'errortrace->'errortrace(** [cons_list error errors] is the sequential composition of all the errors
passed as parameters. It is equivalent to folding [cons] over
[List.rev error :: errors] but more efficient.
Note that [error] and [errors] are separated as parameters to enforce that
empty traces cannot be constructed. The recommended use is:
{[
match all_errors with
| [] -> Ok () (* or something else depending on the context *)
| error :: errors -> Error (cons_list error errors)
]}
When you are within the error monad itself, you should build traces using
the [record_trace], [trace], [record_trace_eval] and [trace_eval]
functions directly. You should rarely need to build traces manually using
[cons_list]. This here function can be useful in the case where you are at
the interface of the error monad. *)valcons_list:'error->'errorlist->'errortrace(** [conp t1 t2] (construct parallel) construct a parallel trace. This is for
tracing events/failure/things that happen concurrently, in parallel, or
simply independently of each other. E.g.,
[let fetch_density () =
let area = fetch_area () in
let population = fetch_population () in
match area, population with
| Ok area, Ok population -> Ok (population / area)
| Error trace, Ok _ | Ok _, Error trace -> Error trace
| Error trace1, Error trace2 -> Error (conp trace1 trace2)
]
When you are within the error monad itself, you should rarely need to
build traces manually using [conp]. The result-concurrent traversors will
return parallel traces when appropriate, and so will [join_e], [join_ep],
[both_e], [both_ep], [all_e] and [all_ep]. *)valconp:'errortrace->'errortrace->'errortrace(** [conp_list trace traces] is the parallel composition of all the traces
passed as parameters. It is equivalent to [List.fold_left conp trace traces]
but more efficient.
Note that [trace] and [traces] are separated as parameters to enforce that
empty traces cannot be constructed. The recommended use is:
{[
match all_traces with
| [] -> Ok () (* or something else depending on the context *)
| trace :: traces -> Error (conp_list trace traces)
]}
When you are within the error monad itself, you should rarely need to
build traces manually using [conp]. The result-concurrent traversors will
return parallel traces when appropriate, and so will [join_e], [join_ep],
[both_e], [both_ep], [all_e] and [all_ep]. *)valconp_list:'errtrace->'errtracelist->'errtrace(** [pp_print] pretty-prints a trace of errors *)valpp_print:(Format.formatter->'err->unit)->Format.formatter->'errtrace->unit(** [pp_print_top] pretty-prints the top errors of the trace *)valpp_print_top:(Format.formatter->'err->unit)->Format.formatter->'errtrace->unitvalencoding:'errorData_encoding.t->'errortraceData_encoding.t(** [fold f init trace] traverses the trace (in an unspecified manner) so that
[init] is folded over each of the error within [trace] by [f]. Typical use
is to find the worst error, to check for the presence of a given error,
etc. *)valfold:('a->'error->'a)->'a->'errortrace->'aendmoduletypeMONAD=sig(** To be subsituted/constrained *)type'errtrace(** Successful result *)valok:'a->('a,'trace)resultvalok_unit:(unit,'trace)resultvalok_none:('aoption,'trace)resultvalok_some:'a->('aoption,'trace)resultvalok_nil:('alist,'trace)resultvalok_true:(bool,'trace)resultvalok_false:(bool,'trace)result(** Successful return *)valreturn:'a->('a,'trace)resultLwt.t(** Successful return of [()] *)valreturn_unit:(unit,'trace)resultLwt.t(** Successful return of [None] *)valreturn_none:('aoption,'trace)resultLwt.t(** [return_some x] is a successful return of [Some x] *)valreturn_some:'a->('aoption,'trace)resultLwt.t(** Successful return of [[]] *)valreturn_nil:('alist,'trace)resultLwt.t(** Successful return of [true] *)valreturn_true:(bool,'trace)resultLwt.t(** Successful return of [false] *)valreturn_false:(bool,'trace)resultLwt.t(** Erroneous result *)valerror:'err->('a,'errtrace)result(** Erroneous return *)valfail:'err->('a,'errtrace)resultLwt.t(** Infix operators for monadic binds/maps. All operators follow this naming
convention:
- the first character is [>]
- the second character is [>] for [bind] and [|] for [map]
- the next character is [=] for Lwt or [?] for Error
- the next character (if present) is [=] for Lwt or [?] for Error, it is
only used for operator that are within both monads.
*)(** Lwt's bind reexported. Following Lwt's convention, in this operator and
the ones below, [=] indicate we operate within Lwt. *)val(>>=):'aLwt.t->('a->'bLwt.t)->'bLwt.t(** Lwt's map reexported. The [|] indicates a map rather than a bind. *)val(>|=):'aLwt.t->('a->'b)->'bLwt.t(** Non-Lwt bind operator. In this operator and the ones below, [?] indicates
that we operate within the error monad. *)val(>>?):('a,'trace)result->('a->('b,'trace)result)->('b,'trace)result(** Non-Lwt map operator. *)val(>|?):('a,'trace)result->('a->'b)->('b,'trace)result(** Combined bind operator. The [=?] indicates that the operator acts within
the combined error-lwt monad. *)val(>>=?):('a,'trace)resultLwt.t->('a->('b,'trace)resultLwt.t)->('b,'trace)resultLwt.t(** Combined map operator. *)val(>|=?):('a,'trace)resultLwt.t->('a->'b)->('b,'trace)resultLwt.t(** Injecting bind operator. This is for transitioning from the simple Error
monad to the combined Error-Lwt monad.
Note the order of the character: it starts with the error monad marker [?]
and has the Lwt monad marker later. This hints at the role of the operator
to transition into Lwt. *)val(>>?=):('a,'trace)result->('a->('b,'trace)resultLwt.t)->('b,'trace)resultLwt.t(** Injecting map operator. *)val(>|?=):('a,'trace)result->('a->'bLwt.t)->('b,'trace)resultLwt.t(** Enrich an error report (or do nothing on a successful result) manually *)valrecord_trace:'err->('a,'errtrace)result->('a,'errtrace)result(** Automatically enrich error reporting on stack rewind *)valtrace:'err->('b,'errtrace)resultLwt.t->('b,'errtrace)resultLwt.t(** Same as record_trace, for unevaluated error *)valrecord_trace_eval:(unit->('err,'errtrace)result)->('a,'errtrace)result->('a,'errtrace)result(** Same as trace, for unevaluated Lwt error *)valtrace_eval:(unit->('err,'errtrace)resultLwt.t)->('b,'errtrace)resultLwt.t->('b,'errtrace)resultLwt.t(** Error on failed assertion *)valerror_unless:bool->'err->(unit,'errtrace)resultvalerror_when:bool->'err->(unit,'errtrace)result(** Erroneous return on failed assertion *)valfail_unless:bool->'err->(unit,'errtrace)resultLwt.tvalfail_when:bool->'err->(unit,'errtrace)resultLwt.tvalunless:bool->(unit->(unit,'trace)resultLwt.t)->(unit,'trace)resultLwt.tvalwhen_:bool->(unit->(unit,'trace)resultLwt.t)->(unit,'trace)resultLwt.t(** Wrapper around [Lwt_utils.dont_wait] *)valdont_wait:(exn->unit)->('trace->unit)->(unit->(unit,'trace)resultLwt.t)->unit(** A few aliases for Lwt functions *)valjoin_p:unitLwt.tlist->unitLwt.tvalall_p:'aLwt.tlist->'alistLwt.tvalboth_p:'aLwt.t->'bLwt.t->('a*'b)Lwt.t(** Similar functions in the error monad *)valjoin_e:(unit,'errtrace)resultlist->(unit,'errtrace)resultvalall_e:('a,'errtrace)resultlist->('alist,'errtrace)resultvalboth_e:('a,'errtrace)result->('b,'errtrace)result->('a*'b,'errtrace)result(** Similar functions in the combined monad *)valjoin_ep:(unit,'errtrace)resultLwt.tlist->(unit,'errtrace)resultLwt.tvalall_ep:('a,'errtrace)resultLwt.tlist->('alist,'errtrace)resultLwt.tvalboth_ep:('a,'errtrace)resultLwt.t->('b,'errtrace)resultLwt.t->('a*'b,'errtrace)resultLwt.tendmoduletypeMONAD_EXT=sig(** for substitution *)typeerrortype'errortracetypetztrace=errortracetype'atzresult=('a,tztrace)resultvalclassify_errors:tztrace->error_category(* This is for legacy, for backwards compatibility, there are old names *)(* NOTE: Right now we leave this [pp_print_error] named as is. Later on we
might rename it to [pp_print_trace]. *)valpp_print_error:Format.formatter->errortrace->unit(** Pretty prints a trace as the message of its first error *)valpp_print_error_first:Format.formatter->errortrace->unitvaltrace_encoding:errortraceData_encoding.t(** A serializer for result of a given type *)valresult_encoding:'aData_encoding.t->'atzresultData_encoding.tend