From 27931531a5ee30fc48224d1876e9574416d42289 Mon Sep 17 00:00:00 2001 From: Prashanth Mundkur Date: Fri, 8 May 2009 11:03:09 -0700 Subject: [PATCH] simplify json_conv error handling --- libs/json/json_conv.ml | 22 ++++++++++++++++------ libs/json/json_conv.mli | 5 +++-- libs/jsonrpc/jsonrpc.ml | 8 ++++---- libs/jsonrpc/jsonrpc.mli | 4 ++-- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/libs/json/json_conv.ml b/libs/json/json_conv.ml index 463c188..991d3d8 100644 --- a/libs/json/json_conv.ml +++ b/libs/json/json_conv.ml @@ -13,25 +13,35 @@ * GNU Lesser General Public License for more details. *) -type conv_error = +type error = | Unexpected_json_type of (* rcvd *) string * (* expected *) string | Array_length of (* rcvd *) int * (* expected *) int | Unknown_constructor of (* type *) string * (* constructor *) string | Missing_object_field of string -exception Json_conv_failure of conv_error +let string_of_error = function + | Unexpected_json_type (r, e) -> + Printf.sprintf "type %s received when %s was expected" r e + | Array_length (r, e) -> + Printf.sprintf "array length %d received when %d was expected" r e + | Unknown_constructor (t, c) -> + Printf.sprintf "unknown constructor %s received for type %s" c t + | Missing_object_field f -> + Printf.sprintf "missing object field %s" f + +exception Json_conv_error of error let raise_unexpected_json_type typ exp = - raise (Json_conv_failure (Unexpected_json_type (typ, exp))) + raise (Json_conv_error (Unexpected_json_type (typ, exp))) let raise_short_array len exp = - raise (Json_conv_failure (Array_length (len, exp))) + raise (Json_conv_error (Array_length (len, exp))) let raise_unknown_constructor typ cons = - raise (Json_conv_failure (Unknown_constructor (typ, cons))) + raise (Json_conv_error (Unknown_constructor (typ, cons))) let raise_missing_object_field field = - raise (Json_conv_failure (Missing_object_field field)) + raise (Json_conv_error (Missing_object_field field)) let string_of_json ?(permissive=false) j = let strict = function diff --git a/libs/json/json_conv.mli b/libs/json/json_conv.mli index 91dcf4b..921d221 100644 --- a/libs/json/json_conv.mli +++ b/libs/json/json_conv.mli @@ -15,13 +15,14 @@ (* conversion errors *) -type conv_error = +type error = | Unexpected_json_type of (* rcvd *) string * (* expected *) string | Array_length of (* rcvd *) int * (* expected *) int | Unknown_constructor of (* type *) string * (* constructor *) string | Missing_object_field of string +val string_of_error: error -> string -exception Json_conv_failure of conv_error +exception Json_conv_error of error (* conversion routines for base types *) diff --git a/libs/jsonrpc/jsonrpc.ml b/libs/jsonrpc/jsonrpc.ml index 284d540..db093d4 100644 --- a/libs/jsonrpc/jsonrpc.ml +++ b/libs/jsonrpc/jsonrpc.ml @@ -23,11 +23,11 @@ let code_error_invalid_params = -32602 (* Invalid method parameters. *) let code_error_internal_error = -32603 (* Internal JSON-RPC error. *) type req_error = - | Request_json_conv_error of conv_error + | Request_json_conv_error of error | Request_invalid_params type resp_error = - | Response_json_conv_error of conv_error + | Response_json_conv_error of error | Response_no_result_or_error | Response_both_result_and_error @@ -63,7 +63,7 @@ let request_of_json j = if not (Json.is_object params) && not (Json.is_array params) then raise (Invalid_request Request_invalid_params); { request_id = id; method_name = method_name; params = params } - with Json_conv_failure e -> raise (Invalid_request (Request_json_conv_error e)) + with Json_conv_error e -> raise (Invalid_request (Request_json_conv_error e)) let request_of_string s = request_of_json (Json.of_string s) @@ -105,7 +105,7 @@ let response_of_json j = response_make_error id c m d | _ -> raise (Invalid_response Response_both_result_and_error) - with Json_conv_failure e -> raise (Invalid_response (Response_json_conv_error e)) + with Json_conv_error e -> raise (Invalid_response (Response_json_conv_error e)) let response_of_string s = response_of_json (Json.of_string s) diff --git a/libs/jsonrpc/jsonrpc.mli b/libs/jsonrpc/jsonrpc.mli index e343bce..bb54c14 100644 --- a/libs/jsonrpc/jsonrpc.mli +++ b/libs/jsonrpc/jsonrpc.mli @@ -20,11 +20,11 @@ val code_error_invalid_params : int val code_error_internal_error : int type req_error = - | Request_json_conv_error of Json_conv.conv_error + | Request_json_conv_error of Json_conv.error | Request_invalid_params type resp_error = - | Response_json_conv_error of Json_conv.conv_error + | Response_json_conv_error of Json_conv.error | Response_no_result_or_error | Response_both_result_and_error -- 2.39.5