Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-raise exceptions from on_data handler #242

Closed
martinv13 opened this issue Oct 11, 2024 · 5 comments
Closed

Re-raise exceptions from on_data handler #242

martinv13 opened this issue Oct 11, 2024 · 5 comments

Comments

@martinv13
Copy link

Thank you for this very useful package.

Is your feature request related to a problem? Please describe.

I am using the on_data handler to process decrypted data. When an exception is raised within it, decrypt_file call prints it to the console but does not raise.

In the result returned by decrypt_file I see no clue that this exception was raised.

Describe the solution you'd like

I would prefer that decrypt_file re-raise the exception instead of printing it. Or alternatively that returncode were changed to non-zero. Or another possibility would be simply to warn about this behavior in the doc as I suppose most users would expect decrypt_file to re-raise in the first place (the example shown in the doc says that it does not handle errors but could be further specified).

Describe alternatives you've considered

I could circumvent this issue by handling all exceptions within the callable object passed to on_data, setting a flag when an exception is raised and read this flag afterwards to raise outside decrypt_file call but it does not look very intuitive.

Thanks!

@vsajip
Copy link
Owner

vsajip commented Oct 12, 2024

The on_data handling happens across several I/O operations, not just decryption, at a lower level than e.g. decrypt_file - so it would be hard to provide the solution you'd like. The exception is actually raised on a different thread, and printing could perhaps be avoided by swallowing an exception, but that doesn't seem ideal either. I don't mind adding suggestions for error handling in on_data to the documentation.

@martinv13
Copy link
Author

Thanks for your answer, I didn't realize that it was used for other cases too.

Indeed adding something along those lines in the doc would be helpful:

Beware that decrypt_file will not re-raise exceptions thrown by the on_data handler, but rather print them to the console. As a consequence you should make sure that you handle them within the on_data handler.

And change one of the examples like this for instance:

class Collector:
        def __init__(self, fn):
            self.out = open(fn, 'wb')
            self.exception = None

        def __call__(self, chunk):
            try:
                self.out.write(chunk)
            except Exception as e:
                self.exception = e
            finally:
                if not chunk:
                    self.out.close()
            return False  # Tell python-gnupg not to buffer the chunk

I can do a quick PR if you'd like, but maybe you prefer writing this a bit differently?

Thanks
Martin

@vsajip
Copy link
Owner

vsajip commented Oct 12, 2024

I've already started on something to address this, I'll push it shortly - just quite busy this weekend. My approach is a little different (allowing on_data to just be a function) - it captures the exception in a low-level I/O handling routine and returns it in an on_data_failure attribute on the result object from a call. I still need to update the docs and tests etc.

@vsajip vsajip closed this as completed in 7fe897f Oct 15, 2024
@vsajip
Copy link
Owner

vsajip commented Oct 15, 2024

I have now pushed the changes, and updated the documentation which you can see at https://gnupg.readthedocs.io/en/latest/ - search for "New in version 0.5.4" to see the update.

@martinv13
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants