From 5e7a00ed24415bc6a76fe076ae76f7d5f10d9df8 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Wed, 22 Sep 2021 20:48:49 -0600 Subject: [PATCH] Defend against losing the first exception's message when Exception.ToString() throws --- src/nbgv/Program.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/nbgv/Program.cs b/src/nbgv/Program.cs index 8691e2c7..a85abf6b 100644 --- a/src/nbgv/Program.cs +++ b/src/nbgv/Program.cs @@ -215,9 +215,23 @@ private static Parser BuildCommandLine() } } }, (MiddlewareOrder)(-3000)) // MiddlewareOrderInternal.ExceptionHandler so [parse] directive is accurate. + .UseExceptionHandler((ex, context) => PrintException(ex, context)) .Build(); } + private static void PrintException(Exception ex, InvocationContext context) + { + try + { + Console.Error.WriteLine("Unhandled exception: {0}", ex); + } + catch (Exception ex2) + { + Console.Error.WriteLine("Unhandled exception: {0}", ex.Message); + Console.Error.WriteLine("Unhandled exception while trying to print string version of the above exception: {0}", ex2); + } + } + private static int MainInner(string[] args) { try