diff --git a/src/lib_stdlib_unix/lwt_utils_unix.ml b/src/lib_stdlib_unix/lwt_utils_unix.ml index a729fd84d318e9e7e1aad4a42bf4f3094ecd2431..01bac84d833162740df2036a36eec618d8125ec0 100644 --- a/src/lib_stdlib_unix/lwt_utils_unix.ml +++ b/src/lib_stdlib_unix/lwt_utils_unix.ml @@ -302,6 +302,19 @@ let getpass () = passwd module Json = struct + type error += Json_decoding_error of {input : string; error : string} + + let () = + register_error_kind + `Temporary + ~id:"json_decoding_error" + ~title:"Json decoding error" + ~description:"The input string is not a valid JSON value" + Data_encoding.(obj2 (req "input" string) (req "error" string)) + (function + | Json_decoding_error {input; error} -> Some (input, error) | _ -> None) + (fun (input, error) -> Json_decoding_error {input; error}) + let to_root = function | `O ctns -> `O ctns | `A ctns -> `A ctns @@ -322,7 +335,9 @@ module Json = struct Lwt_io.with_file ~mode:Input file (fun chan -> let open Lwt_result_syntax in let*! str = Lwt_io.read chan in - return (Ezjsonm.from_string str :> Data_encoding.json))) + match Data_encoding.Json.from_string str with + | Ok json -> return json + | Error error -> tzfail (Json_decoding_error {input = str; error}))) end (* This module is used by [safe_cancel_on_exit] to register and unregister diff --git a/tezt/tests/nonce_seed_revelation.ml b/tezt/tests/nonce_seed_revelation.ml index dbef54d3dcc8d83a8e3b7c27c73e51ecda441f07..b5719a3d4cba02547738617e00313cb130de39ed 100644 --- a/tezt/tests/nonce_seed_revelation.ml +++ b/tezt/tests/nonce_seed_revelation.ml @@ -313,11 +313,16 @@ let test_baking_nonce_migration = Log.info "Concat old nonces contents with the new one" ; let new_nonces_contents = Base.read_file nonces_file in - let () = - Base.write_file - nonces_file - ~contents:(old_nonces_contents ^ new_nonces_contents) + let merged_nonces_contents = + match + ( Data_encoding.Json.from_string old_nonces_contents, + Data_encoding.Json.from_string new_nonces_contents ) + with + | Ok (`A old), Ok (`A new_) -> + Data_encoding.Json.to_string (`A (old @ new_)) + | _ -> Test.fail "Incorrectly formatted nonces files" in + let () = Base.write_file nonces_file ~contents:merged_nonces_contents in Log.info "Restart the baker and wait for ignore failed nonce migration event then \