Improve error handling

Dieser Commit ist enthalten in:
Axel Svensson 2023-08-08 16:59:51 +02:00
Ursprung 7612e52a63
Commit 41d1ab7076

Datei anzeigen

@ -2,7 +2,7 @@
#Dependencies: python3.7 or later with module tkinter, gs (Ghostscript), pdftk and pdfinfo. #Dependencies: python3.7 or later with module tkinter, gs (Ghostscript), pdftk and pdfinfo.
import argparse, os, queue, re, subprocess, sys, tempfile, time import argparse, os, queue, re, subprocess, sys, tempfile, traceback, time
# Inspired by https://unix.stackexchange.com/a/141496 # Inspired by https://unix.stackexchange.com/a/141496
def main(args): def main(args):
@ -137,7 +137,10 @@ def main(args):
doSign=True doSign=True
root.destroy() root.destroy()
# Error handling # Error handling
tk.Tk.report_callback_exception = lambda self, exc, val, tb: die(val) def tkerror(self, exc, val, tb):
traceback.print_exception(exc, val, tb)
sys.exit(1)
tk.Tk.report_callback_exception = tkerror
# Window and menu # Window and menu
root = tk.Tk(className="pdf-sign") root = tk.Tk(className="pdf-sign")
rootmenu = tk.Menu(root) rootmenu = tk.Menu(root)
@ -379,7 +382,7 @@ def fromCmdOutput(cmd, pattern):
def die(reason): def die(reason):
print(reason, file=sys.stderr) print(reason, file=sys.stderr)
sys.exit(2) sys.exit(1)
# Monkey-patch argparse if necessary # Monkey-patch argparse if necessary
if not 'BooleanOptionalAction' in dir(argparse): if not 'BooleanOptionalAction' in dir(argparse):