Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
error reporting: return sensible messages for synthetic errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Oct 5, 2011
1 parent 0303197 commit c787046
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/unix/error.c
Expand Up @@ -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;
}


Expand Down Expand Up @@ -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);
}
59 changes: 59 additions & 0 deletions 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 <stdio.h>
#include <stdlib.h>
#include <string.h>


/*
* 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;
}
3 changes: 3 additions & 0 deletions test/test-list.h
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions uv.gyp
Expand Up @@ -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',
Expand Down

0 comments on commit c787046

Please sign in to comment.