Skip to content
This repository
Browse code

Improve error reporting on windows

  • Loading branch information...
commit 2ce0961de181b5ccb0ff66ff3f0e1c6c417dda12 1 parent 0eb4c21
Bert Belder piscisaureus authored
8 src/node.cc
@@ -1004,7 +1004,13 @@ Local<Value> ErrnoException(int errorno,
1004 1004 const char *path) {
1005 1005 Local<Value> e;
1006 1006 Local<String> estring = String::NewSymbol(errno_string(errorno));
1007   - if (!msg[0]) msg = strerror(errorno);
  1007 + if (!msg[0]) {
  1008 +#ifdef __POSIX__
  1009 + msg = strerror(errorno);
  1010 +#else // __MINGW32__
  1011 + msg = winapi_strerror(errorno);
  1012 +#endif
  1013 + }
1008 1014 Local<String> message = String::NewSymbol(msg);
1009 1015
1010 1016 Local<String> cons1 = String::Concat(estring, String::NewSymbol(", "));
35 src/platform_win32.cc
@@ -20,13 +20,42 @@ using namespace v8;
20 20 static char *process_title = NULL;
21 21
22 22
  23 +// Does the about the same as strerror(),
  24 +// but supports all windows errror messages
  25 +const char *winapi_strerror(const int errorno) {
  26 + char *errmsg = NULL;
  27 +
  28 + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  29 + FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
  30 + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
  31 +
  32 + if (errmsg) {
  33 + // Remove trailing newlines
  34 + for (int i = strlen(errmsg) - 1;
  35 + i >= 0 && (errmsg[i] == '\n' || errmsg[i] == '\r'); i--) {
  36 + errmsg[i] = '\0';
  37 + }
  38 +
  39 + return errmsg;
  40 + } else {
  41 + // FormatMessage failed
  42 + return "Unknown error";
  43 + }
  44 +}
  45 +
  46 +
23 47 // Does the about the same as perror(), but for windows api functions
24 48 void winapi_perror(const char* prefix = NULL) {
25 49 DWORD errorno = GetLastError();
26   - char *errmsg;
  50 + const char *errmsg = NULL;
27 51
28   - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
29   - NULL, errorno, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
  52 + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  53 + FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
  54 + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
  55 +
  56 + if (!errmsg) {
  57 + errmsg = "Unknown error\n";
  58 + }
30 59
31 60 // FormatMessage messages include a newline character
32 61
1  src/platform_win32.h
@@ -42,6 +42,7 @@ namespace node {
42 42 #define NO_IMPL_MSG(name...) \
43 43 fprintf(stderr, "Not implemented: %s\n", #name);
44 44
  45 +const char *winapi_strerror(const int errorno);
45 46 void winapi_perror(const char* prefix);
46 47
47 48 }
10 src/platform_win32_winsock.cc
@@ -110,11 +110,15 @@ static struct WINSOCK_EXTENSION_FUNCTIONS {
110 110 */
111 111 void wsa_perror(const char *prefix) {
112 112 DWORD errorno = WSAGetLastError();
113   - char *errmsg;
  113 + const char *errmsg = NULL;
114 114
115   - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
116   - NULL, errorno, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
  115 + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  116 + FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
  117 + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
117 118
  119 + if (!errmsg) {
  120 + errmsg = "Unknown error\n";
  121 + }
118 122 // FormatMessage messages include a newline character
119 123
120 124 if (prefix) {

0 comments on commit 2ce0961

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