package ez_api

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file answer.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(**************************************************************************)
(*                                                                        *)
(*                 Copyright 2018-2023 OCamlPro                           *)
(*                                                                        *)
(*  All rights reserved. This file is distributed under the terms of the  *)
(*  GNU Lesser General Public License version 2.1, with the special       *)
(*  exception on linking described in the file LICENSE.                   *)
(*                                                                        *)
(**************************************************************************)

type 'a t = {
  code : int;
  body : 'a;
  headers : (string * string) list;
}

let return ?(code=200) ?(headers=[]) body = Lwt.return {code; body; headers}

let not_found () = return ~code:404 ""

let headers = [ "content-type", "application/json" ]

let cannot_parse (descr, msg, path) =
  let body = EzEncoding.construct Json_encoding.any_ezjson_value @@
    `O [ "error", `String ("Cannot parse path argument " ^ descr.EzAPI.Arg.name);
         "path", `String (String.concat "/" path);
         "msg", `String msg ] in
  return ~code:400 ~headers body

let method_not_allowed () = return ~code:405 ""

let destruct_exception exn =
  let body = EzEncoding.construct Json_encoding.any_ezjson_value @@
    `O [ "error", `String "Destruct exception";
         "exception", `String (Printexc.to_string exn) ] in
  return ~code:400 ~headers body

let unsupported_media_type c =
  let c = match c with None -> "none" | Some c -> c in
  let body = EzEncoding.construct Json_encoding.any_ezjson_value @@
    `O [ "error", `String "Unsupported Media Type";
         "content_type", `String c ] in
  return ~code:415 ~headers body

let server_error exn =
  let body = EzEncoding.construct Json_encoding.any_ezjson_value @@
    `O [ "error", `String "Server Error";
         "msg", `String (Printexc.to_string exn) ] in
  return ~code:500 ~headers body
OCaml

Innovation. Community. Security.