From eb34c0b45506ef9260aee77e5ecb413078df5df2 Mon Sep 17 00:00:00 2001 From: Maxime Levillain Date: Tue, 15 Oct 2024 10:18:18 +0200 Subject: [PATCH] pp_error and wrap in EzReq_lwt --- src/request/ezRequest_lwt.ml | 19 +++++++++++++++++++ src/request/virtual/s/ezReq_lwt_S.mli | 2 ++ 2 files changed, 21 insertions(+) diff --git a/src/request/ezRequest_lwt.ml b/src/request/ezRequest_lwt.ml index 8007ca1..8429475 100644 --- a/src/request/ezRequest_lwt.ml +++ b/src/request/ezRequest_lwt.ml @@ -167,6 +167,19 @@ module Make(S : Interface) : S = struct | UnknownError {code; msg} -> let content = match msg with None -> "" | Some s -> ": " ^ s in Printf.sprintf "Unknown Error %d%s" code content + + let pp_error ?error pp = function + | UnknownError {code; msg} -> + Format.fprintf pp "Error %d%s" code + (Option.fold ~none:"" ~some:(fun s -> ": " ^ s) msg) + | KnownError {code; error=e} -> + match error with + | None -> Format.fprintf pp "Error %d" code + | Some ppe -> Format.fprintf pp "Error %d: %a" code ppe e + + let wrap p = Lwt.map (Result.map_error (function + | UnknownError {code; msg} -> code, `unknown msg + | KnownError {code; error} -> code, `known error)) p end include Raw @@ -252,6 +265,12 @@ module Make(S : Interface) : S = struct let string_of_error _ (code, content) = let content = match content with None -> "" | Some s -> ": " ^ s in Printf.sprintf "Error %d%s" code content + + let pp_error ?error:_ pp (code, content) = + Format.fprintf pp "Error %d%s" code + (Option.fold ~none:"" ~some:(fun s -> ": " ^ s) content) + let wrap p = Lwt.map (Result.map_error (fun (code, content) -> code, `unknown content)) p + end end diff --git a/src/request/virtual/s/ezReq_lwt_S.mli b/src/request/virtual/s/ezReq_lwt_S.mli index d499d15..87312c6 100644 --- a/src/request/virtual/s/ezReq_lwt_S.mli +++ b/src/request/virtual/s/ezReq_lwt_S.mli @@ -109,6 +109,8 @@ module type RAWGEN = sig val handle_error : ('a -> string option) -> 'a api_error -> int * string option val string_of_error : ('a -> string option) -> 'a api_error -> string + val pp_error : ?error:(Format.formatter -> 'a -> unit) -> Format.formatter -> 'a api_error -> unit + val wrap : ('a, 'e api_error) result Lwt.t -> ('a, int * [ `known of 'e | `unknown of string option ]) result Lwt.t end module type RAW = RAWGEN