(* err.ml - Error handling combinators for HTTP parsing *) (* Re-export status type for convenience *) type status = Buf_read.status = | Complete | Partial | Invalid_method | Invalid_target | Invalid_version | Invalid_header | Headers_too_large | Malformed | Content_length_overflow | Ambiguous_framing | Bare_cr_detected | Missing_host_header | Unsupported_transfer_encoding (* Core exception *) exception Parse_error of status (* Basic fail - raise with specific status *) let fail status = raise (Parse_error status) (* Common status shortcuts *) let partial () = raise (Parse_error Partial) let malformed () = raise (Parse_error Malformed) (* Conditional raises - raise if condition is TRUE *) let when_ cond status = if cond then raise (Parse_error status) let partial_when cond = if cond then raise (Parse_error Partial) let malformed_when cond = if cond then raise (Parse_error Malformed) (* Guard - raise if condition is FALSE (i.e., require condition to be true) *) let guard cond status = if not cond then raise (Parse_error status) let partial_unless cond = if not cond then raise (Parse_error Partial) let malformed_unless cond = if not cond then raise (Parse_error Malformed) (* Optional: try parser, return None on failure, restore pos via callback *) let optional ~(save : unit -> 'pos) ~(restore : 'pos -> unit) (f : unit -> 'a) : 'a option = let saved = save () in match f () with | v -> Some v | exception Parse_error _ -> restore saved; None