-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display a messagebox when an error occurs during startup
- Loading branch information
1 parent
ca82531
commit 2e7ba73
Showing
2 changed files
with
133 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import sys | ||
import traceback | ||
|
||
|
||
def show_error(exc: Exception, shutdown: bool = True) -> None: | ||
""" | ||
Create a native pop-up without any third party dependency. | ||
:param exc: the error to show to the user | ||
:param shutdown: whether to shut down after showing the error | ||
""" | ||
title = f"A {exc.__class__.__name__} occurred" | ||
text = "\n\n".join([str(a) for a in exc.args]) | ||
sep = "*" * 80 | ||
|
||
print('\n'.join([sep, title, sep, traceback.format_exc(), sep]), file=sys.stderr) # noqa: T201, FLY002 | ||
try: | ||
if sys.platform == 'win32': | ||
import win32api | ||
|
||
win32api.MessageBox(0, text, title) | ||
elif sys.platform == 'Linux': | ||
import subprocess | ||
|
||
subprocess.Popen(['xmessage', '-center', text]) # noqa: S603, S607 | ||
elif sys.platform == 'Darwin': | ||
import subprocess | ||
|
||
subprocess.Popen(['/usr/bin/osascript', '-e', text]) # noqa: S603 | ||
else: | ||
print(f'cannot create native pop-up for system {sys.platform}') # noqa: T201 | ||
except Exception as exception: | ||
# Use base Exception, because code above can raise many | ||
# non-obvious types of exceptions: | ||
# (SubprocessError, ImportError, win32api.error, FileNotFoundError) | ||
print(f'Error while showing a message box: {exception}') # noqa: T201 | ||
|
||
if shutdown: | ||
sys.exit(1) |