1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
open Import
open Errors
(** Error manager for errors and exceptions defined in {!Errors} and
{!Io.S.misc_error} *)
module type S = sig
module Io : Io.S
type t = [ Base.t | `Io_misc of Io.misc_error ] [@@deriving irmin]
val raise_error : [< t ] -> 'a
val log_error : string -> [< t ] -> unit
val catch : (unit -> 'a) -> ('a, t) result
val raise_if_error : ('a, [< t ]) result -> 'a
val log_if_error : string -> ('a, [< t ]) result -> unit
end
module Make (Io : Io.S) : S with module Io = Io = struct
module Io = Io
type t =
[ `Double_close
| `File_exists of string
| `Invalid_parent_directory
| `No_such_file_or_directory
| `Not_a_file
| `Read_out_of_bounds
| `Invalid_argument
| `Decoding_error
| `Not_a_directory of string
| `Index_failure of string
| `Invalid_layout
| `Corrupted_legacy_file
| `Corrupted_mapping_file of string
| `Pending_flush
| `Rw_not_allowed
| `Migration_needed
| `Corrupted_control_file
| `Sys_error of string
| `V3_store_from_the_future
| `Gc_forbidden_during_batch
| `Unknown_major_pack_version of string
| `Only_minimal_indexing_strategy_allowed
| `Commit_key_is_dangling of string
| `Dangling_key of string
| `Gc_disallowed
| `Node_or_contents_key_is_indexed of string
| `Gc_process_error of string
| `Corrupted_gc_result_file of string
| `Gc_process_died_without_result_file of string
| `Gc_forbidden_on_32bit_platforms
| `Invalid_prefix_read of string
| `Invalid_mapping_read of string
| `Invalid_read_of_gced_object of string
| `Inconsistent_store
| `Closed
| `Ro_not_allowed
| `Io_misc of Io.misc_error
| `Split_forbidden_during_batch
| `Multiple_empty_chunks
| `Forbidden_during_gc ]
[@@deriving irmin]
let raise_error = function
| `Io_misc e -> Io.raise_misc_error e
| #error as e -> Base.raise_error e
let log_error context e =
[%log.err "%s failed: %a" context (Irmin.Type.pp t) (e :> t)]
let catch f =
match Base.catch (fun () -> Io.catch_misc_error f) with
| Ok (Ok v) -> Ok v
| Ok (Error e) -> Error (e :> t)
| Error e -> Error (e :> t)
let raise_if_error = function Ok x -> x | Error e -> raise_error e
let log_if_error context = function
| Ok _ -> ()
| Error e -> log_error context e
end