Error management in CodeWorkflows

Build 1501 on 14/Nov/2017  This topic last edited on: 5/Aug/2014, at 15:33

When something’s wrong into a workflow at runtime, the .NET environment throws an exception: it stops the execution, creates a System.Exception object which contains all the information about the error (error message, memory stack, etc…), and makes this object available to the executing code.

It’s up to the code to choose if catching this exception and resuming the execution, or showing a message to the user, or let it go into a crash.

There are many standard types of exception objects (System.Exception.ArgumentNullException, System.Exception.ArgumentOutOfRangeException, etc…): every exception type indicates what error occurred.

GN4 has its own exceptions types. The exception type for the workflow is: TeraDP.GN4.Common.WorkflowException. So, if you want to notify GN4 that something specific of the workflow is wrong, you have to throw an exception of this type.

For example:

Dim destPath As String = Context.ParValue("destPath")

If String.IsNullOrEmpty(destPath) Then

  Throw New TeraDP.GN4.Common.WorkflowException(TeraDP.GN4.Common.ErrorCode.WFInvalidParam, "destPath")

End If

In CodeWorkflows you can manage an error in different ways.

If you explicitly throw an exception, like in the example above, then the workflow stops its execution; but it is not the only choice you have.

You can logging the error as warning using a Log activity, and then continue the execution of the workflow:

If Context.Data.Count = 0 Then

  Dim inputErrorAct As Log = New Log(Context) With {.Name = "input error"}

  inputErrorAct.Code = TeraDP.GN4.Workflow.LogEntry.LogCode.Warning

  inputErrorAct.Message = "Missing input picture file"

  Dim inputErrorRes As LogResult = inputErrorAct.Do()

End If

The output is something like this: “<WARNING> Missing input picture file”

Or you can log it as blocking error and exit from the workflow:

If Context.Data.Count = 0 Then

  Dim inputErrorAct As Log = New Log(Context) With {.Name = "input error"}

  inputErrorAct.Code = TeraDP.GN4.Workflow.LogEntry.LogCode.Error

  inputErrorAct.Message = "Missing input picture file"

  Dim inputErrorRes As LogResult = inputErrorAct.Do()

  Return 'exit from the workflow

End If

The output is something like this: “<ERROR> Missing input picture file”

Note that Back4 considers a workflow failed (and eventually tries to re-execute it) when the output contains at least one log entry with ‘Error’ code (LogCode.Error).

In the old WindowsWorkflows when a runtime exception occurs, the workflow stops. So, in WindowsWorkflows it is not possible to ignore an error and continue the execution.

On the other hand, because the CodeWorkflows are pieces of .NET code, they can catch the .NET exceptions. Infact, VB.NET provides a way to manage the runtime exceptions: the Try...Catch… statement.

This statement surrounds a piece of code: if an error occurs, the exception object is passed to the Catch code, where the exception can be managed as needed.

Here more information about the Try…Catch… statement: http://msdn.microsoft.com/en-us/library/fk6t46tz.aspx

Into the Catch code you can do anything you need: log the error message and continue.

Try

  Dim loadImgAct As LoadObjects = New LoadObjects(Context) With {.Name = "load picture"}

  loadImgAct.ObjectIds = Context.ObjectIds

  Dim loadImgRes As LoadObjectsResult = loadImgAct.Do()

Catch ex As Exception

  'failed to load picture

  Dim inputErrorAct As Log = New Log(Context) With {.Name = "load error"}

  inputErrorAct.Code = TeraDP.GN4.Workflow.LogEntry.LogCode.Warning

  inputErrorAct.Message = ex.Message

  Dim inputErrorRes As LogResult = inputErrorAct.Do()

  'continue the workflow execution

End Try

or return and exit from the workflow.