diff --git a/src/unix/error.c b/src/unix/error.c index cd8bb306..5ce2156e 100644 --- a/src/unix/error.c +++ b/src/unix/error.c @@ -56,8 +56,28 @@ void uv_fatal_error(const int errorno, const char* syscall) { } -char* uv_strerror(uv_err_t err) { - return strerror(err.sys_errno_); +static int uv__translate_lib_error(int code) { + switch (code) { + case UV_ENOENT: return ENOENT; + case UV_EACCESS: return EACCES; + case UV_EBADF: return EBADF; + case UV_EPIPE: return EPIPE; + case UV_EAGAIN: return EAGAIN; + case UV_ECONNRESET: return ECONNRESET; + case UV_EFAULT: return EFAULT; + case UV_EMFILE: return EMFILE; + case UV_EMSGSIZE: return EMSGSIZE; + case UV_EINVAL: return EINVAL; + case UV_ECONNREFUSED: return ECONNREFUSED; + case UV_EADDRINUSE: return EADDRINUSE; + case UV_EADDRNOTAVAIL: return EADDRNOTAVAIL; + case UV_ENOTCONN: return ENOTCONN; + case UV_EEXIST: return EEXIST; + default: return -1; + } + + assert(0 && "unreachable"); + return -1; } @@ -85,3 +105,22 @@ uv_err_code uv_translate_sys_error(int sys_errno) { assert(0 && "unreachable"); return -1; } + + +/* TODO Pull in error messages so we don't have to + * a) rely on what the system provides us + * b) reverse-map the error codes + */ +char* uv_strerror(uv_err_t err) { + int errorno; + + if (err.sys_errno_) + errorno = err.sys_errno_; + else + errorno = uv__translate_lib_error(err.code); + + if (errorno == -1) + return "Unknown error"; + else + return strerror(errorno); +} diff --git a/test/test-error.c b/test/test-error.c new file mode 100644 index 00000000..8d6f2355 --- /dev/null +++ b/test/test-error.c @@ -0,0 +1,59 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "uv.h" +#include "task.h" + +#include +#include +#include + + +/* + * Synthetic errors (errors that originate from within libuv, not the system) + * should produce sensible error messages when run through uv_strerror(). + * + * See https://github.com/joyent/libuv/issues/210 + */ +TEST_IMPL(error_message) { + uv_err_t e; + + /* Cop out. Can't do proper checks on systems with + * i18n-ized error messages... + */ + e.code = 0, e.sys_errno_ = 0; + + if (strcmp(uv_strerror(e), "Success") != 0) { + printf("i18n error messages detected, skipping test.\n"); + return 0; + } + + e.code = UV_EINVAL, e.sys_errno_ = 0; + ASSERT(strstr(uv_strerror(e), "Success") == NULL); + + e.code = UV_UNKNOWN, e.sys_errno_ = 0; + ASSERT(strcmp(uv_strerror(e), "Unknown error") == 0); + + e.code = 1337, e.sys_errno_ = 0; + ASSERT(strcmp(uv_strerror(e), "Unknown error") == 0); + + return 0; +} diff --git a/test/test-list.h b/test/test-list.h index f137493b..95224ff4 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -53,6 +53,7 @@ TEST_DECLARE (connection_fail) TEST_DECLARE (connection_fail_doesnt_auto_close) TEST_DECLARE (shutdown_eof) TEST_DECLARE (callback_stack) +TEST_DECLARE (error_message) TEST_DECLARE (timer) TEST_DECLARE (timer_ref) TEST_DECLARE (timer_ref2) @@ -166,6 +167,8 @@ TASK_LIST_START TEST_ENTRY (callback_stack) TEST_HELPER (callback_stack, tcp4_echo_server) + TEST_ENTRY (error_message) + TEST_ENTRY (timer) TEST_ENTRY (timer_ref) TEST_ENTRY (timer_ref2) diff --git a/uv.gyp b/uv.gyp index e85927aa..43d21ff9 100644 --- a/uv.gyp +++ b/uv.gyp @@ -247,6 +247,7 @@ 'test/test-get-loadavg.c', 'test/task.h', 'test/test-async.c', + 'test/test-error.c', 'test/test-callback-stack.c', 'test/test-connection-fail.c', 'test/test-delayed-accept.c',