From de576e37e1e255b65c093047d11fcfe98312f415 Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Wed, 26 Aug 2020 19:21:23 -0400 Subject: [PATCH] impove uncaught exception printing on windows --- simpledisplay.d | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/simpledisplay.d b/simpledisplay.d index d171e9c..cb39ea0 100644 --- a/simpledisplay.d +++ b/simpledisplay.d @@ -4439,7 +4439,7 @@ class Timer { if(Timer* t = timer in mapping) { try (*t).trigger(); - catch(Exception e) { throw new Error(e.msg, e.file, e.line); } + catch(Exception e) { sdpy_abort(e); assert(0); } } } @@ -4791,16 +4791,19 @@ class WindowsApiException : Exception { if(error == 0) buffer[pos++] = '0'; else { + + auto ec = error; auto init = pos; - while(error) { - buffer[pos++] = (error % 10) + '0'; - error /= 10; - } - for(int i = 0; i < (pos - init) / 2; i++) { - char c = buffer[i + init]; - buffer[i + init] = buffer[pos - (i + init) - 1]; - buffer[pos - (i + init) - 1] = c; + while(ec) { + buffer[pos++] = (ec % 10) + '0'; + ec /= 10; } + + buffer[pos++] = ' '; + + size_t size = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, null, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &(buffer[pos]), buffer.length - pos, null); + + pos += size; } @@ -8507,13 +8510,21 @@ version(Windows) { return DefWindowProc(hWnd, iMessage, wParam, lParam); } } catch (Exception e) { - //try { - //MessageBoxA(null, (e.toString() ~ "\0").ptr, "Exception caught in WndProc", 0); - //} catch(Exception e) {} - assert(false, "Exception caught in WndProc " ~ e.toString()); + try { + sdpy_abort(e); + return 0; + } catch(Exception e) { assert(0); } } } + void sdpy_abort(Throwable e) nothrow { + try + MessageBoxA(null, (e.toString() ~ "\0").ptr, "Exception caught in WndProc", 0); + catch(Exception e) + MessageBoxA(null, "Exception.toString threw too!", "Exception caught in WndProc", 0); + ExitProcess(1); + } + mixin template NativeScreenPainterImplementation() { HDC hdc; HWND hwnd;