-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiError.ml
139 lines (121 loc) · 3.44 KB
/
apiError.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
(* ************************************************************************** *)
(* Project: Life - the game, Official OCaml SDK *)
(* Author: db0 ([email protected], http://db0.fr/) *)
(* Latest Version is on GitHub: https://github.com/Life-the-game/SDK-OCaml *)
(* ************************************************************************** *)
(* ************************************************************************** *)
(* Type *)
(* ************************************************************************** *)
type detail =
{
dcode : int;
dtype : string;
dmessage : string;
key : string option;
value : string option;
}
type t =
{
message : string;
stype : string;
code : int;
details : detail list;
}
(* ************************************************************************** *)
(* Tools *)
(* ************************************************************************** *)
let _convert_each c name f =
let open Yojson.Basic.Util in
match c |> member name |> to_option (convert_each f) with
| Some l -> l
| None -> []
let detail_from_json c =
let open Yojson.Basic.Util in
{
dcode = c |> member "code" |> to_int;
dtype = c |> member "type" |> to_string;
dmessage = c |> member "message" |> to_string;
key = c |> member "key" |> to_string_option;
value = c |> member "value" |> to_string_option;
}
let from_json c =
let open Yojson.Basic.Util in
{
message = c |> member "message" |> to_string;
stype = c |> member "type" |> to_string;
code = c |> member "code" |> to_int;
details = _convert_each c "details" detail_from_json;
}
(* ************************************************************************** *)
(* Client-side errors *)
(* ************************************************************************** *)
let generic =
{
message = "Something went wrong";
stype = "CLIENT_Error";
code = -1;
details = [];
}
let network msg =
{
message = msg;
stype = "CLIENT_NetworkError";
code = -2;
details = [];
}
let invalid_json msg =
{
message = "The JSON tree response is not formatted as expected: " ^ msg;
stype = "CLIENT_InvalidJSON";
code = -3;
details = [];
}
let requirement_missing =
{
message = "One requirement is missing";
stype = "CLIENT_RequirementMissing";
code = -4;
details = [];
}
let invalid_format =
{
message = "Invalid file format";
stype = "CLIENT_InvalidFileFormat";
code = -5;
details = [];
}
let invalid_argument msg =
{
message = msg;
stype = "CLIENT_InvalidArgument";
code = -6;
details = [];
}
let auth_required =
{
message = "Authentication required";
stype = "CLIENT_AuthenticationRequired";
code = -7;
details = [];
}
let file_not_found =
{
message = "File not found";
stype = "CLIENT_FileNotFound";
code = -8;
details = [];
}
let notfound =
{
message = "Not found";
stype = "NotFound";
code = 1011;
details = [];
}
let redirect url =
{
message = url;
stype = "Redirect";
code = 300;
details = [];
}