Skip to content
This repository
Browse code

error reporting: return sensible messages for synthetic errors

  • Loading branch information...
commit c7870465aa86a3d0942c1d42c5ce930c696e1830 1 parent 0303197
Ben Noordhuis authored October 06, 2011
43  src/unix/error.c
@@ -56,8 +56,28 @@ void uv_fatal_error(const int errorno, const char* syscall) {
56 56
 }
57 57
 
58 58
 
59  
-char* uv_strerror(uv_err_t err) {
60  
-  return strerror(err.sys_errno_);
  59
+static int uv__translate_lib_error(int code) {
  60
+  switch (code) {
  61
+    case UV_ENOENT: return ENOENT;
  62
+    case UV_EACCESS: return EACCES;
  63
+    case UV_EBADF: return EBADF;
  64
+    case UV_EPIPE: return EPIPE;
  65
+    case UV_EAGAIN: return EAGAIN;
  66
+    case UV_ECONNRESET: return ECONNRESET;
  67
+    case UV_EFAULT: return EFAULT;
  68
+    case UV_EMFILE: return EMFILE;
  69
+    case UV_EMSGSIZE: return EMSGSIZE;
  70
+    case UV_EINVAL: return EINVAL;
  71
+    case UV_ECONNREFUSED: return ECONNREFUSED;
  72
+    case UV_EADDRINUSE: return EADDRINUSE;
  73
+    case UV_EADDRNOTAVAIL: return EADDRNOTAVAIL;
  74
+    case UV_ENOTCONN: return ENOTCONN;
  75
+    case UV_EEXIST: return EEXIST;
  76
+    default: return -1;
  77
+  }
  78
+
  79
+  assert(0 && "unreachable");
  80
+  return -1;
61 81
 }
62 82
 
63 83
 
@@ -85,3 +105,22 @@ uv_err_code uv_translate_sys_error(int sys_errno) {
85 105
   assert(0 && "unreachable");
86 106
   return -1;
87 107
 }
  108
+
  109
+
  110
+/* TODO Pull in error messages so we don't have to
  111
+ *  a) rely on what the system provides us
  112
+ *  b) reverse-map the error codes
  113
+ */
  114
+char* uv_strerror(uv_err_t err) {
  115
+  int errorno;
  116
+
  117
+  if (err.sys_errno_)
  118
+    errorno = err.sys_errno_;
  119
+  else
  120
+    errorno = uv__translate_lib_error(err.code);
  121
+
  122
+  if (errorno == -1)
  123
+    return "Unknown error";
  124
+  else
  125
+    return strerror(errorno);
  126
+}
59  test/test-error.c
... ...
@@ -0,0 +1,59 @@
  1
+/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2
+ *
  3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
  4
+ * of this software and associated documentation files (the "Software"), to
  5
+ * deal in the Software without restriction, including without limitation the
  6
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7
+ * sell copies of the Software, and to permit persons to whom the Software is
  8
+ * furnished to do so, subject to the following conditions:
  9
+ *
  10
+ * The above copyright notice and this permission notice shall be included in
  11
+ * all copies or substantial portions of the Software.
  12
+ *
  13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19
+ * IN THE SOFTWARE.
  20
+ */
  21
+
  22
+#include "uv.h"
  23
+#include "task.h"
  24
+
  25
+#include <stdio.h>
  26
+#include <stdlib.h>
  27
+#include <string.h>
  28
+
  29
+
  30
+/*
  31
+ * Synthetic errors (errors that originate from within libuv, not the system)
  32
+ * should produce sensible error messages when run through uv_strerror().
  33
+ *
  34
+ * See https://github.com/joyent/libuv/issues/210
  35
+ */
  36
+TEST_IMPL(error_message) {
  37
+  uv_err_t e;
  38
+
  39
+  /* Cop out. Can't do proper checks on systems with
  40
+   * i18n-ized error messages...
  41
+   */
  42
+  e.code = 0, e.sys_errno_ = 0;
  43
+
  44
+  if (strcmp(uv_strerror(e), "Success") != 0) {
  45
+    printf("i18n error messages detected, skipping test.\n");
  46
+    return 0;
  47
+  }
  48
+
  49
+  e.code = UV_EINVAL, e.sys_errno_ = 0;
  50
+  ASSERT(strstr(uv_strerror(e), "Success") == NULL);
  51
+
  52
+  e.code = UV_UNKNOWN, e.sys_errno_ = 0;
  53
+  ASSERT(strcmp(uv_strerror(e), "Unknown error") == 0);
  54
+
  55
+  e.code = 1337, e.sys_errno_ = 0;
  56
+  ASSERT(strcmp(uv_strerror(e), "Unknown error") == 0);
  57
+
  58
+  return 0;
  59
+}
3  test/test-list.h
@@ -53,6 +53,7 @@ TEST_DECLARE   (connection_fail)
53 53
 TEST_DECLARE   (connection_fail_doesnt_auto_close)
54 54
 TEST_DECLARE   (shutdown_eof)
55 55
 TEST_DECLARE   (callback_stack)
  56
+TEST_DECLARE   (error_message)
56 57
 TEST_DECLARE   (timer)
57 58
 TEST_DECLARE   (timer_ref)
58 59
 TEST_DECLARE   (timer_ref2)
@@ -166,6 +167,8 @@ TASK_LIST_START
166 167
   TEST_ENTRY  (callback_stack)
167 168
   TEST_HELPER (callback_stack, tcp4_echo_server)
168 169
 
  170
+  TEST_ENTRY  (error_message)
  171
+
169 172
   TEST_ENTRY  (timer)
170 173
   TEST_ENTRY  (timer_ref)
171 174
   TEST_ENTRY  (timer_ref2)
1  uv.gyp
@@ -247,6 +247,7 @@
247 247
         'test/test-get-loadavg.c',
248 248
         'test/task.h',
249 249
         'test/test-async.c',
  250
+        'test/test-error.c',
250 251
         'test/test-callback-stack.c',
251 252
         'test/test-connection-fail.c',
252 253
         'test/test-delayed-accept.c',

0 notes on commit c787046

Please sign in to comment.
Something went wrong with that request. Please try again.