package octez-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file unix_error.ml

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2021 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Data_encoding
open Unix

let unix_error_to_tag = function
  | E2BIG -> 0
  | EACCES -> 1
  | EAGAIN -> 2
  | EBADF -> 3
  | EBUSY -> 4
  | ECHILD -> 5
  | EDEADLK -> 6
  | EDOM -> 7
  | EEXIST -> 8
  | EFAULT -> 9
  | EFBIG -> 10
  | EINTR -> 11
  | EINVAL -> 12
  | EIO -> 13
  | EISDIR -> 14
  | EMFILE -> 15
  | EMLINK -> 16
  | ENAMETOOLONG -> 17
  | ENFILE -> 18
  | ENODEV -> 19
  | ENOENT -> 20
  | ENOEXEC -> 21
  | ENOLCK -> 22
  | ENOMEM -> 23
  | ENOSPC -> 24
  | ENOSYS -> 25
  | ENOTDIR -> 26
  | ENOTEMPTY -> 27
  | ENOTTY -> 28
  | ENXIO -> 29
  | EPERM -> 30
  | EPIPE -> 31
  | ERANGE -> 32
  | EROFS -> 33
  | ESPIPE -> 34
  | ESRCH -> 35
  | EXDEV -> 36
  | EWOULDBLOCK -> 37
  | EINPROGRESS -> 38
  | EALREADY -> 39
  | ENOTSOCK -> 40
  | EDESTADDRREQ -> 41
  | EMSGSIZE -> 42
  | EPROTOTYPE -> 43
  | ENOPROTOOPT -> 44
  | EPROTONOSUPPORT -> 45
  | ESOCKTNOSUPPORT -> 46
  | EOPNOTSUPP -> 47
  | EPFNOSUPPORT -> 48
  | EAFNOSUPPORT -> 49
  | EADDRINUSE -> 50
  | EADDRNOTAVAIL -> 51
  | ENETDOWN -> 52
  | ENETUNREACH -> 53
  | ENETRESET -> 54
  | ECONNABORTED -> 55
  | ECONNRESET -> 56
  | ENOBUFS -> 57
  | EISCONN -> 58
  | ENOTCONN -> 59
  | ESHUTDOWN -> 60
  | ETOOMANYREFS -> 61
  | ETIMEDOUT -> 62
  | ECONNREFUSED -> 63
  | EHOSTDOWN -> 64
  | EHOSTUNREACH -> 65
  | ELOOP -> 66
  | EOVERFLOW -> 67
  | EUNKNOWNERR _ -> 68

let get_constructor_name = function
  | E2BIG -> "E2BIG"
  | EACCES -> "EACCES"
  | EAGAIN -> "EAGAIN"
  | EBADF -> "EBADF"
  | EBUSY -> "EBUSY"
  | ECHILD -> "ECHILD"
  | EDEADLK -> "EDEADLK"
  | EDOM -> "EDOM"
  | EEXIST -> "EEXIST"
  | EFAULT -> "EFAULT"
  | EFBIG -> "EFBIG"
  | EINTR -> "EINTR"
  | EINVAL -> "EINVAL"
  | EIO -> "EIO"
  | EISDIR -> "EISDIR"
  | EMFILE -> "EMFILE"
  | EMLINK -> "EMLINK"
  | ENAMETOOLONG -> "ENAMETOOLONG"
  | ENFILE -> "ENFILE"
  | ENODEV -> "ENODEV"
  | ENOENT -> "ENOENT"
  | ENOEXEC -> "ENOEXEC"
  | ENOLCK -> "ENOLCK"
  | ENOMEM -> "ENOMEM"
  | ENOSPC -> "ENOSPC"
  | ENOSYS -> "ENOSYS"
  | ENOTDIR -> "ENOTDIR"
  | ENOTEMPTY -> "ENOTEMPTY"
  | ENOTTY -> "ENOTTY"
  | ENXIO -> "ENXIO"
  | EPERM -> "EPERM"
  | EPIPE -> "EPIPE"
  | ERANGE -> "ERANGE"
  | EROFS -> "EROFS"
  | ESPIPE -> "ESPIPE"
  | ESRCH -> "ESRCH"
  | EXDEV -> "EXDEV"
  | EWOULDBLOCK -> "EWOULDBLOCK"
  | EINPROGRESS -> "EINPROGRESS"
  | EALREADY -> "EALREADY"
  | ENOTSOCK -> "ENOTSOCK"
  | EDESTADDRREQ -> "EDESTADDRREQ"
  | EMSGSIZE -> "EMSGSIZE"
  | EPROTOTYPE -> "EPROTOTYPE"
  | ENOPROTOOPT -> "ENOPROTOOPT"
  | EPROTONOSUPPORT -> "EPROTONOSUPPORT"
  | ESOCKTNOSUPPORT -> "ESOCKTNOSUPPORT"
  | EOPNOTSUPP -> "EOPNOTSUPP"
  | EPFNOSUPPORT -> "EPFNOSUPPORT"
  | EAFNOSUPPORT -> "EAFNOSUPPORT"
  | EADDRINUSE -> "EADDRINUSE"
  | EADDRNOTAVAIL -> "EADDRNOTAVAIL"
  | ENETDOWN -> "ENETDOWN"
  | ENETUNREACH -> "ENETUNREACH"
  | ENETRESET -> "ENETRESET"
  | ECONNABORTED -> "ECONNABORTED"
  | ECONNRESET -> "ECONNRESET"
  | ENOBUFS -> "ENOBUFS"
  | EISCONN -> "EISCONN"
  | ENOTCONN -> "ENOTCONN"
  | ESHUTDOWN -> "ESHUTDOWN"
  | ETOOMANYREFS -> "ETOOMANYREFS"
  | ETIMEDOUT -> "ETIMEDOUT"
  | ECONNREFUSED -> "ECONNREFUSED"
  | EHOSTDOWN -> "EHOSTDOWN"
  | EHOSTUNREACH -> "EHOSTUNREACH"
  | ELOOP -> "ELOOP"
  | EOVERFLOW -> "EOVERFLOW"
  | EUNKNOWNERR _ -> "EUNKNOWNERR"

