aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/gnunet_error_codes.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/gnunet_error_codes.c')
-rw-r--r--src/lib/util/gnunet_error_codes.c262
1 files changed, 262 insertions, 0 deletions
diff --git a/src/lib/util/gnunet_error_codes.c b/src/lib/util/gnunet_error_codes.c
new file mode 100644
index 000000000..11ce2d0c8
--- /dev/null
+++ b/src/lib/util/gnunet_error_codes.c
@@ -0,0 +1,262 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2012-2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20#include "gnunet_error_codes.h"
21#include <stddef.h>
22#include <microhttpd.h>
23#include <gettext.h>
24
25/**
26 * MHD does not define our value for 0 (client-side generated code).
27 */
28#define MHD_HTTP_UNINITIALIZED 0
29
30/**
31 * A pair containing an error code and its hint.
32 */
33struct ErrorCodeAndHint
34{
35 /**
36 * The error code.
37 */
38 enum GNUNET_ErrorCode ec;
39
40 /**
41 * The hint.
42 */
43 const char *hint;
44
45 /**
46 * The HTTP status code.
47 */
48 unsigned int http_code;
49};
50
51
52/**
53 * The list of all error codes with their hints.
54 */
55static const struct ErrorCodeAndHint code_hint_pairs[] = {
56
57 {
58 .ec = GNUNET_EC_NONE,
59 .hint = gettext_noop ("No error (success)."),
60 .http_code = MHD_HTTP_UNINITIALIZED
61 },
62
63 {
64 .ec = GNUNET_EC_UNKNOWN,
65 .hint = gettext_noop ("Unknown and unspecified error."),
66 .http_code = MHD_HTTP_INTERNAL_SERVER_ERROR
67 },
68
69 {
70 .ec = GNUNET_EC_SERVICE_COMMUNICATION_FAILED,
71 .hint = gettext_noop ("Communication with service failed."),
72 .http_code = MHD_HTTP_INTERNAL_SERVER_ERROR
73 },
74
75 {
76 .ec = GNUNET_EC_IDENTITY_NOT_FOUND,
77 .hint = gettext_noop ("Ego not found."),
78 .http_code = MHD_HTTP_NOT_FOUND
79 },
80
81 {
82 .ec = GNUNET_EC_IDENTITY_NAME_CONFLICT,
83 .hint = gettext_noop ("Identifier already in use for another ego."),
84 .http_code = MHD_HTTP_CONFLICT
85 },
86
87 {
88 .ec = GNUNET_EC_IDENTITY_INVALID,
89 .hint = gettext_noop ("The given ego is invalid or malformed."),
90 .http_code = MHD_HTTP_INTERNAL_SERVER_ERROR
91 },
92
93 {
94 .ec = GNUNET_EC_NAMESTORE_UNKNOWN,
95 .hint = gettext_noop ("Unknown namestore error."),
96 .http_code = MHD_HTTP_INTERNAL_SERVER_ERROR
97 },
98
99 {
100 .ec = GNUNET_EC_NAMESTORE_ITERATION_FAILED,
101 .hint = gettext_noop ("Zone iteration failed."),
102 .http_code = MHD_HTTP_INTERNAL_SERVER_ERROR
103 },
104
105 {
106 .ec = GNUNET_EC_NAMESTORE_ZONE_NOT_FOUND,
107 .hint = gettext_noop ("Zone not found."),
108 .http_code = MHD_HTTP_NOT_FOUND
109 },
110
111 {
112 .ec = GNUNET_EC_NAMESTORE_RECORD_NOT_FOUND,
113 .hint = gettext_noop ("Record not found."),
114 .http_code = MHD_HTTP_NOT_FOUND
115 },
116
117 {
118 .ec = GNUNET_EC_NAMESTORE_RECORD_DELETE_FAILED,
119 .hint = gettext_noop ("Zone iteration failed."),
120 .http_code = MHD_HTTP_INTERNAL_SERVER_ERROR
121 },
122
123 {
124 .ec = GNUNET_EC_NAMESTORE_ZONE_EMPTY,
125 .hint = gettext_noop ("Zone does not contain any records."),
126 .http_code = MHD_HTTP_NOT_FOUND
127 },
128
129 {
130 .ec = GNUNET_EC_NAMESTORE_LOOKUP_ERROR,
131 .hint = gettext_noop ("Failed to lookup record."),
132 .http_code = MHD_HTTP_INTERNAL_SERVER_ERROR
133 },
134
135 {
136 .ec = GNUNET_EC_NAMESTORE_NO_RECORDS_GIVEN,
137 .hint = gettext_noop ("No records given."),
138 .http_code = MHD_HTTP_BAD_REQUEST
139 },
140
141 {
142 .ec = GNUNET_EC_NAMESTORE_RECORD_DATA_INVALID,
143 .hint = gettext_noop ("Record data invalid."),
144 .http_code = MHD_HTTP_BAD_REQUEST
145 },
146
147 {
148 .ec = GNUNET_EC_NAMESTORE_NO_LABEL_GIVEN,
149 .hint = gettext_noop ("No label given."),
150 .http_code = MHD_HTTP_BAD_REQUEST
151 },
152
153 {
154 .ec = GNUNET_EC_NAMESTORE_NO_RESULTS,
155 .hint = gettext_noop ("No results given."),
156 .http_code = MHD_HTTP_NOT_FOUND
157 },
158
159 {
160 .ec = GNUNET_EC_NAMESTORE_RECORD_EXISTS,
161 .hint = gettext_noop ("Record already exists."),
162 .http_code = MHD_HTTP_CONFLICT
163 },
164
165 {
166 .ec = GNUNET_EC_NAMESTORE_RECORD_TOO_BIG,
167 .hint = gettext_noop ("Record size exceeds maximum limit."),
168 .http_code = MHD_HTTP_INTERNAL_SERVER_ERROR
169 },
170
171 {
172 .ec = GNUNET_EC_NAMESTORE_BACKEND_FAILED,
173 .hint = gettext_noop ("There was an error in the database backend."),
174 .http_code = MHD_HTTP_INTERNAL_SERVER_ERROR
175 },
176
177 {
178 .ec = GNUNET_EC_NAMESTORE_STORE_FAILED,
179 .hint = gettext_noop ("Failed to store the given records."),
180 .http_code = MHD_HTTP_INTERNAL_SERVER_ERROR
181 },
182
183 {
184 .ec = GNUNET_EC_NAMESTORE_LABEL_INVALID,
185 .hint = gettext_noop ("Label invalid or malformed."),
186 .http_code = MHD_HTTP_BAD_REQUEST
187 },
188
189
190};
191
192
193/**
194 * The length of @e code_hint_pairs.
195 */
196static const unsigned int code_hint_pairs_length = 22;
197
198
199
200const char *
201GNUNET_ErrorCode_get_hint (enum GNUNET_ErrorCode ec)
202{
203 unsigned int lower = 0;
204 unsigned int upper = code_hint_pairs_length - 1;
205 unsigned int mid = upper / 2;
206 while (lower <= upper)
207 {
208 mid = (upper + lower) / 2;
209 if (code_hint_pairs[mid].ec < ec)
210 {
211 lower = mid + 1;
212 }
213 else if (code_hint_pairs[mid].ec > ec)
214 {
215 upper = mid - 1;
216 }
217 else
218 {
219 return code_hint_pairs[mid].hint;
220 }
221 }
222 return "<no hint found>";
223}
224
225
226unsigned int
227GNUNET_ErrorCode_get_http_status (enum GNUNET_ErrorCode ec)
228{
229 unsigned int lower = 0;
230 unsigned int upper = code_hint_pairs_length - 1;
231 unsigned int mid = upper / 2;
232 while (lower <= upper)
233 {
234 mid = (upper + lower) / 2;
235 if (code_hint_pairs[mid].ec < ec)
236 {
237 lower = mid + 1;
238 }
239 else if (code_hint_pairs[mid].ec > ec)
240 {
241 upper = mid - 1;
242 }
243 else
244 {
245 return code_hint_pairs[mid].http_code;
246 }
247 }
248 return UINT_MAX;
249}
250
251
252unsigned int
253GNUNET_ErrorCode_get_http_status_safe (enum GNUNET_ErrorCode ec)
254{
255 unsigned int hc;
256
257 hc = GNUNET_ErrorCode_get_http_status (ec);
258 if ( (0 == hc) ||
259 (UINT_MAX == hc) )
260 return MHD_HTTP_INTERNAL_SERVER_ERROR;
261 return hc;
262}