diff --git a/src/api_piqi_util.ml b/src/api_piqi_util.ml index 62f4454..a66444e 100644 --- a/src/api_piqi_util.ml +++ b/src/api_piqi_util.ml @@ -246,13 +246,6 @@ module ReferencePerson end -let get_params conf parse = - match (p_getenvbin conf.env "data", p_getenvbin conf.env "input") with - | (Some d, Some "pb") -> parse d `pb - | (Some d, Some "json") -> parse d `json - | (Some d, Some "xml") -> parse d `xml - | _ -> exit (-2) - let print_result conf data = let (content_type, output) = match p_getenvbin conf.env "output" with @@ -265,6 +258,36 @@ let print_result conf data = Util.html ~content_type conf ; Output.print_string conf data +let from_piqi_status = function + | `bad_request -> Def.Bad_Request + | `unauthorized -> Def.Unauthorized + | `forbidden -> Def.Forbidden + | `not_found -> Def.Not_Found + | `conflict -> Def.Conflict + +(** [print_error conf code] + Print error code and [raise Exit] +*) +let print_error conf code msg = + let piqi_error = Api_piqi.default_error () in + piqi_error.Api_piqi.Error.code <- code ; + piqi_error.Api_piqi.Error.message <- Opt.of_string msg ; + let data = Api_piqi_ext.gen_error piqi_error in + Output.status conf (from_piqi_status code) ; + print_result conf data ; + raise Exit + +let get_params conf parse = + match (p_getenvbin conf.env "data", p_getenvbin conf.env "input") with + | (Some d, Some "pb") -> parse d `pb + | (Some d, Some "json") -> parse d `json + | (Some d, Some "xml") -> parse d `xml + | _ -> assert false + +let get_params conf parse = + try get_params conf parse + with e -> print_error conf `bad_request (Printexc.to_string e) + let piqi_fevent_name_of_fevent_name = function | Efam_Marriage -> `efam_marriage | Efam_NoMarriage -> `efam_no_marriage diff --git a/src/api_util.ml b/src/api_util.ml index 20503c3..732c01a 100644 --- a/src/api_util.ml +++ b/src/api_util.ml @@ -1586,26 +1586,6 @@ let person_node_map_lia conf base l = })) l) -let from_piqi_status = function - | `bad_request -> Def.Bad_Request - | `unauthorized -> Def.Unauthorized - | `forbidden -> Def.Forbidden - | `not_found -> Def.Not_Found - | `conflict -> Def.Conflict - -(** [print_error conf code] - Print error code and [raise Exit] -*) -let print_error conf code msg = - let piqi_error = M.default_error () in - piqi_error.M.Error.code <- code ; - piqi_error.M.Error.message <- Opt.of_string msg ; - let data = Mext.gen_error piqi_error in - Output.status conf (from_piqi_status code) ; - print_result conf data ; - raise Exit - - let chop_base_prefix base_prefix = let len = String.length base_prefix in if len > 2 && @@ -1614,3 +1594,5 @@ let chop_base_prefix base_prefix = then String.sub base_prefix 0 (len - 2) else base_prefix + +let print_error = Api_piqi_util.print_error