onError permalink

add custom data to exception permalink

Provide custom error information if the step raises an exception. This lets you add extra information to the error itself.

If this step errors, write the contents of onError to runErrors[n].customError in context. Steps inside a failure handler then can use this information. Alternatively, subsequent steps can also use this information, assuming you’ve got a swallow somewhere in the call chain.

onError can be a simple string, or your your own dict, or any given object.

You can use substitutions to assign values dynamically at run-time.

simple string extra error data permalink

You can add a simple string for extra error information:

# ./onerror-decorator.yaml
steps:
  - name: pypyr.steps.assert
    comment: deliberately raise error
             with custom error info
    in:
      assert: False
    onError: this is a custom error
on_failure:
  - name: pypyr.steps.echo
    comment: the custom error is in runErrors
             it's the 1st and only error in
             this pipeline, hence index 0.
    in:
      echoMe: "the error was: {runErrors[0][customError]}"

When you run this pipeline, this is the result:

$ pypyr onerror-decorator
Error while running step pypyr.steps.assert at pipeline yaml line: 3, col: 5
Something went wrong. Will now try to run on_failure.
the error was: this is a custom error

AssertionError: assert False evaluated to False.

create custom exception object permalink

onError will take any given type. You can add your own custom object here.

In this pipeline, add an error code and a description to the custom exception object:

# ./onerror-complex-obj.yaml
steps:
  - name: pypyr.steps.set
    comment: arbitrarily set a context value.
             will be using this value in error
             object.
    in:
      set: 
        arbKey: FROM SUBSTITUTION EXPRESSION
  - name: pypyr.steps.assert
    comment: deliberately raise error
             with custom error object
    in:
      assert: False
    onError:
      myerr_code: 123
      myerr_description: "my err {arbKey} description"
on_failure:
  - name: pypyr.steps.echo
    comment: the custom error is in runErrors
             notice your custom error object
             is here with substitutions applied.
    in:
      echoMe: |
               the error code: {runErrors[0][customError][myerr_code]}
               the error description: {runErrors[0][customError][myerr_description]}               

When you run this pipeline, this is the result:

$ pypyr onerror-complex-obj
Error while running step pypyr.steps.assert at pipeline yaml line: 10, col: 5
Something went wrong. Will now try to run on_failure.
the error code: 123
the error description: my err FROM SUBSTITUTION EXPRESSION description

AssertionError: assert False evaluated to False.

see also

last updated on .