/src/WasmEdge/include/host/wasi/error.h
Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2024 Second State INC |
3 | | |
4 | | #pragma once |
5 | | |
6 | | #include "common/expected.h" |
7 | | #include "common/spdlog.h" |
8 | | #include "wasi/api.hpp" |
9 | | #include <string_view> |
10 | | |
11 | | namespace WasmEdge { |
12 | | namespace Host { |
13 | | namespace WASI { |
14 | | |
15 | | /// Type aliasing for Expected<T, __wasi_errno_t>. |
16 | | template <typename T> using WasiExpect = Expected<T, __wasi_errno_t>; |
17 | | |
18 | | /// Helper function for Unexpected<ErrCode>. |
19 | 0 | constexpr auto WasiUnexpect(__wasi_errno_t Val) { |
20 | 0 | return Unexpected<__wasi_errno_t>(Val); |
21 | 0 | } |
22 | 0 | template <typename T> constexpr auto WasiUnexpect(const WasiExpect<T> &Val) { |
23 | 0 | return Unexpected<__wasi_errno_t>(Val.error()); |
24 | 0 | } Unexecuted instantiation: auto WasmEdge::Host::WASI::WasiUnexpect<std::__1::shared_ptr<WasmEdge::Host::WASI::VINode> >(cxx20::expected<std::__1::shared_ptr<WasmEdge::Host::WASI::VINode>, __wasi_errno_t> const&) Unexecuted instantiation: auto WasmEdge::Host::WASI::WasiUnexpect<void>(cxx20::expected<void, __wasi_errno_t> const&) |
25 | | |
26 | | } // namespace WASI |
27 | | } // namespace Host |
28 | | } // namespace WasmEdge |
29 | | |
30 | | template <> |
31 | | struct fmt::formatter<__wasi_errno_t> : fmt::formatter<std::string_view> { |
32 | | fmt::format_context::iterator |
33 | 0 | format(__wasi_errno_t ErrNo, fmt::format_context &Ctx) const noexcept { |
34 | 0 | fmt::memory_buffer Buffer; |
35 | 0 | using namespace std::literals; |
36 | 0 | auto Iter = std::back_inserter(Buffer); |
37 | 0 | switch (ErrNo) { |
38 | 0 | case __WASI_ERRNO_SUCCESS: |
39 | 0 | fmt::format_to( |
40 | 0 | Iter, "No error occurred. System call completed successfully."sv); |
41 | 0 | break; |
42 | 0 | case __WASI_ERRNO_2BIG: |
43 | 0 | fmt::format_to(Iter, "Argument list too long."sv); |
44 | 0 | break; |
45 | 0 | case __WASI_ERRNO_ACCES: |
46 | 0 | fmt::format_to(Iter, "Permission denied."sv); |
47 | 0 | break; |
48 | 0 | case __WASI_ERRNO_ADDRINUSE: |
49 | 0 | fmt::format_to(Iter, "Address in use."sv); |
50 | 0 | break; |
51 | 0 | case __WASI_ERRNO_ADDRNOTAVAIL: |
52 | 0 | fmt::format_to(Iter, "Address not available."sv); |
53 | 0 | break; |
54 | 0 | case __WASI_ERRNO_AFNOSUPPORT: |
55 | 0 | fmt::format_to(Iter, "Address family not supported."sv); |
56 | 0 | break; |
57 | 0 | case __WASI_ERRNO_AGAIN: |
58 | 0 | fmt::format_to(Iter, "Resource unavailable, or operation would block."sv); |
59 | 0 | break; |
60 | 0 | case __WASI_ERRNO_ALREADY: |
61 | 0 | fmt::format_to(Iter, "Connection already in progress."sv); |
62 | 0 | break; |
63 | 0 | case __WASI_ERRNO_BADF: |
64 | 0 | fmt::format_to(Iter, "Bad file descriptor."sv); |
65 | 0 | break; |
66 | 0 | case __WASI_ERRNO_BADMSG: |
67 | 0 | fmt::format_to(Iter, "Bad message."sv); |
68 | 0 | break; |
69 | 0 | case __WASI_ERRNO_BUSY: |
70 | 0 | fmt::format_to(Iter, "Device or resource busy."sv); |
71 | 0 | break; |
72 | 0 | case __WASI_ERRNO_CANCELED: |
73 | 0 | fmt::format_to(Iter, "Operation canceled."sv); |
74 | 0 | break; |
75 | 0 | case __WASI_ERRNO_CHILD: |
76 | 0 | fmt::format_to(Iter, "No child processes."sv); |
77 | 0 | break; |
78 | 0 | case __WASI_ERRNO_CONNABORTED: |
79 | 0 | fmt::format_to(Iter, "Connection aborted."sv); |
80 | 0 | break; |
81 | 0 | case __WASI_ERRNO_CONNREFUSED: |
82 | 0 | fmt::format_to(Iter, "Connection refused."sv); |
83 | 0 | break; |
84 | 0 | case __WASI_ERRNO_CONNRESET: |
85 | 0 | fmt::format_to(Iter, "Connection reset."sv); |
86 | 0 | break; |
87 | 0 | case __WASI_ERRNO_DEADLK: |
88 | 0 | fmt::format_to(Iter, "Resource deadlock would occur."sv); |
89 | 0 | break; |
90 | 0 | case __WASI_ERRNO_DESTADDRREQ: |
91 | 0 | fmt::format_to(Iter, "Destination address required."sv); |
92 | 0 | break; |
93 | 0 | case __WASI_ERRNO_DOM: |
94 | 0 | fmt::format_to(Iter, "Mathematics argument out of domain of function."sv); |
95 | 0 | break; |
96 | 0 | case __WASI_ERRNO_DQUOT: |
97 | 0 | fmt::format_to(Iter, "Reserved."sv); |
98 | 0 | break; |
99 | 0 | case __WASI_ERRNO_EXIST: |
100 | 0 | fmt::format_to(Iter, "File exists."sv); |
101 | 0 | break; |
102 | 0 | case __WASI_ERRNO_FAULT: |
103 | 0 | fmt::format_to(Iter, "Bad address."sv); |
104 | 0 | break; |
105 | 0 | case __WASI_ERRNO_FBIG: |
106 | 0 | fmt::format_to(Iter, "File too large."sv); |
107 | 0 | break; |
108 | 0 | case __WASI_ERRNO_HOSTUNREACH: |
109 | 0 | fmt::format_to(Iter, "Host is unreachable."sv); |
110 | 0 | break; |
111 | 0 | case __WASI_ERRNO_IDRM: |
112 | 0 | fmt::format_to(Iter, "Identifier removed."sv); |
113 | 0 | break; |
114 | 0 | case __WASI_ERRNO_ILSEQ: |
115 | 0 | fmt::format_to(Iter, "Illegal byte sequence."sv); |
116 | 0 | break; |
117 | 0 | case __WASI_ERRNO_INPROGRESS: |
118 | 0 | fmt::format_to(Iter, "Operation in progress."sv); |
119 | 0 | break; |
120 | 0 | case __WASI_ERRNO_INTR: |
121 | 0 | fmt::format_to(Iter, "Interrupted function."sv); |
122 | 0 | break; |
123 | 0 | case __WASI_ERRNO_INVAL: |
124 | 0 | fmt::format_to(Iter, "Invalid argument."sv); |
125 | 0 | break; |
126 | 0 | case __WASI_ERRNO_IO: |
127 | 0 | fmt::format_to(Iter, "I/O error."sv); |
128 | 0 | break; |
129 | 0 | case __WASI_ERRNO_ISCONN: |
130 | 0 | fmt::format_to(Iter, "Socket is connected."sv); |
131 | 0 | break; |
132 | 0 | case __WASI_ERRNO_ISDIR: |
133 | 0 | fmt::format_to(Iter, "Is a directory."sv); |
134 | 0 | break; |
135 | 0 | case __WASI_ERRNO_LOOP: |
136 | 0 | fmt::format_to(Iter, "Too many levels of symbolic links."sv); |
137 | 0 | break; |
138 | 0 | case __WASI_ERRNO_MFILE: |
139 | 0 | fmt::format_to(Iter, "File descriptor value too large."sv); |
140 | 0 | break; |
141 | 0 | case __WASI_ERRNO_MLINK: |
142 | 0 | fmt::format_to(Iter, "Too many links."sv); |
143 | 0 | break; |
144 | 0 | case __WASI_ERRNO_MSGSIZE: |
145 | 0 | fmt::format_to(Iter, "Message too large."sv); |
146 | 0 | break; |
147 | 0 | case __WASI_ERRNO_MULTIHOP: |
148 | 0 | fmt::format_to(Iter, "Reserved."sv); |
149 | 0 | break; |
150 | 0 | case __WASI_ERRNO_NAMETOOLONG: |
151 | 0 | fmt::format_to(Iter, "Filename too long."sv); |
152 | 0 | break; |
153 | 0 | case __WASI_ERRNO_NETDOWN: |
154 | 0 | fmt::format_to(Iter, "Network is down."sv); |
155 | 0 | break; |
156 | 0 | case __WASI_ERRNO_NETRESET: |
157 | 0 | fmt::format_to(Iter, "Connection aborted by network."sv); |
158 | 0 | break; |
159 | 0 | case __WASI_ERRNO_NETUNREACH: |
160 | 0 | fmt::format_to(Iter, "Network unreachable."sv); |
161 | 0 | break; |
162 | 0 | case __WASI_ERRNO_NFILE: |
163 | 0 | fmt::format_to(Iter, "Too many files open in system."sv); |
164 | 0 | break; |
165 | 0 | case __WASI_ERRNO_NOBUFS: |
166 | 0 | fmt::format_to(Iter, "No buffer space available."sv); |
167 | 0 | break; |
168 | 0 | case __WASI_ERRNO_NODEV: |
169 | 0 | fmt::format_to(Iter, "No such device."sv); |
170 | 0 | break; |
171 | 0 | case __WASI_ERRNO_NOENT: |
172 | 0 | fmt::format_to(Iter, "No such file or directory."sv); |
173 | 0 | break; |
174 | 0 | case __WASI_ERRNO_NOEXEC: |
175 | 0 | fmt::format_to(Iter, "Executable file format error."sv); |
176 | 0 | break; |
177 | 0 | case __WASI_ERRNO_NOLCK: |
178 | 0 | fmt::format_to(Iter, "No locks available."sv); |
179 | 0 | break; |
180 | 0 | case __WASI_ERRNO_NOLINK: |
181 | 0 | fmt::format_to(Iter, "Reserved."sv); |
182 | 0 | break; |
183 | 0 | case __WASI_ERRNO_NOMEM: |
184 | 0 | fmt::format_to(Iter, "Not enough space."sv); |
185 | 0 | break; |
186 | 0 | case __WASI_ERRNO_NOMSG: |
187 | 0 | fmt::format_to(Iter, "No message of the desired type."sv); |
188 | 0 | break; |
189 | 0 | case __WASI_ERRNO_NOPROTOOPT: |
190 | 0 | fmt::format_to(Iter, "Protocol not available."sv); |
191 | 0 | break; |
192 | 0 | case __WASI_ERRNO_NOSPC: |
193 | 0 | fmt::format_to(Iter, "No space left on device."sv); |
194 | 0 | break; |
195 | 0 | case __WASI_ERRNO_NOSYS: |
196 | 0 | fmt::format_to(Iter, "Function not supported."sv); |
197 | 0 | break; |
198 | 0 | case __WASI_ERRNO_NOTCONN: |
199 | 0 | fmt::format_to(Iter, "The socket is not connected."sv); |
200 | 0 | break; |
201 | 0 | case __WASI_ERRNO_NOTDIR: |
202 | 0 | fmt::format_to(Iter, |
203 | 0 | "Not a directory or a symbolic link to a directory."sv); |
204 | 0 | break; |
205 | 0 | case __WASI_ERRNO_NOTEMPTY: |
206 | 0 | fmt::format_to(Iter, "Directory not empty."sv); |
207 | 0 | break; |
208 | 0 | case __WASI_ERRNO_NOTRECOVERABLE: |
209 | 0 | fmt::format_to(Iter, "State not recoverable."sv); |
210 | 0 | break; |
211 | 0 | case __WASI_ERRNO_NOTSOCK: |
212 | 0 | fmt::format_to(Iter, "Not a socket."sv); |
213 | 0 | break; |
214 | 0 | case __WASI_ERRNO_NOTSUP: |
215 | 0 | fmt::format_to(Iter, |
216 | 0 | "Not supported, or operation not supported on socket."sv); |
217 | 0 | break; |
218 | 0 | case __WASI_ERRNO_NOTTY: |
219 | 0 | fmt::format_to(Iter, "Inappropriate I/O control operation."sv); |
220 | 0 | break; |
221 | 0 | case __WASI_ERRNO_NXIO: |
222 | 0 | fmt::format_to(Iter, "No such device or address."sv); |
223 | 0 | break; |
224 | 0 | case __WASI_ERRNO_OVERFLOW: |
225 | 0 | fmt::format_to(Iter, "Value too large to be stored in data type."sv); |
226 | 0 | break; |
227 | 0 | case __WASI_ERRNO_OWNERDEAD: |
228 | 0 | fmt::format_to(Iter, "Previous owner died."sv); |
229 | 0 | break; |
230 | 0 | case __WASI_ERRNO_PERM: |
231 | 0 | fmt::format_to(Iter, "Operation not permitted."sv); |
232 | 0 | break; |
233 | 0 | case __WASI_ERRNO_PIPE: |
234 | 0 | fmt::format_to(Iter, "Broken pipe."sv); |
235 | 0 | break; |
236 | 0 | case __WASI_ERRNO_PROTO: |
237 | 0 | fmt::format_to(Iter, "Protocol error."sv); |
238 | 0 | break; |
239 | 0 | case __WASI_ERRNO_PROTONOSUPPORT: |
240 | 0 | fmt::format_to(Iter, "Protocol not supported."sv); |
241 | 0 | break; |
242 | 0 | case __WASI_ERRNO_PROTOTYPE: |
243 | 0 | fmt::format_to(Iter, "Protocol wrong type for socket."sv); |
244 | 0 | break; |
245 | 0 | case __WASI_ERRNO_RANGE: |
246 | 0 | fmt::format_to(Iter, "Result too large."sv); |
247 | 0 | break; |
248 | 0 | case __WASI_ERRNO_ROFS: |
249 | 0 | fmt::format_to(Iter, "Read-only file system."sv); |
250 | 0 | break; |
251 | 0 | case __WASI_ERRNO_SPIPE: |
252 | 0 | fmt::format_to(Iter, "Invalid seek."sv); |
253 | 0 | break; |
254 | 0 | case __WASI_ERRNO_SRCH: |
255 | 0 | fmt::format_to(Iter, "No such process."sv); |
256 | 0 | break; |
257 | 0 | case __WASI_ERRNO_STALE: |
258 | 0 | fmt::format_to(Iter, "Reserved."sv); |
259 | 0 | break; |
260 | 0 | case __WASI_ERRNO_TIMEDOUT: |
261 | 0 | fmt::format_to(Iter, "Connection timed out."sv); |
262 | 0 | break; |
263 | 0 | case __WASI_ERRNO_TXTBSY: |
264 | 0 | fmt::format_to(Iter, "Text file busy."sv); |
265 | 0 | break; |
266 | 0 | case __WASI_ERRNO_XDEV: |
267 | 0 | fmt::format_to(Iter, "Cross-device link."sv); |
268 | 0 | break; |
269 | 0 | case __WASI_ERRNO_NOTCAPABLE: |
270 | 0 | fmt::format_to(Iter, "Extension: Capabilities insufficient."sv); |
271 | 0 | break; |
272 | 0 | case __WASI_ERRNO_AIADDRFAMILY: |
273 | 0 | fmt::format_to(Iter, |
274 | 0 | "The specified network host does not have any network " |
275 | 0 | "addresses in the requested address family."sv); |
276 | 0 | break; |
277 | 0 | case __WASI_ERRNO_AIAGAIN: |
278 | 0 | fmt::format_to(Iter, "Try again later."sv); |
279 | 0 | break; |
280 | 0 | case __WASI_ERRNO_AIBADFLAG: |
281 | 0 | fmt::format_to(Iter, "Hints.ai_flags contains invalid flags"sv); |
282 | 0 | break; |
283 | 0 | case __WASI_ERRNO_AIFAIL: |
284 | 0 | fmt::format_to( |
285 | 0 | Iter, "The name server returned a permanent failure indication."sv); |
286 | 0 | break; |
287 | 0 | case __WASI_ERRNO_AIFAMILY: |
288 | 0 | fmt::format_to(Iter, "The requested address family is not supported."sv); |
289 | 0 | break; |
290 | 0 | case __WASI_ERRNO_AIMEMORY: |
291 | 0 | fmt::format_to(Iter, "Addrinfo out of memory."sv); |
292 | 0 | break; |
293 | 0 | case __WASI_ERRNO_AINODATA: |
294 | 0 | fmt::format_to(Iter, "Network host exists, but does not have any network " |
295 | 0 | "addresses defined."sv); |
296 | 0 | break; |
297 | 0 | case __WASI_ERRNO_AINONAME: |
298 | 0 | fmt::format_to( |
299 | 0 | Iter, |
300 | 0 | "Node or service is not known; or both node and service are NULL."sv); |
301 | 0 | break; |
302 | 0 | case __WASI_ERRNO_AISERVICE: |
303 | 0 | fmt::format_to( |
304 | 0 | Iter, "Service is not available for the requested socket type."sv); |
305 | 0 | break; |
306 | 0 | case __WASI_ERRNO_AISOCKTYPE: |
307 | 0 | fmt::format_to(Iter, "The requested socket type is not supported."sv); |
308 | 0 | break; |
309 | 0 | case __WASI_ERRNO_AISYSTEM: |
310 | 0 | fmt::format_to(Iter, "Other system error."sv); |
311 | 0 | break; |
312 | 0 | default: |
313 | 0 | fmt::format_to( |
314 | 0 | Iter, "Unknown error code {}"sv, |
315 | 0 | static_cast<std::underlying_type_t<__wasi_errno_t>>(ErrNo)); |
316 | 0 | break; |
317 | 0 | } |
318 | 0 | return fmt::formatter<std::string_view>::format( |
319 | 0 | std::string_view(Buffer.data(), Buffer.size()), Ctx); |
320 | 0 | } |
321 | | }; |