Skip to content

Commit

Permalink
pp_error and wrap in EzReq_lwt
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtori committed Oct 15, 2024
1 parent 038934c commit eb34c0b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/request/ezRequest_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/request/virtual/s/ezReq_lwt_S.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit eb34c0b

Please sign in to comment.