-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
How can i access the exception that fire handles? #209
Comments
bump |
rewrite fire code?use monkey patch? |
You can also catch fire.core.FireExit exceptions. |
Thanks @dbieber ! Yes, a complete example would be awesome! I already figured out most of it in a try catch, but what I am really trying to suppress is the |
@dbieber bump. Still cant figure out how to handle https://github.com/google/python-fire/blob/master/fire/core.py#L275 without forking and patching... |
import fire
class SomeClass:
def demo(self, x):
if x > 5:
raise fire.core.FireError('x is too large')
fire.Fire(SomeClass) $ python example.py demo 10
ERROR: x is too large
Usage: example.py demo X
For detailed information on this command, run:
example.py demo --help
Here's an example of accessing the error / trace though: import fire
class SomeClass:
def a(self, x):
return x
try:
fire.Fire(SomeClass)
except fire.core.FireExit as e:
print(e.trace) # e.trace is a FireTrace object FireTrace source: Line 45 in b77f5ae
|
Thanks @dbieber . Yes, I had figured out this part, and am handing the error in |
I know that whether I am using a class or a function, fire will internally handle the exception and show the help if there is an issue.
So considering
fire_obj = fire.Fire(SomeClass)
, how can I access that exception from fire_obj? I know that fire_obj points to a new instance of SomeClass, but I am trying to capture and handle the exceptions that fire is handling internally when it shows the help or exceptions.When I wrap it in a try except, I can see that the exception is
FireExit
, but I dont want the python-fire help message to print (which is printing from the code in the library). I want to instead raise my own exception, but even better, access the exception that happened insideSomeClass
which is trigger the error.@dbieber
The text was updated successfully, but these errors were encountered: