How can I turn VTK pipeline errors into python exceptions?

I am using VTK to write files in a highly parallelized python application across multiple nodes. Because of the libraries I use (parsl), the stdout and stderr messages are all “swallowed”, giving me no access to VTK errors. When something goes wrong inside the pipeline, the VTK code sliently fails.

A solution would be to convert all VTK errors to python exceptions.

I can raise errors by using VTK’s AddObserver functions. However, this does not catch Pipeline errors (such as vtkCompositeDataPipeline errors). The following example fails (unstructured grid cannot be put into a STL Writer), but does not raise a python Exception:

import vtk

ug = vtk.vtkUnstructuredGrid()
pts = vtk.vtkPoints()
pts.InsertNextPoint( 0,1,2 )
ug.SetPoints( pts )

#pd = vtk.vtkPolyData()
#pts = vtk.vtkPoints()
#pd.SetPoints( pts )

def observer( a, b ):
    raise Exception( a, b )

w = vtk.vtkSTLWriter()
w.AddObserver( "ErrorEvent", observer )
w.SetFileName( "123.stl" )
w.SetInputData( ug )

How do I turn all VTK errors into python exceptions? I read something about vtkOutputWindow usage, but haven’t found examples of how to use this to raise exceptions…?

Ideally, I’d like to enable this globally, because there are so many different places where I use VTK… and adding these observers everywhere can get very tedious.