2026-02-23 07:24:40 +01:00
|
|
|
let encode_error_detail err =
|
2026-03-26 06:23:42 +01:00
|
|
|
match Api.encode Api.ErrorDetail.jsont err with
|
|
|
|
|
| Error e ->
|
|
|
|
|
Fmt.failwith "json encoding error on `ErrorDetail`: %a." Result.pp_err e
|
2026-02-23 07:24:40 +01:00
|
|
|
| Ok s -> s
|
|
|
|
|
|
2026-02-12 12:13:30 +01:00
|
|
|
let respond_json req content status =
|
2026-03-11 14:17:52 +01:00
|
|
|
let open Vifu.Response in
|
2026-02-12 12:13:30 +01:00
|
|
|
let open Syntax in
|
|
|
|
|
let* () = add ~field:"content-type" "application/json" in
|
|
|
|
|
let* () = with_string req content in
|
|
|
|
|
respond status
|
|
|
|
|
|
|
|
|
|
let mk_error_content ?hint _status =
|
|
|
|
|
let open Api in
|
|
|
|
|
let code = -1 in
|
|
|
|
|
let err = ErrorDetail.make ?hint code in
|
2026-02-23 07:24:40 +01:00
|
|
|
encode_error_detail err
|
2026-02-12 12:13:30 +01:00
|
|
|
|
2026-03-26 06:23:42 +01:00
|
|
|
let error err req =
|
|
|
|
|
let hint = Fmt.str "%a" Result.pp_err err in
|
2026-02-23 07:24:40 +01:00
|
|
|
Logs.err (fun m -> m "error: %s" hint);
|
2026-02-12 12:13:30 +01:00
|
|
|
let body = mk_error_content ~hint `Internal_server_error in
|
|
|
|
|
respond_json req body `Internal_server_error
|
|
|
|
|
|
|
|
|
|
let bad_request ?hint req =
|
|
|
|
|
Logs.err (fun m -> m "bad request");
|
|
|
|
|
let body = mk_error_content ?hint `Bad_request in
|
|
|
|
|
respond_json req body `Bad_request
|
|
|
|
|
|
|
|
|
|
let ok content req =
|
|
|
|
|
Logs.debug (fun m -> m "ok");
|
|
|
|
|
respond_json req content `OK
|
|
|
|
|
|
2026-02-23 07:24:40 +01:00
|
|
|
let no_content () =
|
|
|
|
|
Logs.debug (fun m -> m "no content");
|
2026-03-11 14:17:52 +01:00
|
|
|
let open Vifu.Response in
|
2026-02-23 07:24:40 +01:00
|
|
|
let open Syntax in
|
|
|
|
|
let* () = empty in
|
|
|
|
|
respond `No_content
|
|
|
|
|
|
2026-02-12 12:13:30 +01:00
|
|
|
let not_modified () =
|
|
|
|
|
Logs.debug (fun m -> m "not modified");
|
2026-03-11 14:17:52 +01:00
|
|
|
let open Vifu.Response in
|
2026-02-12 12:13:30 +01:00
|
|
|
let open Syntax in
|
|
|
|
|
let* () = empty in
|
|
|
|
|
respond `Not_modified
|
|
|
|
|
|
|
|
|
|
let result res req =
|
2026-03-26 06:23:42 +01:00
|
|
|
match res with Error e -> error e req | Ok content -> ok content req
|
2026-02-23 07:24:40 +01:00
|
|
|
|
|
|
|
|
let result_no_content res req =
|
2026-03-26 06:23:42 +01:00
|
|
|
match res with Error e -> error e req | Ok () -> no_content ()
|