@jarun Btw, may I suggest that you dump a traceback for the last exception in debug mode, like what googler does? Right now you only print the exception message (not even the exception type), and are left guessing where it comes from, which is especially hard when the exception comes from anywhere in your dependency chain, and/or when the exception message isn't clear (sometimes there may not even be a message, like `cryptography.fernet.InvalidToken`).
Since you seem to use `sys.exit(1)` in quite a few places instead of centralizing the reporting, and `LOGERR` may not be exclusively used with exceptions, let alone fatal ones, I think the following trick would probably introduce the minimum amount of change while achieving the goal:
```diff
diff --git a/buku b/buku
index 9495cef..baf7181 100755
--- a/buku
+++ b/buku
@@ -5566,4 +5566,14 @@ POSITIONAL ARGUMENTS:
bdb.close_quit(0)
if __name__ == '__main__':
- main()
+ try:
+ main()
+ except SystemExit as e:
+ # If we exited via sys.exit(1), and if we handled an exception before
+ # that, we want to get the traceback of that exception in debug mode,
+ # which would be conveniently stored in the __context__ field of the
+ # current exception.
+ if e.code != 0 and e.__context__ is not None and LOGGER.isEnabledFor(logging.DEBUG):
+ cause = e.__context__
+ import traceback
+ traceback.print_exception(type(cause), cause, cause.__traceback__)
```
This way, bug reports could look like this:
```
$ buku -l --debug
[DEBUG] buku v4.4
[DEBUG] Python v3.8.5
Password:
Password:
[ERROR] Context was already finalized.
Traceback (most recent call last):
File "buku", line 231, in encrypt_file
outfp.write(encryptor.update(chunk) + encryptor.finalize())
File "/tmp/buku/venv/lib/python3.8/site-packages/cryptography/hazmat/primitives/ciphers/base.py", line 153, in update
raise AlreadyFinalized("Context was already finalized.")
cryptography.exceptions.AlreadyFinalized: Context was already finalized.
```
The crypto routine doesn’t care about whether the file is a SQLite database, or a video, or anything. You should be able to put any file there and it either works, or not. Content just doesn’t play a role here, only the byte count.