Skip to content

Commit 5009e0b

Browse files
authored
Merge pull request #103 from maxtori/destruct-error
simplify and widen the destruct error result
2 parents 7ce409c + 8090daf commit 5009e0b

File tree

9 files changed

+19
-45
lines changed

9 files changed

+19
-45
lines changed

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
all: build
33

44
build:
5-
dune build src
5+
@dune build src
66

77
dev:
8-
dune build
8+
@dune build
99

1010
install:
11-
dune install
11+
@dune install
1212

1313
clean:
14-
dune clean
14+
@dune clean
1515

1616
doc:
17-
dune build @doc
18-
rsync -ru _build/default/_doc/_html/* docs/
17+
@dune build @doc
18+
@rsync -ru _build/default/_doc/_html/* docs/

src/encoding/ezEncoding.ml

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,11 @@ let destruct encoding buf =
2020
field buf ;
2121
raise DestructError
2222

23-
type destruct_error = [
24-
| `cannot_destruct of string * string
25-
| `unexpected_field of string ]
23+
type destruct_error = [ `destruct_exn of exn ]
2624

2725
let destruct_res encoding s =
2826
try Ok (Json_encoding.destruct encoding (Ezjsonm.from_string s))
29-
with
30-
| Cannot_destruct (path, exn) ->
31-
Json_query.print_path_as_json_path ~wildcards:true Format.str_formatter path;
32-
Error (`cannot_destruct ((Format.flush_str_formatter ()), Printexc.to_string exn))
33-
| Unexpected_field field ->
34-
Error (`unexpected_field field)
35-
36-
let error_to_string ?from e =
37-
let from = match from with None -> "" | Some s -> " in\n" ^s in
38-
match e with
39-
| `cannot_destruct (path, exn) ->
40-
Printf.sprintf "Cannot destruct: %s from %s%s" path exn from
41-
| `unexpected_field field ->
42-
Printf.sprintf "Unexpected_field: %s%s" field from
27+
with exn -> Error (`destruct_exn exn)
4328

4429
let construct ?compact encoding data =
4530
Ezjsonm.to_string ?minify:compact (Json_encoding.construct encoding data)

src/encoding/ezEncoding.mli

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11

22
exception DestructError
33

4-
type destruct_error = [
5-
| `cannot_destruct of string * string
6-
| `unexpected_field of string ]
4+
type destruct_error = [ `destruct_exn of exn ]
75

86
val destruct : 'a Json_encoding.encoding -> string -> 'a
97
val destruct_res : 'a Json_encoding.encoding -> string -> ('a, [> destruct_error]) result
108
val construct : ?compact:bool -> 'a Json_encoding.encoding -> 'a -> string
11-
val error_to_string : ?from:string -> [< destruct_error ] -> string
129

1310
module Ezjsonm : sig
1411
val from_string : string -> Json_repr.ezjsonm

src/request/ezRequest.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ let before_hook = ref (fun () -> ())
1212
let decode_result ?error io f res =
1313
match IO.from_string io f res with
1414
| Ok () -> ()
15-
| Error e -> match error with
15+
| Error (`destruct_exn exn) -> match error with
1616
| None -> ()
17-
| Some error -> error (-3) (Some (EzEncoding.error_to_string ~from:res e))
17+
| Some error -> error (-3) (Some (Printexc.to_string exn))
1818

1919
let any_get = ref (fun ?meth:_m ?headers:_ ?msg:_ _url f ->
2020
f (Error (-2,Some "No http client loaded")))

src/request/ezRequest_lwt.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ module Make(S : Interface) : S = struct
7171
| Ok res ->
7272
match IO.from_string io (fun x -> x) res with
7373
| Ok s -> Ok s
74-
| Error e -> Error (UnknownError {
74+
| Error (`destruct_exn exn) -> Error (UnknownError {
7575
code = -3;
76-
msg = Some (EzEncoding.error_to_string ~from:res e) })
76+
msg = Some (Printexc.to_string exn) })
7777

7878
let handle_result service res =
7979
let err_encodings = Service.error service.s in

src/server/answer.ml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,10 @@ let cannot_parse (descr, msg, path) =
1919

2020
let method_not_allowed () = return ~code:405 ""
2121

22-
let cannot_destruct (path, exn) =
22+
let destruct_exception exn =
2323
let body = EzEncoding.construct Json_encoding.any_ezjson_value @@
24-
`O [ "error", `String "Cannot destruct JSON";
25-
"path", `String path;
26-
"msg", `String exn ] in
27-
return ~code:400 ~headers body
28-
29-
let unexpected_field f =
30-
let body = EzEncoding.construct Json_encoding.any_ezjson_value @@
31-
`O [ "error", `String "Unexpected field in JSON";
32-
"field", `String f ] in
24+
`O [ "error", `String "Destruct exception";
25+
"exception", `String (Printexc.to_string exn) ] in
3326
return ~code:400 ~headers body
3427

3528
let unsupported_media_type c =

src/server/ezAPIServerUtils.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ let handle ?meth ?content_type ?ws s r path body =
123123
| Ok (`http h) ->
124124
begin
125125
h body >>= function
126-
| Error (`cannot_destruct a) -> Answer.cannot_destruct a
127-
| Error (`unexpected_field f) -> Answer.unexpected_field f
126+
| Error (`destruct_exn exn) -> Answer.destruct_exception exn
128127
| Error (`unsupported c) -> Answer.unsupported_media_type c
129128
| Error (`handler_error s) ->
130129
EzDebug.printf "In %s: error %s" (String.concat "/" path) s;

src/websocket/js/ezWs.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ let connect0 ?msg ?protocols ?error ~react base service =
6565
let send i = a.send (EzAPI.IO.to_string input i) in
6666
match EzAPI.IO.res_from_string output (res_encoding errors) (react {send; close=a.close}) s with
6767
| Ok r -> r
68-
| Error e -> Lwt.return_error (EzEncoding.error_to_string e) in
68+
| Error (`destruct_exn exn) -> Lwt.return_error (Printexc.to_string exn) in
6969
connect ?msg ?protocols ?error ~react url >|= function
7070
| Error e -> Error e
7171
| Ok r ->

src/websocket/unix/ezWs.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ let connect0 ?msg ?protocols ?error ~react base service =
8282
let action = {send; close = action.close} in
8383
match EzAPI.IO.res_from_string output (res_encoding errors) (react action) s with
8484
| Ok r -> r
85-
| Error e -> Lwt.return_error (EzEncoding.error_to_string e) in
85+
| Error (`destruct_exn exn) -> Lwt.return_error (Printexc.to_string exn) in
8686
connect ?msg ?protocols ?error ~react url >|= function
8787
| Error e -> Error e
8888
| Ok r ->

0 commit comments

Comments
 (0)