Skip to content

Commit

Permalink
ae.utils.main: Catch GetOptException even in debug builds
Browse files Browse the repository at this point in the history
Don't show stack traces for usage errors in debug builds,
as non-technical users find them unsightly.

Debug builds are produced by default by Dub and
sometimes are needed to work around compiler bugs
(e.g. CyberShadow/Digger#37).

Hopefully the try/catch clause does not affect debugability of
applications; otherwise, this change will be reverted.

Partially fixes CyberShadow/Digger#81.
  • Loading branch information
CyberShadow committed Dec 15, 2019
1 parent 6ea2a12 commit 179adca
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions utils/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,38 @@ mixin template main(alias realMain)
return realMain(args);
}

int main(string[] args)
int runCatchingException(E, string message)(string[] args)
{
debug
try
return run(args);
else
catch (E e)
{
try
return run(args);
catch (Throwable e)
version (Windows)
{
version (Windows)
import core.sys.windows.windows;
auto h = GetStdHandle(STD_ERROR_HANDLE);
if (!h || h == INVALID_HANDLE_VALUE)
{
import core.sys.windows.windows;
auto h = GetStdHandle(STD_ERROR_HANDLE);
if (!h || h == INVALID_HANDLE_VALUE)
{
import ae.sys.windows : messageBox;
messageBox(e.msg, "Fatal error", MB_ICONERROR);
return 1;
}
import ae.sys.windows : messageBox;
messageBox(e.msg, message, MB_ICONERROR);
return 1;
}

import std.stdio;
stderr.writefln("Fatal error: %s", e.msg);
return 1;
}

import std.stdio : stderr;
stderr.writefln("%s: %s", message, e.msg);
return 1;
}
}

int main(string[] args)
{
debug
static if(is(std.getopt.GetOptException))
return runCatchingException!(std.getopt.GetOptException, "Usage error")(args);
else
return run(args);
else
return runCatchingException!(Throwable, "Fatal error")(args);
}
}

0 comments on commit 179adca

Please sign in to comment.