Permalink
Browse files

Improve error reporting on windows

  • Loading branch information...
1 parent 0eb4c21 commit 2ce0961de181b5ccb0ff66ff3f0e1c6c417dda12 @piscisaureus piscisaureus committed Jan 17, 2011
Showing with 47 additions and 7 deletions.
  1. +7 −1 src/node.cc
  2. +32 −3 src/platform_win32.cc
  3. +1 −0 src/platform_win32.h
  4. +7 −3 src/platform_win32_winsock.cc
View
@@ -1004,7 +1004,13 @@ Local<Value> ErrnoException(int errorno,
const char *path) {
Local<Value> e;
Local<String> estring = String::NewSymbol(errno_string(errorno));
- if (!msg[0]) msg = strerror(errorno);
+ if (!msg[0]) {
+#ifdef __POSIX__
+ msg = strerror(errorno);
+#else // __MINGW32__
+ msg = winapi_strerror(errorno);
+#endif
+ }
Local<String> message = String::NewSymbol(msg);
Local<String> cons1 = String::Concat(estring, String::NewSymbol(", "));
View
@@ -20,13 +20,42 @@ using namespace v8;
static char *process_title = NULL;
+// Does the about the same as strerror(),
+// but supports all windows errror messages
+const char *winapi_strerror(const int errorno) {
+ char *errmsg = NULL;
+
+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
+
+ if (errmsg) {
+ // Remove trailing newlines
+ for (int i = strlen(errmsg) - 1;
+ i >= 0 && (errmsg[i] == '\n' || errmsg[i] == '\r'); i--) {
+ errmsg[i] = '\0';
+ }
+
+ return errmsg;
+ } else {
+ // FormatMessage failed
+ return "Unknown error";
+ }
+}
+
+
// Does the about the same as perror(), but for windows api functions
void winapi_perror(const char* prefix = NULL) {
DWORD errorno = GetLastError();
- char *errmsg;
+ const char *errmsg = NULL;
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, errorno, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
+
+ if (!errmsg) {
+ errmsg = "Unknown error\n";
+ }
// FormatMessage messages include a newline character
View
@@ -42,6 +42,7 @@ namespace node {
#define NO_IMPL_MSG(name...) \
fprintf(stderr, "Not implemented: %s\n", #name);
+const char *winapi_strerror(const int errorno);
void winapi_perror(const char* prefix);
}
@@ -110,11 +110,15 @@ static struct WINSOCK_EXTENSION_FUNCTIONS {
*/
void wsa_perror(const char *prefix) {
DWORD errorno = WSAGetLastError();
- char *errmsg;
+ const char *errmsg = NULL;
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, errorno, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);
+ if (!errmsg) {
+ errmsg = "Unknown error\n";
+ }
// FormatMessage messages include a newline character
if (prefix) {

0 comments on commit 2ce0961

Please sign in to comment.