2019-12-10 22:00:21 +04:00
|
|
|
open Trace
|
2019-06-28 16:05:04 +04:00
|
|
|
|
2019-09-20 13:59:44 +04:00
|
|
|
let rec error_pp ?(dev = false) out (e : error) =
|
2019-06-28 16:05:04 +04:00
|
|
|
let open JSON_string_utils in
|
|
|
|
let message =
|
|
|
|
let opt = e |> member "message" |> string in
|
|
|
|
match opt with
|
|
|
|
| Some msg -> ": " ^ msg
|
|
|
|
| None -> "" in
|
|
|
|
let error_code =
|
|
|
|
let error_code = e |> member "error_code" in
|
|
|
|
match error_code with
|
|
|
|
| `Null -> ""
|
|
|
|
| _ -> " (" ^ (J.to_string error_code) ^ ")" in
|
|
|
|
let title =
|
|
|
|
let opt = e |> member "title" |> string in
|
|
|
|
Option.unopt ~default:"" opt in
|
|
|
|
let data =
|
|
|
|
let data = e |> member "data" in
|
|
|
|
match data with
|
|
|
|
| `Null -> ""
|
|
|
|
| _ -> " " ^ (J.to_string data) ^ "\n" in
|
|
|
|
let infos =
|
|
|
|
let infos = e |> member "infos" in
|
|
|
|
match infos with
|
|
|
|
| `List lst -> lst
|
|
|
|
| `Null -> []
|
|
|
|
| x -> [ x ] in
|
2019-09-07 20:42:59 +04:00
|
|
|
let children =
|
|
|
|
let infos = e |> member "children" in
|
|
|
|
match infos with
|
|
|
|
| `List lst -> lst
|
|
|
|
| `Null -> []
|
|
|
|
| x -> [ x ] in
|
2019-06-28 16:05:04 +04:00
|
|
|
let location =
|
|
|
|
let opt = e |> member "data" |> member "location" |> string in
|
2019-12-31 01:16:22 +04:00
|
|
|
let aux cur prec =
|
2019-06-28 16:05:04 +04:00
|
|
|
match prec with
|
|
|
|
| None -> cur |> member "data" |> member "location" |> string
|
|
|
|
| Some s -> Some s
|
|
|
|
in
|
2019-12-31 01:16:22 +04:00
|
|
|
match List.fold_right aux infos opt with
|
2019-06-28 16:05:04 +04:00
|
|
|
| None -> ""
|
|
|
|
| Some s -> s ^ ". "
|
|
|
|
in
|
|
|
|
let print x = Format.fprintf out x in
|
2019-09-07 20:42:59 +04:00
|
|
|
if not dev then (
|
|
|
|
print "%s%s%s%s%s" location title error_code message data
|
|
|
|
) else (
|
|
|
|
print "%s%s%s.\n%s%s\n%a\n%a\n" title error_code message data location
|
2019-09-20 13:59:44 +04:00
|
|
|
(Format.pp_print_list (error_pp ~dev)) infos
|
|
|
|
(Format.pp_print_list (error_pp ~dev)) children
|
2019-09-07 20:42:59 +04:00
|
|
|
)
|
|
|
|
|
2019-09-20 13:59:44 +04:00
|
|
|
let result_pp_hr f out (r : _ result) =
|
|
|
|
match r with
|
|
|
|
| Ok (s , _) -> Format.fprintf out "%a" f s
|
|
|
|
| Error e -> Format.fprintf out "%a" (error_pp ~dev:false) (e ())
|
|
|
|
|
|
|
|
let string_result_pp_hr = result_pp_hr (fun out s -> Format.fprintf out "%s" s)
|
|
|
|
|
|
|
|
let result_pp_dev f out (r : _ result) =
|
|
|
|
match r with
|
|
|
|
| Ok (s , _) -> Format.fprintf out "%a" f s
|
2019-10-07 18:18:32 +04:00
|
|
|
| Error e -> Format.fprintf out "%a" (error_pp ~dev:true) (e ())
|
2019-09-20 13:59:44 +04:00
|
|
|
|
|
|
|
let string_result_pp_dev = result_pp_hr (fun out s -> Format.fprintf out "%s" s)
|
|
|
|
|
2019-09-25 12:49:14 +04:00
|
|
|
let json_pp out x = Format.fprintf out "%s" (J.to_string x)
|
|
|
|
|
2019-09-20 13:59:44 +04:00
|
|
|
let string_result_pp_json out (r : string result) =
|
|
|
|
let status_json status content : J.t = `Assoc ([
|
|
|
|
("status" , `String status) ;
|
|
|
|
("content" , content) ;
|
|
|
|
]) in
|
|
|
|
match r with
|
|
|
|
| Ok (x , _) -> (
|
2019-09-25 12:49:14 +04:00
|
|
|
Format.fprintf out "%a" json_pp (status_json "ok" (`String x))
|
2019-09-20 13:59:44 +04:00
|
|
|
)
|
|
|
|
| Error e -> (
|
2019-09-25 12:49:14 +04:00
|
|
|
Format.fprintf out "%a" json_pp (status_json "error" (e ()))
|
2019-09-20 13:59:44 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type display_format = [
|
|
|
|
| `Human_readable
|
|
|
|
| `Json
|
|
|
|
| `Dev
|
|
|
|
]
|
|
|
|
|
|
|
|
let formatted_string_result_pp (display_format : display_format) =
|
|
|
|
match display_format with
|
|
|
|
| `Human_readable -> string_result_pp_hr
|
|
|
|
| `Dev -> string_result_pp_dev
|
|
|
|
| `Json -> string_result_pp_json
|
2019-09-20 20:56:55 +04:00
|
|
|
|
|
|
|
type michelson_format = [
|
2019-10-26 21:57:22 +04:00
|
|
|
| `Text
|
|
|
|
| `Json
|
|
|
|
| `Hex
|
2019-09-20 20:56:55 +04:00
|
|
|
]
|
|
|
|
|
|
|
|
let michelson_pp (mf : michelson_format) = match mf with
|
2019-10-26 21:57:22 +04:00
|
|
|
| `Text -> Michelson.pp
|
|
|
|
| `Json -> Michelson.pp_json
|
|
|
|
| `Hex -> Michelson.pp_hex
|