error.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #include <assert.h>
  22. #include <errno.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include "uv.h"
  27. #include "internal.h"
  28. /*
  29. * Display an error message and abort the event loop.
  30. */
  31. void uv_fatal_error(const int errorno, const char* syscall) {
  32. char* buf = NULL;
  33. const char* errmsg;
  34. FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  35. FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
  36. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buf, 0, NULL);
  37. if (buf) {
  38. errmsg = buf;
  39. } else {
  40. errmsg = "Unknown error";
  41. }
  42. /* FormatMessage messages include a newline character already, so don't add
  43. * another. */
  44. if (syscall) {
  45. fprintf(stderr, "%s: (%d) %s", syscall, errorno, errmsg);
  46. } else {
  47. fprintf(stderr, "(%d) %s", errorno, errmsg);
  48. }
  49. if (buf) {
  50. LocalFree(buf);
  51. }
  52. DebugBreak();
  53. abort();
  54. }
  55. int uv_translate_sys_error(int sys_errno) {
  56. if (sys_errno <= 0) {
  57. return sys_errno; /* If < 0 then it's already a libuv error. */
  58. }
  59. switch (sys_errno) {
  60. case ERROR_NOACCESS: return UV_EACCES;
  61. case WSAEACCES: return UV_EACCES;
  62. case ERROR_ELEVATION_REQUIRED: return UV_EACCES;
  63. case ERROR_ADDRESS_ALREADY_ASSOCIATED: return UV_EADDRINUSE;
  64. case WSAEADDRINUSE: return UV_EADDRINUSE;
  65. case WSAEADDRNOTAVAIL: return UV_EADDRNOTAVAIL;
  66. case WSAEAFNOSUPPORT: return UV_EAFNOSUPPORT;
  67. case WSAEWOULDBLOCK: return UV_EAGAIN;
  68. case WSAEALREADY: return UV_EALREADY;
  69. case ERROR_INVALID_FLAGS: return UV_EBADF;
  70. case ERROR_INVALID_HANDLE: return UV_EBADF;
  71. case ERROR_LOCK_VIOLATION: return UV_EBUSY;
  72. case ERROR_PIPE_BUSY: return UV_EBUSY;
  73. case ERROR_SHARING_VIOLATION: return UV_EBUSY;
  74. case ERROR_OPERATION_ABORTED: return UV_ECANCELED;
  75. case WSAEINTR: return UV_ECANCELED;
  76. case ERROR_NO_UNICODE_TRANSLATION: return UV_ECHARSET;
  77. case ERROR_CONNECTION_ABORTED: return UV_ECONNABORTED;
  78. case WSAECONNABORTED: return UV_ECONNABORTED;
  79. case ERROR_CONNECTION_REFUSED: return UV_ECONNREFUSED;
  80. case WSAECONNREFUSED: return UV_ECONNREFUSED;
  81. case ERROR_NETNAME_DELETED: return UV_ECONNRESET;
  82. case WSAECONNRESET: return UV_ECONNRESET;
  83. case ERROR_ALREADY_EXISTS: return UV_EEXIST;
  84. case ERROR_FILE_EXISTS: return UV_EEXIST;
  85. case ERROR_BUFFER_OVERFLOW: return UV_EFAULT;
  86. case WSAEFAULT: return UV_EFAULT;
  87. case ERROR_HOST_UNREACHABLE: return UV_EHOSTUNREACH;
  88. case WSAEHOSTUNREACH: return UV_EHOSTUNREACH;
  89. case ERROR_INSUFFICIENT_BUFFER: return UV_EINVAL;
  90. case ERROR_INVALID_DATA: return UV_EINVAL;
  91. case ERROR_INVALID_PARAMETER: return UV_EINVAL;
  92. case ERROR_SYMLINK_NOT_SUPPORTED: return UV_EINVAL;
  93. case WSAEINVAL: return UV_EINVAL;
  94. case WSAEPFNOSUPPORT: return UV_EINVAL;
  95. case WSAESOCKTNOSUPPORT: return UV_EINVAL;
  96. case ERROR_BEGINNING_OF_MEDIA: return UV_EIO;
  97. case ERROR_BUS_RESET: return UV_EIO;
  98. case ERROR_CRC: return UV_EIO;
  99. case ERROR_DEVICE_DOOR_OPEN: return UV_EIO;
  100. case ERROR_DEVICE_REQUIRES_CLEANING: return UV_EIO;
  101. case ERROR_DISK_CORRUPT: return UV_EIO;
  102. case ERROR_EOM_OVERFLOW: return UV_EIO;
  103. case ERROR_FILEMARK_DETECTED: return UV_EIO;
  104. case ERROR_GEN_FAILURE: return UV_EIO;
  105. case ERROR_INVALID_BLOCK_LENGTH: return UV_EIO;
  106. case ERROR_IO_DEVICE: return UV_EIO;
  107. case ERROR_NO_DATA_DETECTED: return UV_EIO;
  108. case ERROR_NO_SIGNAL_SENT: return UV_EIO;
  109. case ERROR_OPEN_FAILED: return UV_EIO;
  110. case ERROR_SETMARK_DETECTED: return UV_EIO;
  111. case ERROR_SIGNAL_REFUSED: return UV_EIO;
  112. case WSAEISCONN: return UV_EISCONN;
  113. case ERROR_CANT_RESOLVE_FILENAME: return UV_ELOOP;
  114. case ERROR_TOO_MANY_OPEN_FILES: return UV_EMFILE;
  115. case WSAEMFILE: return UV_EMFILE;
  116. case WSAEMSGSIZE: return UV_EMSGSIZE;
  117. case ERROR_FILENAME_EXCED_RANGE: return UV_ENAMETOOLONG;
  118. case ERROR_NETWORK_UNREACHABLE: return UV_ENETUNREACH;
  119. case WSAENETUNREACH: return UV_ENETUNREACH;
  120. case WSAENOBUFS: return UV_ENOBUFS;
  121. case ERROR_BAD_PATHNAME: return UV_ENOENT;
  122. case ERROR_DIRECTORY: return UV_ENOENT;
  123. case ERROR_FILE_NOT_FOUND: return UV_ENOENT;
  124. case ERROR_INVALID_NAME: return UV_ENOENT;
  125. case ERROR_INVALID_DRIVE: return UV_ENOENT;
  126. case ERROR_INVALID_REPARSE_DATA: return UV_ENOENT;
  127. case ERROR_MOD_NOT_FOUND: return UV_ENOENT;
  128. case ERROR_PATH_NOT_FOUND: return UV_ENOENT;
  129. case WSAHOST_NOT_FOUND: return UV_ENOENT;
  130. case WSANO_DATA: return UV_ENOENT;
  131. case ERROR_NOT_ENOUGH_MEMORY: return UV_ENOMEM;
  132. case ERROR_OUTOFMEMORY: return UV_ENOMEM;
  133. case ERROR_CANNOT_MAKE: return UV_ENOSPC;
  134. case ERROR_DISK_FULL: return UV_ENOSPC;
  135. case ERROR_EA_TABLE_FULL: return UV_ENOSPC;
  136. case ERROR_END_OF_MEDIA: return UV_ENOSPC;
  137. case ERROR_HANDLE_DISK_FULL: return UV_ENOSPC;
  138. case ERROR_NOT_CONNECTED: return UV_ENOTCONN;
  139. case WSAENOTCONN: return UV_ENOTCONN;
  140. case ERROR_DIR_NOT_EMPTY: return UV_ENOTEMPTY;
  141. case WSAENOTSOCK: return UV_ENOTSOCK;
  142. case ERROR_NOT_SUPPORTED: return UV_ENOTSUP;
  143. case ERROR_BROKEN_PIPE: return UV_EOF;
  144. case ERROR_ACCESS_DENIED: return UV_EPERM;
  145. case ERROR_PRIVILEGE_NOT_HELD: return UV_EPERM;
  146. case ERROR_BAD_PIPE: return UV_EPIPE;
  147. case ERROR_NO_DATA: return UV_EPIPE;
  148. case ERROR_PIPE_NOT_CONNECTED: return UV_EPIPE;
  149. case WSAESHUTDOWN: return UV_EPIPE;
  150. case WSAEPROTONOSUPPORT: return UV_EPROTONOSUPPORT;
  151. case ERROR_WRITE_PROTECT: return UV_EROFS;
  152. case ERROR_SEM_TIMEOUT: return UV_ETIMEDOUT;
  153. case WSAETIMEDOUT: return UV_ETIMEDOUT;
  154. case ERROR_NOT_SAME_DEVICE: return UV_EXDEV;
  155. case ERROR_INVALID_FUNCTION: return UV_EISDIR;
  156. case ERROR_META_EXPANSION_TOO_LONG: return UV_E2BIG;
  157. default: return UV_UNKNOWN;
  158. }
  159. }