Error Handling
An integration application has very little control over the systems it depends on. Errors are inevitable; what matters is how you detect, report, and recover from them. This lesson covers Magic xpi's three layers of error handling — the step, the flow, and the dedicated error flow.
Context Error Variables
Magic xpi exposes a set of predefined context variables that carry error information. The current error variables can only be read in the condition that immediately follows the failing step — they are cleared before the next step runs:
C.sys.ErrorCode | The error code raised by the step. |
|---|---|
C.sys.ErrorDescription | The error description. |
To trace the origin of an error, use:
C.sys.LastErrorFlowName | The flow where the last error occurred. |
|---|---|
C.sys.ErrorStepName | The step that raised it. |
The "last error" set is updated only when a new error is raised:
C.sys.LastErrorCode | Last error code; survives until the next error. |
|---|---|
C.sys.LastErrorDescription | Description of the last error. |
C.sys.LastErrorInfo | BLOB with full info — SQL statements, Java stack traces, etc. |
Step-Level Error Handling
By default, when a step errors it exits — even if it had more methods to run. Two components are exceptions: Data Mapper and SAP B1 have an Error Behavior property you can set to either Exit or Continue.
The simplest way to handle an error is to put a step or branch behind a
condition that tests C.sys.ErrorCode:
- On the Scan for New Requests flow, add an Email child of Extract Details from Request; name it Send Thank You Email; processing mode Parallel; method interface Quick Send.
- Use
F.CustomerEmailas To, "We received your request." as Subject, and "Thank you for your request..." as Body. UseASCIIChr (10)for line breaks. - Add another Email step as a child — name it Send Error Email. Send to
postmaster@magic.xpi.course.comwith subject "Email to the customer was returned". - Set the condition on Send Error Email to
C.sys.ErrorCode = 102— only fire on the Email "send mail error".
Flow-Level Error Handling
Step-level conditions get repetitive when the same handling is needed across many steps. Error Policies let you declare automatic handling for an entire range of error codes. Open the policies by double-clicking Error Policies under a flow.
| Abort | Stop the flow. |
|---|---|
| Ignore | Continue as if nothing happened. |
| Restart Flow | Start the flow over from the top. |
| Retry | Re-execute the failing step. |
| Jump | Continue from a specific step that may not normally be reachable. |
C.sys.LastErrorComponentType tells you
which one fired.
To wire up a Jump policy:
- Place a "handler" step somewhere in the flow with the condition
'FALSE'Logso it can't be reached normally. - Open Error Policies and click Add.
- Use the zoom button on From to pick the component (e.g. Email) and the error code (e.g. 102 Send Mail Error). Repeat for To.
- Set Error Policy to Jump and pick the handler step.
When the matching error fires, control jumps directly to that step — bypassing the false condition.
Dedicated Error Flows
For sophisticated handling, define an error flow — an ordinary Magic xpi flow assigned to handle errors raised by another flow. You associate the error flow via the External section of the parent flow's properties; an icon then appears next to that flow's Trigger area.
An example skeleton:
- Create a flow named Errors.
- Drop an Email step named Send Email to Administrator; configure Quick Send to
postmaster@magic.xpi.course.comwith the appropriate subject and body. - Set its condition to
C.sys.LastErrorCode = 102— only handle that specific error.
When the error flow ends, control returns to the failing flow at the step after the one that errored, and the error is cleared.
Returning Errors From an Error Flow
By default, an error flow ends without re-raising. If you want it to re-raise — the same error or a different one — finish the flow with a Flow Data utility:
- Add a Flow Data step at the end of the error flow.
- Update
C.sys.ErrorCodewith a value compatible with the original component (Email accepts 100, 101, 102, 103). - Or, to re-raise the same code, set
C.sys.ErrorCodefromC.sys.LastErrorCode.
When control returns to the parent flow, that error will be raised and handled by the flow-level mechanism.
Exercise
In the previous lesson you used the File Management component to copy a static HTML response file. What happens if the file or folder doesn't exist?
- If the file or folder is missing, return an error to the browser by sending the
Error.tpltemplate from the templates folder. - This is a single specific case — consider declaring an Error Policy for it rather than a step-level condition.
Summary
Magic xpi resolves an error in this order:
- If a dedicated error flow is defined, it is invoked.
- If the error flow re-raises (or there's no error flow), the flow's Error Policy applies.
- Any step-level condition referencing
C.sys.ErrorCodesees the surviving value.
You should now be able to:
- Read the context error variables and condition steps on them.
- Build flow-level Error Policies that abort, ignore, retry, restart, or jump.
- Define and assign a dedicated error flow to handle complex scenarios.
- Re-raise errors deliberately when business rules require it.
