Error Handling and Transactions¶
A server script runs as part of a workflow operation, such as processing a work item. This page explains what happens when a script fails, which changes are kept, and how to handle errors from data checks and external systems.
What Happens When a Script Fails¶
An error that the script does not catch stops the script and fails the operation that ran it. The operation's transaction is not committed, so the changes made during the operation are discarded, including process data changes and rows written with $Database.
Calls to external systems are not part of the transaction. A REST or SOAP request that succeeded before the error is not undone. If the operation runs again, the request is sent again; see Making External Calls Safe to Repeat.
Stopping with a Clear Message¶
Stop the script with an error when the data does not allow the process to continue. Write a message that tells the reader what is wrong:
1 2 3 | |
$Xml.Assert is a shorter form for conditions on process data:
1 | |
Do not include passwords, tokens or personal data in error messages. Messages can be written to logs and diagnostics.
Catching Errors¶
Catch an error only when the script can continue in a meaningful way, for example when a value is optional:
1 2 3 4 5 6 7 8 9 10 11 12 | |
When the error must still fail the operation, add context and throw it again instead of hiding it:
1 2 3 4 5 6 7 8 | |
Keeping Records When the Operation Fails¶
Some records must be kept even when the operation fails, such as an audit entry of an attempt. Write them in Script.Isolated. The function runs in a new, independent transaction that is committed when the function completes, so a later failure of the operation does not remove the record:
1 2 3 4 5 6 | |
Use Script.Isolated only for this purpose. Changes made in it are not undone together with the operation, so the data can become inconsistent if it is used for ordinary updates.
Limiting Long Operations¶
Script.Timeout runs a function with a time limit and returns null when the function fails or exceeds the limit. Because a failure and a timeout give the same result, use it only when the script can continue without the value:
1 2 3 4 5 6 7 | |
In long loops, call Script.ThrowIfCancelled so that the script stops when the operation is cancelled.
Making External Calls Safe to Repeat¶
Because external calls are not rolled back, design them so that repeating them does not create duplicates:
- Send a stable identifier with the request, such as the instance number or the work item ID, so that the service can recognize a repeated request.
- Before creating a record, check whether a record with the same identifier already exists.
- Store the identifier returned by the service in the process data, and skip the call when it is already set.
1 2 3 4 5 6 7 8 9 10 11 12 | |
The process data is saved only when the operation succeeds, so the stored identifier does not prevent a repeat after a failure. The stable reference value lets the service detect the duplicate in that case.