abey79 / vpype

The Swiss-Army-knife command-line tool for plotter vector graphics.
https://vpype.readthedocs.io/
MIT License
699 stars 61 forks source link

Output error messages when using `execute` from vpype API #664

Open ithinkido opened 1 year ago

ithinkido commented 1 year ago

Would it be possible to get error messages when using the execute command from the vpype cli ? The use case would be something like this :

error == vp.execute( do something)
      if error:
          print( error)
      else :
            print( "conversion success")
abey79 commented 1 year ago

This is a good feature idea. However, it would work using exceptions:

try:
    doc = vp.execute_throw("....", doc)
except Exception:
   pass # handle error here
ithinkido commented 1 year ago

Can execption differentiate between errors and info messages ?

abey79 commented 1 year ago

Info message would continue to go to stdout. Maybe we should have a separate mechanism to capture stdout and make it programmatically available.

abey79 commented 9 months ago

A more complete workaround is:

import contextlib
import io

import vpype_cli

try:
    output = io.StringIO()
    with contextlib.redirect_stdout(output):
        vpype_cli.execute("read doesntexist.svg show", global_opt="-vv")
    print("Output")
    print(output.getvalue())
except Exception as e:
    print("Error:")
    print(type(e))
    print(e)

This captures both output and catches errors.