Commit 418e0ad4 authored by Kenton Varda's avatar Kenton Varda

Get Win32 error messages with FormatMessageW instead of FormatMessageA.

Also include the error number in the string even if there is a message, because otherwise looking them up is hard.

Also remove the trailing newline from the message.
parent ff9c3321
......@@ -34,6 +34,7 @@
#define NOIME 1
#include <windows.h>
#include "windows-sanity.h"
#include "encoding.h"
#endif
namespace kj {
......@@ -357,26 +358,30 @@ void Debug::Fault::init(
const char* file, int line, Win32Error osErrorNumber,
const char* condition, const char* macroArgs, ArrayPtr<String> argValues) {
LPVOID ptr;
// TODO(soon): Use FormatMessageW() instead.
// TODO(soon): Why doesn't this work for winsock errors?
DWORD result = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
DWORD result = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, osErrorNumber.number,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &ptr, 0, NULL);
(LPWSTR) &ptr, 0, NULL);
String message;
if (result > 0) {
KJ_DEFER(LocalFree(ptr));
exception = new Exception(typeOfWin32Error(osErrorNumber.number), file, line,
makeDescriptionImpl(SYSCALL, condition, 0, reinterpret_cast<char*>(ptr),
macroArgs, argValues));
const wchar_t* desc = reinterpret_cast<wchar_t*>(ptr);
size_t len = wcslen(desc);
if (len > 0 && desc[len-1] == '\n') --len;
if (len > 0 && desc[len-1] == '\r') --len;
message = kj::str('#', osErrorNumber.number, ' ',
decodeWideString(arrayPtr(desc, len)));
} else {
auto message = kj::str("win32 error code: ", osErrorNumber.number);
exception = new Exception(typeOfWin32Error(osErrorNumber.number), file, line,
makeDescriptionImpl(SYSCALL, condition, 0, message.cStr(),
macroArgs, argValues));
message = kj::str("win32 error code: ", osErrorNumber.number);
}
exception = new Exception(typeOfWin32Error(osErrorNumber.number), file, line,
makeDescriptionImpl(SYSCALL, condition, 0, message.cStr(),
macroArgs, argValues));
}
#endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment