Skip to content

Commit

Permalink
add an embedder API to stringify error codes
Browse files Browse the repository at this point in the history
This commit adds uvwasi_embedder_err_code_to_string(), which
converts a uvwasi error code into a string representation of
the error code.
  • Loading branch information
cjihrig committed Dec 14, 2019
1 parent db9719e commit 447676a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/uvwasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ void uvwasi_destroy(uvwasi_t* uvwasi);
uvwasi_errno_t uvwasi_embedder_remap_fd(uvwasi_t* uvwasi,
const uvwasi_fd_t fd,
uv_file new_host_fd);
const char* uvwasi_embedder_err_code_to_string(uvwasi_errno_t code);


// WASI system call API.
Expand Down
87 changes: 87 additions & 0 deletions src/uvwasi.c
Original file line number Diff line number Diff line change
Expand Up @@ -2150,3 +2150,90 @@ uvwasi_errno_t uvwasi_sock_shutdown(uvwasi_t* uvwasi,
https://github.com/WebAssembly/WASI/issues/4 */
return UVWASI_ENOTSUP;
}


const char* uvwasi_embedder_err_code_to_string(uvwasi_errno_t code) {
switch (code) {
#define V(errcode) case errcode: return #errcode;
V(UVWASI_E2BIG)
V(UVWASI_EACCES)
V(UVWASI_EADDRINUSE)
V(UVWASI_EADDRNOTAVAIL)
V(UVWASI_EAFNOSUPPORT)
V(UVWASI_EAGAIN)
V(UVWASI_EALREADY)
V(UVWASI_EBADF)
V(UVWASI_EBADMSG)
V(UVWASI_EBUSY)
V(UVWASI_ECANCELED)
V(UVWASI_ECHILD)
V(UVWASI_ECONNABORTED)
V(UVWASI_ECONNREFUSED)
V(UVWASI_ECONNRESET)
V(UVWASI_EDEADLK)
V(UVWASI_EDESTADDRREQ)
V(UVWASI_EDOM)
V(UVWASI_EDQUOT)
V(UVWASI_EEXIST)
V(UVWASI_EFAULT)
V(UVWASI_EFBIG)
V(UVWASI_EHOSTUNREACH)
V(UVWASI_EIDRM)
V(UVWASI_EILSEQ)
V(UVWASI_EINPROGRESS)
V(UVWASI_EINTR)
V(UVWASI_EINVAL)
V(UVWASI_EIO)
V(UVWASI_EISCONN)
V(UVWASI_EISDIR)
V(UVWASI_ELOOP)
V(UVWASI_EMFILE)
V(UVWASI_EMLINK)
V(UVWASI_EMSGSIZE)
V(UVWASI_EMULTIHOP)
V(UVWASI_ENAMETOOLONG)
V(UVWASI_ENETDOWN)
V(UVWASI_ENETRESET)
V(UVWASI_ENETUNREACH)
V(UVWASI_ENFILE)
V(UVWASI_ENOBUFS)
V(UVWASI_ENODEV)
V(UVWASI_ENOENT)
V(UVWASI_ENOEXEC)
V(UVWASI_ENOLCK)
V(UVWASI_ENOLINK)
V(UVWASI_ENOMEM)
V(UVWASI_ENOMSG)
V(UVWASI_ENOPROTOOPT)
V(UVWASI_ENOSPC)
V(UVWASI_ENOSYS)
V(UVWASI_ENOTCONN)
V(UVWASI_ENOTDIR)
V(UVWASI_ENOTEMPTY)
V(UVWASI_ENOTRECOVERABLE)
V(UVWASI_ENOTSOCK)
V(UVWASI_ENOTSUP)
V(UVWASI_ENOTTY)
V(UVWASI_ENXIO)
V(UVWASI_EOVERFLOW)
V(UVWASI_EOWNERDEAD)
V(UVWASI_EPERM)
V(UVWASI_EPIPE)
V(UVWASI_EPROTO)
V(UVWASI_EPROTONOSUPPORT)
V(UVWASI_EPROTOTYPE)
V(UVWASI_ERANGE)
V(UVWASI_EROFS)
V(UVWASI_ESPIPE)
V(UVWASI_ESRCH)
V(UVWASI_ESTALE)
V(UVWASI_ETIMEDOUT)
V(UVWASI_ETXTBSY)
V(UVWASI_EXDEV)
V(UVWASI_ENOTCAPABLE)
V(UVWASI_ESUCCESS)
#undef V
default:
return "UVWASI_UNKNOWN_ERROR";
}
}
21 changes: 21 additions & 0 deletions test/test-err-to-string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <assert.h>
#include <string.h>
#include "uvwasi.h"

int main(void) {
/* Verify that a few uvwasi_embedder_err_code_to_string() calls work. */
assert(0 == strcmp(uvwasi_embedder_err_code_to_string(UVWASI_E2BIG),
"UVWASI_E2BIG"));
assert(0 == strcmp(uvwasi_embedder_err_code_to_string(UVWASI_EBADF),
"UVWASI_EBADF"));
assert(0 == strcmp(uvwasi_embedder_err_code_to_string(UVWASI_ENOENT),
"UVWASI_ENOENT"));
assert(0 == strcmp(uvwasi_embedder_err_code_to_string(UVWASI_ENOTCAPABLE),
"UVWASI_ENOTCAPABLE"));
assert(0 == strcmp(uvwasi_embedder_err_code_to_string(UVWASI_ESUCCESS),
"UVWASI_ESUCCESS"));
assert(0 == strcmp(uvwasi_embedder_err_code_to_string(34000),
"UVWASI_UNKNOWN_ERROR"));

return 0;
}

0 comments on commit 447676a

Please sign in to comment.