Skip to content

Commit

Permalink
more flexible api error
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtori committed May 19, 2022
1 parent 5009e0b commit ac7dd5a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
29 changes: 12 additions & 17 deletions src/request/ezRequest_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) =
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
@@ -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` *)
Expand Down

0 comments on commit ac7dd5a

Please sign in to comment.