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

Custom raise message helper #419

Merged
merged 2 commits into from
Jun 16, 2022
Merged

Custom raise message helper #419

merged 2 commits into from
Jun 16, 2022

Conversation

muellerzr
Copy link
Contributor

@muellerzr muellerzr commented Jun 16, 2022

Raise with message helper

What does this add?

This PR adds a helper function that will allow a user to easily add custom messages to already existing Exceptions

Who is it for?

Developers who want to provide clearer messages for errors

Why is it needed?

I constantly see myself going through the same repeating patterns when writing better error logs for Exceptions, and this PR improves this frustration by writing a quick util for it

What parts of the API does this impact?

User-facing:

Adds a new raise_with_msg function that takes in an exception, a message, and a boolean of whether to append the new message to the original exception args (e.args) or to override them completely with the new message

Internal structure:

None, however after this PR it will be propagated into fastai for the debugging errors/logs

Basic Usage Example(s):

Before:

try: dosomething()
except Exception as e:
    e.args = ["Some custom error message"]
    raise

After:

try: dosomething()
except Exception as e: raise_with_msg(e, "Some custom error message")

When would I use it, and when wouldn't I?

You'd use this specifically when the default error/exception might need to be changed, and putting the message in the base exception will not do. Such as when there are multiple triggers for the same exception, but depending on where it was called from their context could be slightly different

Does a similar feature exist? If so, why is this better?

No

cc @hamelsmu @jph00

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@jph00
Copy link
Contributor

jph00 commented Jun 16, 2022 via email

@muellerzr
Copy link
Contributor Author

muellerzr commented Jun 16, 2022

@jph00 it did by one frame due to the nested callings. Sadly not even doing the from None helped because it'll still trace to where we called it. Instead this is a func that now just modifies e.args and returns it. Aka the earlier example is now:

try: dosomething()
except Exception as e: raise modify_exception(e, "Some custom error message")

I'd monkey-patch exception but sadly we can't do that since it's base C.

If we're fine with one extra frame in the midst of our error finding I can have it be raised in the func itself, but otherwise sadly not 😄

@jph00 jph00 merged commit 4a5a643 into AnswerDotAI:master Jun 16, 2022
@jph00
Copy link
Contributor

jph00 commented Jun 16, 2022

I think that's a better approach - thanks

@hamelsmu hamelsmu added the enhancement New feature or request label Jun 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants