-
Notifications
You must be signed in to change notification settings - Fork 699
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
Feature request: Add except exceptions list to catch decorator. #248
Comments
Hey @heckad. The next Loguru release will feature a new However, I'm not sure I want to add too much functionality to this method. It should remain a very basic decorator whose main purpose is to catch unexpected exceptions. In particular, it should ideally not alter the control flow of a program. Good old |
In function
|
Thanks for the code sample. 👍 I don't know. On the one hand, I like your suggestion to add an On the other hand, I'm not sure it's "good practice". As I said, I think it's important that logging remains separate from the control flow of the program. By adding an try:
handle_new_value(new_event.value)
except StopConsumeNewValue:
pass
except Exception:
logger.exception("Unexpected error") I'm mostly worried about the case where the function returns a value. If one day you decide to return something from try:
result = handle_new_value(new_event.value)
except StopConsumeNewValue:
pass
except Exception:
logger.exception("Unexpected error")
else:
logger.info("Result: {}", result) Using try:
result = handle_new_value(new_event.value)
except StopConsumeNewValue:
pass
else:
if result is not None:
logger.info("Result: {}", result) Of course, someone could argue that it's already the case, even without adding the new |
Please add ability to control raise an exception or not to
Implementation example
do something like
|
It's something I've been thinking about, but it's not a good idea in my opinion. It's too implicit and not what one expects with a function named like this. The purpose of Another solution is to "silently" catch exception if @logger.catch
@logger.catch(StopConsumeNewValue, message=None)
def f():
... However, this means non-trivial exception handling need to be shifted to |
Maybe add to
It's simple and doesn’t look bad. |
If I were to implement it, I would choose the |
Also, it's needed for a flask app. Flask recommends use abort function which raise |
All right, you won. I added the new It was very simple to implement and it doesn't complicate the API. Since it can be useful according to the use cases you reported, let's just add it. Thanks for the feature suggestion. 👍 |
Thanks for add new feature. |
For some reason, my function has special exception signals, and I don’t want to catch them. Have a filter will be helpful.
The text was updated successfully, but these errors were encountered: