Coverage Report

Created: 2026-06-30 06:10

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