log response status code

This commit is contained in:
swrup 2026-03-30 14:57:36 +02:00 committed by Swrup
parent 18c4f0b3d5
commit e966b62712
2 changed files with 22 additions and 37 deletions

View file

@ -25,4 +25,4 @@ let not_implemented_route : (string, Global.env) Vifu.Handler.t =
Logs.debug (fun m -> m "not_implemented_route_handler `%s`" target); Logs.debug (fun m -> m "not_implemented_route_handler `%s`" target);
uri_prefix_list uri_prefix_list
|> List.find_opt (fun uri -> Vifu.Uri.execp uri target) |> List.find_opt (fun uri -> Vifu.Uri.execp uri target)
|> Option.map (fun _uri -> Respond.not_implemented req) |> Option.map (fun _uri -> Respond.not_implemented ())

View file

@ -1,32 +1,31 @@
let bad_request _req = let pp_status ppf status =
let open Vifu.Response in match status with
Logs.debug (fun m -> m "Bad request"); | #H2.Status.standard as status ->
respond `Bad_request Fmt.pf ppf "HTTP [%d %s]" (H2.Status.to_code status)
(H2.Status.default_reason_phrase status)
| _ -> Fmt.pf ppf "HTTP [%d]" (H2.Status.to_code status)
let no_content _req = let empty status =
Logs.debug (fun m -> m "No content"); Logs.info (fun m -> m "%a" pp_status status);
let open Vifu.Response in let open Vifu.Response in
let open Syntax in let open Syntax in
let* () = empty in let* () = empty in
respond `No_content respond status
let not_modified () = let bad_request () = empty `Bad_request
Logs.debug (fun m -> m "Not modified"); let no_content () = empty `No_content
let open Vifu.Response in let not_modified () = empty `Not_modified
let open Syntax in let not_implemented () = empty `Not_implemented
let* () = empty in
respond `Not_modified
let respond_json req jsont status body = let respond_json req jsont status body =
Logs.info (fun m -> m "%a" pp_status status);
let open Vifu.Response in let open Vifu.Response in
let open Syntax in let open Syntax in
let* () = add ~field:"content-type" "application/json" in let* () = add ~field:"content-type" "application/json" in
let* () = with_json req jsont body in let* () = with_json req jsont body in
respond status respond status
let ok req jsont body = let ok req jsont body = respond_json req jsont `OK body
Logs.debug (fun m -> m "OK");
respond_json req jsont `OK body
let map_err_status err = let map_err_status err =
match err with match err with
@ -37,32 +36,18 @@ let map_err_status err =
| `Caqti _s | `Mfat _s | `Json_encode _s | `Bin_encode _s | `Msg _s -> | `Caqti _s | `Mfat _s | `Json_encode _s | `Bin_encode _s | `Msg _s ->
`Internal_server_error `Internal_server_error
let status_to_string = function
| `Bad_request -> "Bad request"
| `Forbidden -> "Forbidden"
| `Not_found -> "Not found"
| `Conflict -> "Conflict"
| `Internal_server_error -> "Internal server error"
let error_detail hint =
let open Api in
let code = -1 in
ErrorDetail.make ~hint code
let error req err = let error req err =
let status = map_err_status err in let status = map_err_status err in
let hint = Result.err_to_string err in let hint = Result.err_to_string err in
let body = error_detail hint in let log_lvl =
Logs.err (fun m -> m "%s: %s" (status_to_string status) hint); if H2.Status.is_server_error status then Logs.Error else Logs.Warning
in
Logs.msg log_lvl (fun m -> m "%a %s" pp_status status hint);
let body = Api.ErrorDetail.make ~hint (-1) in
respond_json req Api.ErrorDetail.jsont status body respond_json req Api.ErrorDetail.jsont status body
let not_implemented req =
Logs.debug (fun m -> m "Not implemented");
let body = error_detail "Not Implemented" in
respond_json req Api.ErrorDetail.jsont `Not_implemented body
let result req jsont res = let result req jsont res =
match res with Error e -> error req e | Ok body -> ok req jsont body match res with Error e -> error req e | Ok body -> ok req jsont body
let result_no_content req res = let result_no_content req res =
match res with Error e -> error req e | Ok () -> no_content req match res with Error e -> error req e | Ok () -> no_content ()