pyinvoke / invoke

Pythonic task management & command execution.
http://pyinvoke.org
BSD 2-Clause "Simplified" License
4.31k stars 365 forks source link

What is the correct way to do error handling of tools executed with run? #896

Closed red8888 closed 1 year ago

red8888 commented 1 year ago

I want to catch certain errors from a command line tool I'm executing. What is the best way to do this? I had trouble finding examples of this in the docs.

I'm doing this:

result = None
try:
    result = ctx.run(
        "somecommand",
        hide=False,
    )
except UnexpectedExit:
    print(result.stderr)

When I try to print result.stderr it throws an actual error.

How do I search the stderr output a command returns for a substring and raise my own exception or log message if the substring exists in the error output?

I also tried using warn=true, but if I do that how do I check if an error was returned and if so see if it contains my substring?

red8888 commented 1 year ago

Found my issue the command doesn't appear to send output to stdout