diff --git a/CHANGES.md b/CHANGES.md index 3175bf6e..23f0bf76 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -18,6 +18,8 @@ Unreleased ([#110](https://github.com/anmonteiro/piaf/pull/110)) - openssl: set the client verify callback ([#112](https://github.com/anmonteiro/piaf/pull/112)) +- Piaf.Response: add `or_internal_error` + ([#120](https://github.com/anmonteiro/piaf/pull/120)) 0.1.0 2021-02-03 -------------- diff --git a/flake.lock b/flake.lock index 5d066115..b0ecf1d5 100644 --- a/flake.lock +++ b/flake.lock @@ -35,11 +35,11 @@ "flake-utils": "flake-utils_2" }, "locked": { - "lastModified": 1646336317, - "narHash": "sha256-tp+yu3UGWQx+nzmfVy9R/Uxgp7aEL5unvefVaL0CQPI=", + "lastModified": 1646447780, + "narHash": "sha256-y9diaTU+y5WnovGTlhO3v5VrvN87Oa79KE3c/hVIxaA=", "owner": "anmonteiro", "repo": "nix-overlays", - "rev": "b59a2c4dc6f08b583fc3777c49677577b4e1542b", + "rev": "8bc4e7e5f4d7579d5875ec1c11f5f20bb632b1fc", "type": "github" }, "original": { diff --git a/lib/piaf.mli b/lib/piaf.mli index 08294018..e467ebff 100644 --- a/lib/piaf.mli +++ b/lib/piaf.mli @@ -509,6 +509,7 @@ module Response : sig -> string -> (t, Error.t) Lwt_result.t + val or_internal_error : (t, Error.t) Result.t -> t val persistent_connection : t -> bool val pp_hum : Format.formatter -> t -> unit [@@ocaml.toplevel_printer] end diff --git a/lib/response.ml b/lib/response.ml index 65da7470..25f2927a 100644 --- a/lib/response.ml +++ b/lib/response.ml @@ -140,6 +140,12 @@ let of_h2 ?(body = Body.empty) response = let headers = H2.Headers.remove headers ":status" in { status; headers; version = { major = 2; minor = 0 }; body } +let or_internal_error = function + | Ok r -> r + | Error err -> + let body = Format.asprintf "Internal Server Error: %a" Error.pp_hum err in + of_string ~body `Internal_server_error + let persistent_connection { version; headers; _ } = Message.persistent_connection version headers