Skip to content

Commit

Permalink
Api_piqi_util.get_param: print Bad_Request error if parsing failed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Sagot committed Mar 22, 2022
1 parent 1b4875b commit 5c74bc5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
37 changes: 30 additions & 7 deletions src/api_piqi_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
22 changes: 2 additions & 20 deletions src/api_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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

0 comments on commit 5c74bc5

Please sign in to comment.