(* This function is used to create an encoding for
   the majority of unix error cases, which could be
   encoded as constants.
   It should not be applied to eunknownerr which needs
   to also encode the error code *)
let unix_error_constant_encoding error =
  let error_name = get_constructor_name @@ error in
  obj1 @@ req "unix-error" (constant error_name)

let eunknownerr_encoding =
  let error_name = get_constructor_name @@ EUNKNOWNERR 0 in
  obj1 @@ req "unix-error" @@ obj1 @@ req error_name int31

let constant_case error =
  let title = get_constructor_name error in
  let tag = unix_error_to_tag error in
  case
    ~title
    (Tag tag)
    (unix_error_constant_encoding error)
    (fun x -> if x = error then Some () else None)
    (fun () -> error)

let encoding =
  matching
    (fun error ->
      let tag = unix_error_to_tag error in
      match error with
      | EUNKNOWNERR code -> matched tag eunknownerr_encoding code
      | _ -> matched tag (unix_error_constant_encoding error) ())
    [
      constant_case E2BIG;
      constant_case EACCES;
      constant_case EAGAIN;
      constant_case EBADF;
      constant_case EBUSY;
      constant_case ECHILD;
      constant_case EDEADLK;
      constant_case EDOM;
      constant_case EEXIST;
      constant_case EFAULT;
      constant_case EFBIG;
      constant_case EINTR;
      constant_case EINVAL;
      constant_case EIO;
      constant_case EISDIR;
      constant_case EMFILE;
      constant_case EMLINK;
      constant_case ENAMETOOLONG;
      constant_case ENFILE;
      constant_case ENODEV;
      constant_case ENOENT;
      constant_case ENOEXEC;
      constant_case ENOLCK;
      constant_case ENOMEM;
      constant_case ENOSPC;
      constant_case ENOSYS;
      constant_case ENOTDIR;
      constant_case ENOTEMPTY;
      constant_case ENOTTY;
      constant_case ENXIO;
      constant_case EPERM;
      constant_case EPIPE;
      constant_case ERANGE;
      constant_case EROFS;
      constant_case ESPIPE;
      constant_case ESRCH;
      constant_case EXDEV;
      constant_case EWOULDBLOCK;
      constant_case EINPROGRESS;
      constant_case EALREADY;
      constant_case ENOTSOCK;
      constant_case EDESTADDRREQ;
      constant_case EMSGSIZE;
      constant_case EPROTOTYPE;
      constant_case ENOPROTOOPT;
      constant_case EPROTONOSUPPORT;
      constant_case ESOCKTNOSUPPORT;
      constant_case EOPNOTSUPP;
      constant_case EPFNOSUPPORT;
      constant_case EAFNOSUPPORT;
      constant_case EADDRINUSE;
      constant_case EADDRNOTAVAIL;
      constant_case ENETDOWN;
      constant_case ENETUNREACH;
      constant_case ENETRESET;
      constant_case ECONNABORTED;
      constant_case ECONNRESET;
      constant_case ENOBUFS;
      constant_case EISCONN;
      constant_case ENOTCONN;
      constant_case ESHUTDOWN;
      constant_case ETOOMANYREFS;
      constant_case ETIMEDOUT;
      constant_case ECONNREFUSED;
      constant_case EHOSTDOWN;
      constant_case EHOSTUNREACH;
      constant_case ELOOP;
      constant_case EOVERFLOW;
      case
        ~title:(get_constructor_name @@ EUNKNOWNERR 0)
        (Tag (unix_error_to_tag @@ EUNKNOWNERR 0))
        eunknownerr_encoding
        (function EUNKNOWNERR e -> Some e | _ -> None)
        (fun e -> EUNKNOWNERR e);
    ]

module Internal_for_tests = struct
  let get_constructor_name = get_constructor_name
end
OCaml

Innovation. Community. Security.