From ac7dd5ab7302b540082ebebc4f6fd6566233d90f 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 +--- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/request/ezRequest_lwt.ml b/src/request/ezRequest_lwt.ml index 43698f2..b8ba7f1 100644 --- a/src/request/ezRequest_lwt.ml +++ b/src/request/ezRequest_lwt.ml @@ -54,26 +54,21 @@ 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 | Ok s -> Ok s - | Error (`destruct_exn exn) -> Error (UnknownError { - code = -3; - msg = Some (Printexc.to_string exn) }) + | Error (`destruct_exn exn) -> Error (-3, `unknown (Some (Printexc.to_string exn))) let handle_result service res = let err_encodings = Service.error service.s in @@ -141,14 +136,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 @@ -191,8 +186,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 3219fab..9d20fed 100644 --- a/src/request/virtual/s/ezReq_lwt_S.mli +++ b/src/request/virtual/s/ezReq_lwt_S.mli @@ -1,8 +1,6 @@ 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` *)