Something went wrong. Try again.
My own corner of monopam
Something went wrong. Try again.
787 B · 24 lines
OCaml
at main
12345678910111213141516171819202122232425type t = { status : int; message : string }
(* The JSON error body is just {"message": "..."}. *)let message_codec = let open Json.Codec in Object.map (fun message -> message) |> Object.member "message" string ~dec_absent:"" ~enc:(fun m -> m) |> Object.skip_unknown |> Object.seal
let of_response ~status ~body = let message = match Json.of_string message_codec body with | Ok m when m <> "" -> m | _ -> ( match String.trim body with "" -> Fmt.str "HTTP %d" status | s -> s) in { status; message }
let to_body t = Json.to_string message_codec t.message
let pp ppf t = (* status 0 means no HTTP response was received (e.g. a transport failure). *) if t.status = 0 then Fmt.string ppf t.message else Fmt.pf ppf "HTTP %d: %s" t.status t.message