Skip to content

Commit ac7dd5a

Browse files
committed
more flexible api error
1 parent 5009e0b commit ac7dd5a

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

src/request/ezRequest_lwt.ml

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,21 @@ module Make(S : Interface) : S = struct
5454
let post = internal_post
5555

5656
module Raw = struct
57-
type nonrec 'e api_error = 'e EzReq_lwt_S.api_error =
58-
| KnownError of { code : int ; error : 'e }
59-
| UnknownError of { code : int ; msg : string option }
57+
type 'e api_error = 'e EzReq_lwt_S.api_error
6058

6159
let decode_result io err_encodings = function
62-
| Error (code, None) -> Error (UnknownError { code ; msg = None })
60+
| Error (code, None) -> Error (code, `unknown None)
6361
| Error (code, Some msg) ->
6462
(match err_encodings ~code with
65-
| None -> Error (UnknownError { code ; msg = Some msg })
63+
| None -> Error (code, `unknown (Some msg))
6664
| Some encoding ->
67-
try Error (
68-
KnownError { code ; error = EzEncoding.destruct encoding msg })
69-
with _ -> Error (UnknownError { code ; msg = Some msg })
65+
try Error (code, `known (EzEncoding.destruct encoding msg))
66+
with _ -> Error (code, `unknown (Some msg))
7067
)
7168
| Ok res ->
7269
match IO.from_string io (fun x -> x) res with
7370
| Ok s -> Ok s
74-
| Error (`destruct_exn exn) -> Error (UnknownError {
75-
code = -3;
76-
msg = Some (Printexc.to_string exn) })
71+
| Error (`destruct_exn exn) -> Error (-3, `unknown (Some (Printexc.to_string exn)))
7772

7873
let handle_result service res =
7974
let err_encodings = Service.error service.s in
@@ -141,14 +136,14 @@ module Make(S : Interface) : S = struct
141136
request ?headers ?params ?msg ?url_encode ~input api service ((Req.dummy, arg1), arg2)
142137

143138
let handle_error kn = function
144-
| KnownError {code; error} -> code, kn error
145-
| UnknownError {code; msg} -> code, msg
139+
| (code, `known error) -> code, kn error
140+
| (code, `unknown msg) -> code, msg
146141

147142
let string_of_error kn = function
148-
| KnownError {code; error} ->
143+
| (code, `known error) ->
149144
let content = match kn error with None -> "" | Some s -> ": " ^ s in
150145
Printf.sprintf "Error %d%s" code content
151-
| UnknownError {code; msg} ->
146+
| (code, `unknown msg) ->
152147
let content = match msg with None -> "" | Some s -> ": " ^ s in
153148
Printf.sprintf "Unknown Error %d%s" code content
154149
end
@@ -191,8 +186,8 @@ module Make(S : Interface) : S = struct
191186

192187
let unresultize = function
193188
| Ok res -> Ok res
194-
| Error UnknownError { code ; msg } -> Error (code, msg)
195-
| Error KnownError _ -> assert false (* Security.unreachable error *)
189+
| Error (code, `unknown msg) -> Error (code, msg)
190+
| Error (_code, `known _) -> assert false (* Security.unreachable error *)
196191

197192
let get0 ?post ?headers ?params ?msg
198193
api (service: 'output EzAPI.Legacy.service0) =

src/request/virtual/s/ezReq_lwt_S.mli

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
open EzAPI
22

3-
type 'e api_error =
4-
| KnownError of { code : int ; error : 'e }
5-
| UnknownError of { code : int ; msg : string option }
3+
type 'e api_error = int * [`known of 'e | `unknown of string option]
64

75
(* Note that `?content_type` in post can be overriden by a content-type
86
header in `?headers` *)

0 commit comments

Comments
 (0)