From 7de89bd55502c876e06582f95a84e6cd4566e5e1 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 28 Feb 2024 16:11:16 -0500 Subject: [PATCH] expose Response.Bad_req --- src/core/response.ml | 2 ++ src/core/response.mli | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/core/response.ml b/src/core/response.ml index 3e2d4347..e1d2721a 100644 --- a/src/core/response.ml +++ b/src/core/response.ml @@ -63,6 +63,8 @@ let make ?headers ?(code = 200) r : t = let fail ?headers ~code fmt = Printf.ksprintf (fun msg -> make_raw ?headers ~code msg) fmt +exception Bad_req = Bad_req + let fail_raise ~code fmt = Printf.ksprintf (fun msg -> raise (Bad_req (code, msg))) fmt diff --git a/src/core/response.mli b/src/core/response.mli index 6ddf08b8..4cf0f192 100644 --- a/src/core/response.mli +++ b/src/core/response.mli @@ -99,10 +99,14 @@ val fail : ?headers:Headers.t -> code:int -> ('a, unit, string, t) format4 -> 'a Example: [fail ~code:404 "oh noes, %s not found" "waldo"]. *) +exception Bad_req of int * string +(** Exception raised by {!fail_raise} with the HTTP code and body *) + val fail_raise : code:int -> ('a, unit, string, 'b) format4 -> 'a (** Similar to {!fail} but raises an exception that exits the current handler. - This should not be used outside of a (path) handler. - Example: [fail_raise ~code:404 "oh noes, %s not found" "waldo"; never_executed()] + This should not be used outside of a (path) handler. + Example: [fail_raise ~code:404 "oh noes, %s not found" "waldo"; never_executed()] + @raise Bad_req always *) val pp : Format.formatter -> t -> unit