From 0c486c9c89d67dbe88f0a2a4e1148ad8987fc159 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 19 May 2022 12:41:45 +0200 Subject: [PATCH] more flexible api error --- src/request/ezRequest_lwt.ml | 29 +++++++++++---------------- src/request/virtual/s/ezReq_lwt_S.mli | 4 +--- test/ppx/test_ppx_client.ml | 2 +- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/request/ezRequest_lwt.ml b/src/request/ezRequest_lwt.ml index 8007ca1..7106725 100644 --- a/src/request/ezRequest_lwt.ml +++ b/src/request/ezRequest_lwt.ml @@ -64,19 +64,16 @@ module Make(S : Interface) : S = struct let post = internal_post module Raw = struct - type nonrec 'e api_error = 'e EzReq_lwt_S.api_error = - | KnownError of { code : int ; error : 'e } - | UnknownError of { code : int ; msg : string option } + type 'e api_error = 'e EzReq_lwt_S.api_error let decode_result io err_encodings = function - | Error (code, None) -> Error (UnknownError { code ; msg = None }) + | Error (code, None) -> Error (code, `unknown None) | Error (code, Some msg) -> (match err_encodings ~code with - | None -> Error (UnknownError { code ; msg = Some msg }) + | None -> Error (code, `unknown (Some msg)) | Some encoding -> - try Error ( - KnownError { code ; error = EzEncoding.destruct encoding msg }) - with _ -> Error (UnknownError { code ; msg = Some msg }) + try Error (code, `known (EzEncoding.destruct encoding msg)) + with _ -> Error (code, `unknown (Some msg)) ) | Ok res -> match IO.from_string io (fun x -> x) res with @@ -87,9 +84,7 @@ module Make(S : Interface) : S = struct Json_encoding.print_error Format.str_formatter exn; Format.flush_str_formatter () | _ -> Printexc.to_string exn in - Error (UnknownError { - code = -3; - msg = Some msg }) + Error (-3, `unknown (Some msg)) let handle_result service res = let err_encodings = Service.error service.s in @@ -157,14 +152,14 @@ module Make(S : Interface) : S = struct request ?headers ?params ?msg ?url_encode ~input api service ((Req.dummy, arg1), arg2) let handle_error kn = function - | KnownError {code; error} -> code, kn error - | UnknownError {code; msg} -> code, msg + | (code, `known error) -> code, kn error + | (code, `unknown msg) -> code, msg let string_of_error kn = function - | KnownError {code; error} -> + | (code, `known error) -> let content = match kn error with None -> "" | Some s -> ": " ^ s in Printf.sprintf "Error %d%s" code content - | UnknownError {code; msg} -> + | (code, `unknown msg) -> let content = match msg with None -> "" | Some s -> ": " ^ s in Printf.sprintf "Unknown Error %d%s" code content end @@ -207,8 +202,8 @@ module Make(S : Interface) : S = struct let unresultize = function | Ok res -> Ok res - | Error UnknownError { code ; msg } -> Error (code, msg) - | Error KnownError _ -> assert false (* Security.unreachable error *) + | Error (code, `unknown msg) -> Error (code, msg) + | Error (_code, `known _) -> assert false (* Security.unreachable error *) let get0 ?post ?headers ?params ?msg api (service: 'output EzAPI.Legacy.service0) = diff --git a/src/request/virtual/s/ezReq_lwt_S.mli b/src/request/virtual/s/ezReq_lwt_S.mli index d499d15..27e32bc 100644 --- a/src/request/virtual/s/ezReq_lwt_S.mli +++ b/src/request/virtual/s/ezReq_lwt_S.mli @@ -10,9 +10,7 @@ open EzAPI -type 'e api_error = - | KnownError of { code : int ; error : 'e } - | UnknownError of { code : int ; msg : string option } +type 'e api_error = int * [`known of 'e | `unknown of string option] (* Note that `?content_type` in post can be overriden by a content-type header in `?headers` *) diff --git a/test/ppx/test_ppx_client.ml b/test/ppx/test_ppx_client.ml index 7909062..b66a2fe 100644 --- a/test/ppx/test_ppx_client.ml +++ b/test/ppx/test_ppx_client.ml @@ -4,7 +4,7 @@ open Lwt.Infix let () = EzLwtSys.run @@ fun () -> EzReq_lwt.post0 (EzAPI.BASE "http://localhost:8080") echo_input ~input:"bla" >|= function - | Error (EzReq_lwt_S.UnknownError {code; msg}) -> + | Error (code, `unknown msg) -> EzDebug.printf "error %d %s" code (Option.value ~default:"none" msg) | Error _ -> EzDebug.printf "error" | Ok s -> EzDebug.printf "ok %s" s