Skip to content

Commit d7cee2e

Browse files
authored
Merge pull request #20 from cryptosense/fix-error-messages
Fix the error messages/exit codes.
2 parents 2209e3d + 2f100d6 commit d7cee2e

6 files changed

Lines changed: 98 additions & 9 deletions

File tree

cs_api_cli/cs_api_cli.ml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let resolve_project_name ~client ~api ~project_id ~project_name =
2828
| (None, Some name) -> (
2929
Cs_api_core.build_list_projects_request ~api
3030
|> Cs_api_io.send_request ~client
31-
>>= Cs_api_io.get_response
31+
>>= Cs_api_io.get_response_graphql
3232
>>= fun body ->
3333
let projects = Cs_api_core.parse_list_projects_response ~body in
3434
match CCList.Assoc.get ~eq:String.equal name projects with
@@ -51,7 +51,7 @@ let upload_trace
5151
>>= fun project_id ->
5252
Cs_api_core.build_s3_signed_post_request ~api
5353
|> Cs_api_io.send_request ~client
54-
>>= Cs_api_io.get_response
54+
>>= Cs_api_io.get_response_graphql
5555
>>= (fun body ->
5656
match Cs_api_core.parse_s3_signature_request ~body with
5757
| None ->
@@ -71,13 +71,14 @@ let upload_trace
7171
in
7272
Lwt.return (Ok import_request))
7373
>>= Cs_api_io.send_request ~client
74-
>>= Cs_api_io.get_response
75-
>|= fun _ -> Printf.printf "Trace uploaded\n")
74+
>>= Cs_api_io.get_response_graphql)
7675
>|= function
77-
| Ok _ as ok -> ok
76+
| Ok _ ->
77+
Printf.printf "Trace uploaded\n";
78+
0
7879
| Error message ->
7980
Printf.printf "%s\n" message;
80-
Error ()
81+
1
8182

8283
let trace_file =
8384
let doc = "Path to the file containing the trace" in
@@ -188,4 +189,5 @@ let default_info = Cmdliner.Term.info "cs-api" ~version:"%%VERSION_NUM%%"
188189
let default_cmd = (default_term, default_info)
189190

190191
let () =
191-
Cmdliner.Term.eval_choice default_cmd [upload_trace_cmd] |> Cmdliner.Term.exit
192+
Cmdliner.Term.eval_choice default_cmd [upload_trace_cmd]
193+
|> Cmdliner.Term.exit_status

cs_api_io/cs_api_io.ml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,33 @@ let get_response {Response.code; body} =
7777
else
7878
let message = Printf.sprintf "HTTP response: %d\n%s" code body in
7979
Lwt_result.fail message
80+
81+
let get_graphql_errors (json : Yojson.Safe.t) =
82+
(match json with
83+
| `Assoc _ -> (Some (Yojson.Safe.Util.member "errors" json), [])
84+
| _ -> (None, ["Unexpected response from the server"]))
85+
|> function
86+
| (None, errors) -> errors
87+
| (Some `Null, _) -> []
88+
| (Some errors_json, _) -> (
89+
match errors_json with
90+
| `List error_list ->
91+
error_list
92+
|> CCList.map (Yojson.Safe.Util.member "message")
93+
|> CCList.map (function
94+
| `String error_message -> error_message
95+
| _ -> "Unexpected response from the server")
96+
| _ -> ["Unexpected response from the server"])
97+
98+
let get_response_graphql {Response.code; body} =
99+
if code < 300 then
100+
try
101+
let json = Yojson.Safe.from_string body in
102+
match get_graphql_errors json with
103+
| [] -> Lwt_result.return body
104+
| errors -> CCString.concat "\n" errors |> Lwt_result.fail
105+
with
106+
| _ -> Lwt_result.return body
107+
else
108+
let message = Printf.sprintf "HTTP response: %d\n%s" code body in
109+
Lwt_result.fail message

cs_api_io/cs_api_io.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ val send_request :
2121
client:t -> Api.Request.t -> (Response.t, string) result Lwt.t
2222

2323
val get_response : Response.t -> (string, string) result Lwt.t
24+
25+
val get_graphql_errors : Yojson.Safe.t -> string list
26+
27+
val get_response_graphql : Response.t -> (string, string) result Lwt.t

test/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
(test
22
(name test)
3-
(libraries api cs_api_core alcotest multipart_form_writer))
3+
(libraries api cs_api_core cs_api_io alcotest multipart_form_writer))

test/test.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,5 @@ let request_builder_tests =
6464
let () =
6565
Alcotest.run "API Client"
6666
[ ("Request builders", request_builder_tests)
67-
; ("Multipart writer", Test_writer.accumulator) ]
67+
; ("Multipart writer", Test_writer.accumulator)
68+
; ("Graphql errors parsing", Test_api_io.tests) ]

test/test_api_io.ml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
let test ~name ~body ~expected_errors =
2+
( name
3+
, `Quick
4+
, fun () ->
5+
let json = Yojson.Safe.from_string body in
6+
let result = Cs_api_io.get_graphql_errors json in
7+
Alcotest.(check (list string)) name result expected_errors )
8+
9+
let body_ok = {|{"data":{"createTrace":null}}|}
10+
11+
let body_error =
12+
{|{
13+
"errors": [
14+
{
15+
"message": "No project with this ID was found",
16+
"locations": [{"line":3,"column":9}],
17+
"path": ["createTrace"]
18+
}
19+
],
20+
"data": {"createTrace":null}
21+
}|}
22+
23+
let body_no_error_message =
24+
{|{
25+
"errors": [
26+
{
27+
"locations": [{"line":3,"column":9}],
28+
"path": ["createTrace"]
29+
}
30+
],
31+
"data": {"createTrace":null}
32+
}|}
33+
34+
let body_multiple_errors =
35+
{|{
36+
"errors": [
37+
{"message": "No project with this ID was found"},
38+
{"message": "No project with this name was found"}
39+
],
40+
"data": {"createTrace":null}
41+
}|}
42+
43+
let tests =
44+
[ test ~name:"test_ok" ~body:body_ok ~expected_errors:[]
45+
; test ~name:"test_error" ~body:body_error
46+
~expected_errors:["No project with this ID was found"]
47+
; test ~name:"test_no_error_message" ~body:body_no_error_message
48+
~expected_errors:["Unexpected response from the server"]
49+
; test ~name:"test_multiple_errors" ~body:body_multiple_errors
50+
~expected_errors:
51+
[ "No project with this ID was found"
52+
; "No project with this name was found" ] ]

0 commit comments

Comments
 (0)