51 lines
1.4 KiB
OCaml
51 lines
1.4 KiB
OCaml
let encode_error_detail err =
|
|
match Api.(encode ErrorDetail.jsont err) with
|
|
| Error e -> Fmt.failwith "json encoding error on `ErrorDetail`: %s." e
|
|
| Ok s -> s
|
|
|
|
let respond_json req content status =
|
|
let open Vifu.Response in
|
|
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
|
|
encode_error_detail err
|
|
|
|
let error ~hint req =
|
|
Logs.err (fun m -> m "error: %s" hint);
|
|
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
|
|
|
|
let no_content () =
|
|
Logs.debug (fun m -> m "no content");
|
|
let open Vifu.Response in
|
|
let open Syntax in
|
|
let* () = empty in
|
|
respond `No_content
|
|
|
|
let not_modified () =
|
|
Logs.debug (fun m -> m "not modified");
|
|
let open Vifu.Response in
|
|
let open Syntax in
|
|
let* () = empty in
|
|
respond `Not_modified
|
|
|
|
let result res req =
|
|
match res with Error hint -> error ~hint req | Ok content -> ok content req
|
|
|
|
let result_no_content res req =
|
|
match res with Error hint -> error ~hint req | Ok () -> no_content ()
|