Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Response.or_internal_error #120

Merged
merged 2 commits into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/piaf.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/response.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down