| 1 | (************************************************************************) |
| 2 | (* * The Rocq Prover / The Rocq Development Team *) |
| 3 | (* v * Copyright INRIA, CNRS and contributors *) |
| 4 | (* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 | (* \VV/ **************************************************************) |
| 6 | (* // * This file is distributed under the terms of the *) |
| 7 | (* * GNU Lesser General Public License Version 2.1 *) |
| 8 | (* * (see LICENSE file for the text of the license) *) |
| 9 | (************************************************************************) |
| 10 | |
| 11 | (** This modules implements basic manipulations of errors for use |
| 12 | throughout Rocq's code. *) |
| 13 | |
| 14 | (** {6 Generic errors.} |
| 15 | |
| 16 | [Anomaly] is used for system errors and [UserError] for the |
| 17 | user's ones. *) |
| 18 | |
| 19 | val anomaly : ?loc:Loc.t -> ?info:Exninfo.info -> ?label:string -> Pp.t -> 'a |
| 20 | (** Raise an anomaly, with an optional location and an optional |
| 21 | label identifying the anomaly. *) |
| 22 | |
| 23 | val exit_code : exn -> int |
| 24 | (** Return [1] for most exceptions and [129] for exceptions which |
| 25 | would print the "Please report" message. *) |
| 26 | |
| 27 | val is_async : exn -> bool |
| 28 | (** Whether the exception may have been raised asynchronously |
| 29 | (Timeout, stack overflow, etc). Avoid catching such exceptions at all costs! *) |
| 30 | |
| 31 | val is_sync_anomaly : exn -> bool |
| 32 | (** Check whether a given exception is an anomaly and not asynchronous. |
| 33 | This is mostly provided for compatibility. Please avoid doing specific |
| 34 | tricks with anomalies thanks to it. See rather [noncritical] below. *) |
| 35 | |
| 36 | exception UserError of Pp.t |
| 37 | (** Main error signaling exception. It carries a header plus a pretty printing |
| 38 | doc *) |
| 39 | |
| 40 | val user_err : ?loc:Loc.t -> ?info:Exninfo.info -> Pp.t -> 'a |
| 41 | (** Main error raising primitive. [user_err ?loc pp] signals an |
| 42 | error [pp] with optional header and location [loc] *) |
| 43 | |
| 44 | (** [register_handler h] registers [h] as a handler. |
| 45 | When an expression is printed with [print e], it |
| 46 | goes through all registered handles (the most |
| 47 | recent first) until a handle deals with it. |
| 48 | |
| 49 | Handles signal that they don't deal with some exception |
| 50 | by returning None. Raising any other exception is |
| 51 | forbidden and will result in an anomaly. |
| 52 | |
| 53 | Exceptions that are considered anomalies should not be |
| 54 | handled by registered handlers. |
| 55 | *) |
| 56 | |
| 57 | val register_handler : (exn -> Pp.t option) -> unit |
| 58 | |
| 59 | (** The standard exception printer *) |
| 60 | val print : exn -> Pp.t |
| 61 | val iprint : Exninfo.iexn -> Pp.t |
| 62 | |
| 63 | (** Same as [print], except that the "Please report" part of an anomaly |
| 64 | isn't printed (used in Ltac debugging). *) |
| 65 | val print_no_report : exn -> Pp.t |
| 66 | val iprint_no_report : Exninfo.iexn -> Pp.t |
| 67 | |
| 68 | (** "Critical" exceptions, such as anomalies or interruptions should |
| 69 | not be caught and ignored by mistake by inner Rocq functions by |
| 70 | means of doing a "catch-all". They should be handled instead by |
| 71 | the compiler layer which is in charge of coordinating the |
| 72 | intepretation of Rocq vernaculars. |
| 73 | |
| 74 | Please, avoid exceptions catch-all! If you must do so, then use the form: |
| 75 | {[ |
| 76 | try my_comp () |
| 77 | with exn when noncritical exn -> |
| 78 | my_handler |
| 79 | ]} |
| 80 | |
| 81 | If you need to re-raise the excepction, you must work to preserve |
| 82 | the backtrace and other important information: |
| 83 | {[ |
| 84 | try my_comp () |
| 85 | with exn when noncritical exn -> |
| 86 | let iexn = Exninfo.capture exn in |
| 87 | ... |
| 88 | Exninfo.iraise iexn |
| 89 | ]} |
| 90 | *) |
| 91 | val noncritical : exn -> bool |
| 92 | |
| 93 | (** Register a printer for errors carrying additional information on |
| 94 | exceptions. This method is fragile and should be considered |
| 95 | deprecated *) |
| 96 | val register_additional_error_info |
| 97 | : (Exninfo.info -> Pp.t option) |
| 98 | -> unit |
| 99 | |
| 100 | (** Print using the printers given to [register_additional_error_info]. *) |
| 101 | val print_extra : Exninfo.info -> Pp.t |
| 102 | |
| 103 | (** [to_result ~f x] reifies (non-critical) exceptions into a [('a, |
| 104 | iexn) Result.t] type *) |
| 105 | val to_result : f:('a -> 'b) -> 'a -> ('b, Exninfo.iexn) Result.t |