Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

[QUESTION] How do I disable typer's exception handler #129

Closed
cazador481 opened this issue Jun 26, 2020 · 6 comments
Closed

[QUESTION] How do I disable typer's exception handler #129

cazador481 opened this issue Jun 26, 2020 · 6 comments
Labels
question Question or problem

Comments

@cazador481
Copy link

I want to be able to disable typer/Click's exception handler, and handle the exceptions myself.
I see that in click you can do this by setting standalone_mode = False in the command.main() function.

@cazador481 cazador481 added the question Question or problem label Jun 26, 2020
@varioustoxins
Copy link

varioustoxins commented Aug 4, 2021

to answer my own question after some digging it looks like you can do this....

if you normally have

import typer
app = typer.Typer()

if __name__ == '__main__':
     app()

you can instead do

import typer
app = typer.Typer()

if __name__ == '__main__':
    command = typer.main.get_command(app)
     
    try:
       result = command(standalone_mode=False)
    except:
       print(f'exception was thrown')

    sys.exit(result)

@jakob1379
Copy link

I dug into the source code and found that typer.run is simply a wrapper for

app = typer.Typer()
app.command()(main)
app()` # <-- this app takes the standalone_mode argurment

@sdargoeuves
Copy link

I saw this question when I was looking at disabling the typer's exception message in case of issue, as it was too verbose.
It's not really related to this question, but could be useful for others. What I needed to do was to disable the prettier_exceptions, which in my case only show_locals was the issue:

app = typer.Typer(add_completion=False, pretty_exceptions_show_locals = False)

or to remove the typer exception completely:

app = typer.Typer(add_completion=False, pretty_exceptions_enable=False)

sersorrel added a commit to wtsi-hgi/softpack-core that referenced this issue Jan 18, 2024
Typer runs Click commands in "standalone mode":

> Click will then handle exceptions and convert them into error messages
> and the function will never return but shut down the interpreter.

so any code after the call to Typer will never actually be executed.

The `assert False` is required to convince mypy that we know Typer will
never return.

Related: fastapi/typer#129
@thunze
Copy link

thunze commented Apr 29, 2024

A disadvantage of setting standalone_mode=False is that exceptions from Click like click.UsageError won't be handled by Typer anymore. Since _original_except_hook is set when importing Typer, and is called by Typer when pretty_exceptions_enable=False, a (rather ugly) workaround is to set sys.excepthook before importing Typer.

@dereksz
Copy link

dereksz commented Jun 6, 2024

I actually preferd to add a handler for the throws, then I still benefit from more of typer's goodness:

            try:
                app()
            except SystemExit as ex:
                if ex.args[0] != 0:
                    raise
            # else continue, all is well

@svlandeg
Copy link
Member

[Maintenance note]: moving this to the discussion forum - we can continue the discussion there! 🙏

@fastapi fastapi locked and limited conversation to collaborators Jul 16, 2024
@svlandeg svlandeg converted this issue into discussion #880 Jul 16, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem
Projects
None yet
Development

No branches or pull requests

7 participants