Skip to content

Commit 0c486c9

Browse files
committed
more flexible api error
1 parent 6ffd13a commit 0c486c9

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

src/request/ezRequest_lwt.ml

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,16 @@ module Make(S : Interface) : S = struct
6464
let post = internal_post
6565

6666
module Raw = struct
67-
type nonrec 'e api_error = 'e EzReq_lwt_S.api_error =
68-
| KnownError of { code : int ; error : 'e }
69-
| UnknownError of { code : int ; msg : string option }
67+
type 'e api_error = 'e EzReq_lwt_S.api_error
7068

7169
let decode_result io err_encodings = function
72-
| Error (code, None) -> Error (UnknownError { code ; msg = None })
70+
| Error (code, None) -> Error (code, `unknown None)
7371
| Error (code, Some msg) ->
7472
(match err_encodings ~code with
75-
| None -> Error (UnknownError { code ; msg = Some msg })
73+
| None -> Error (code, `unknown (Some msg))
7674
| Some encoding ->
77-
try Error (
78-
KnownError { code ; error = EzEncoding.destruct encoding msg })
79-
with _ -> Error (UnknownError { code ; msg = Some msg })
75+
try Error (code, `known (EzEncoding.destruct encoding msg))
76+
with _ -> Error (code, `unknown (Some msg))
8077
)
8178
| Ok res ->
8279
match IO.from_string io (fun x -> x) res with
@@ -87,9 +84,7 @@ module Make(S : Interface) : S = struct
8784
Json_encoding.print_error Format.str_formatter exn;
8885
Format.flush_str_formatter ()
8986
| _ -> Printexc.to_string exn in
90-
Error (UnknownError {
91-
code = -3;
92-
msg = Some msg })
87+
Error (-3, `unknown (Some msg))
9388

9489
let handle_result service res =
9590
let err_encodings = Service.error service.s in
@@ -157,14 +152,14 @@ module Make(S : Interface) : S = struct
157152
request ?headers ?params ?msg ?url_encode ~input api service ((Req.dummy, arg1), arg2)
158153

159154
let handle_error kn = function
160-
| KnownError {code; error} -> code, kn error
161-
| UnknownError {code; msg} -> code, msg
155+
| (code, `known error) -> code, kn error
156+
| (code, `unknown msg) -> code, msg
162157

163158
let string_of_error kn = function
164-
| KnownError {code; error} ->
159+
| (code, `known error) ->
165160
let content = match kn error with None -> "" | Some s -> ": " ^ s in
166161
Printf.sprintf "Error %d%s" code content
167-
| UnknownError {code; msg} ->
162+
| (code, `unknown msg) ->
168163
let content = match msg with None -> "" | Some s -> ": " ^ s in
169164
Printf.sprintf "Unknown Error %d%s" code content
170165
end
@@ -207,8 +202,8 @@ module Make(S : Interface) : S = struct
207202

208203
let unresultize = function
209204
| Ok res -> Ok res
210-
| Error UnknownError { code ; msg } -> Error (code, msg)
211-
| Error KnownError _ -> assert false (* Security.unreachable error *)
205+
| Error (code, `unknown msg) -> Error (code, msg)
206+
| Error (_code, `known _) -> assert false (* Security.unreachable error *)
212207

213208
let get0 ?post ?headers ?params ?msg
214209
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
@@ -10,9 +10,7 @@
1010

1111
open EzAPI
1212

13-
type 'e api_error =
14-
| KnownError of { code : int ; error : 'e }
15-
| UnknownError of { code : int ; msg : string option }
13+
type 'e api_error = int * [`known of 'e | `unknown of string option]
1614

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

test/ppx/test_ppx_client.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ open Lwt.Infix
44
let () =
55
EzLwtSys.run @@ fun () ->
66
EzReq_lwt.post0 (EzAPI.BASE "http://localhost:8080") echo_input ~input:"bla" >|= function
7-
| Error (EzReq_lwt_S.UnknownError {code; msg}) ->
7+
| Error (code, `unknown msg) ->
88
EzDebug.printf "error %d %s" code (Option.value ~default:"none" msg)
99
| Error _ -> EzDebug.printf "error"
1010
| Ok s -> EzDebug.printf "ok %s" s

0 commit comments

Comments
 (0)