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

How can i access the exception that fire handles? #209

Open
securisec opened this issue Nov 15, 2019 · 8 comments
Open

How can i access the exception that fire handles? #209

securisec opened this issue Nov 15, 2019 · 8 comments

Comments

@securisec
Copy link

securisec commented Nov 15, 2019

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 inside SomeClass which is trigger the error.

@dbieber

@securisec
Copy link
Author

bump

@sanmao2018
Copy link

rewrite fire code?use monkey patch?

@dbieber
Copy link
Member

dbieber commented Nov 26, 2019

try:
  fire.Fire(SomeClass)
except fire.core.FireError as e:
  pass  # Access it here.

You can also catch fire.core.FireExit exceptions.
I will try to provide a more complete answer explaining how to use these in the future, but this should get you started.

@securisec
Copy link
Author

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 FireExit exception. Because it blurts out a lot of data because its instantiating a class with many methods. I am trying to override the FireExit message with my own,.

@securisec
Copy link
Author

This is where i am tyring to handle it. @dbieber code

@securisec
Copy link
Author

@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...

@dbieber
Copy link
Member

dbieber commented Mar 2, 2020

  1. If you want to insert your own error handling into Fire, just have your code raise a FireError.
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
  1. Looks like we don't have a way to suppress the output right now. I'll consider this a feature request.

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:

class FireTrace(object):

@securisec
Copy link
Author

Thanks @dbieber . Yes, I had figured out this part, and am handing the error in fire.core.FireExit so stop it closing out. Now trying to figure out how to access the traceback and send back my own exception handler.

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

3 participants