Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more flexible api error #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions src/request/ezRequest_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) =
Expand Down
4 changes: 1 addition & 3 deletions src/request/virtual/s/ezReq_lwt_S.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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` *)
Expand Down
2 changes: 1 addition & 1 deletion test/ppx/test_ppx_client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading