Handle Errors and Leave a Trace¶
A workflow administrator gets a call: "the Custom Step did something odd to yesterday's invoices". The Custom Step has no console and no log, so the only evidence is what the script left behind. This pattern makes every run explain itself. The script:
- Writes what it decided into metadata.
- Records problems per document and keeps going.
- Fails the step on purpose, with a clear message, when it must not continue.
At a glance
- Extension point Custom Step
- You need Document fields
Index.InvoiceNumberandIndex.InvoiceTotal; permission to edit the workflow. - Teaches A trace in metadata, per-document
try/catch, and failing the step with a useful message.
Set it up¶
- Open any Custom Step in the Workflow designer, or add one after extraction.
- Paste the script below into the JavaScript Block, then put your own logic inside the
tryblock. - Save the step and the workflow.
Script¶
The script writes document General.StepNote, and work item General.StepTrace and General.ProblemCount.
Custom Step: Check Invoices With a Trace
// Custom Step "Check Invoices With a Trace".
// Reads: Index.InvoiceNumber, Index.InvoiceTotal (each document).
// Writes: General.StepNote (each document); General.StepTrace, General.ProblemCount (work item).
// Fails the step when the work item has no documents.
(function () {
var trace = [];
var problems = 0;
function note(text) {
trace.push(text);
}
function toNumber(text) {
var n = parseFloat(String(text || "").replace(/[^0-9.\-]/g, ""));
return isNaN(n) ? null : n;
}
note("start, documents=" + batch.Documents.length);
// Stop early, and fail the step, when the input makes no sense.
if (batch.Documents.length === 0) {
throw new Error("Work item " + batch.BatchId + " has no documents");
}
for (var i = 0; i < batch.Documents.length; i++) {
var doc = batch.Documents[i];
try {
var index = doc.Metadata["Index"] || {};
var invoiceNumber = index["InvoiceNumber"] || "";
var total = toNumber(index["InvoiceTotal"]);
if (!invoiceNumber) {
throw new Error("InvoiceNumber is empty");
}
if (total === null) {
throw new Error("InvoiceTotal '" + (index["InvoiceTotal"] || "") + "' is not a number");
}
builder.SetDocumentMetadata(doc.DocumentId, "General", "StepNote", "OK, total " + total.toFixed(2));
} catch (e) {
// A problem with one document: record it and keep going.
problems++;
builder.SetDocumentMetadata(doc.DocumentId, "General", "StepNote", "Problem: " + e.message);
note("doc " + (i + 1) + ": " + e.message);
}
}
note("end, problems=" + problems);
// Keep the trace short. Metadata values are not a log store.
builder.SetBatchMetadata("General", "StepTrace", trace.join(" | ").substring(0, 2000));
builder.SetBatchMetadata("General", "ProblemCount", String(problems));
})();
How it works¶
note()collects short facts in an array. The script writes them once, at the end, as one work-item value capped at 2,000 characters.- Each document runs inside its own
try/catch. A bad value on one document is recorded on that document, and the loop continues. - An empty work item is a different kind of problem: nothing useful can happen, so the script throws. The step fails, nothing from the run is saved, and the message goes to
Error.LastErrorMessage. - Route on
General.ProblemCountto send work items with problems to review.
Choose between recording and failing¶
| Situation | What to do |
|---|---|
| One document has a bad value; the rest can continue | Catch the error, write it to that document (General.StepNote), count it, and route on the count. |
| The whole work item is unusable, or continuing would write wrong data | throw new Error("..."). The step fails, nothing from this run is saved, and the message goes to Error.LastErrorMessage. |
When the step fails, its Error Behavior decides what happens next:
- Stop (the default) puts the work item in an error state.
- Continue Runs the routes.
- Route Sends it to a step you pick.
Test it¶
- Upload a work item with a valid invoice and an invoice with no invoice number.
- Open it in Monitor or Insight. Read work item
General.StepTraceandGeneral.ProblemCount, and documentGeneral.StepNote. - Start a work item with no documents, for example from a trigger, and read
Error.LastErrorMessageandError.LastErrorStepName.
You should see
General.ProblemCount = 1, a StepTrace such as start, documents=2 | doc 2: InvoiceNumber is empty | end, problems=1, and Problem: InvoiceNumber is empty on the second document. The empty work item fails with Work item ... has no documents in its error message.
What can go wrong¶
Watch out
ReferenceError: console is not defined(orlogger). These objects do not exist in the Custom Step. Usenote()and metadata as shown.- The trace is missing after a failure. When the script throws, nothing from the run is saved, including the trace. Put the key facts in the message you throw.
throw "text"gives an unclear message. Throw anErrorobject so the message and the line are kept.- A secret appears in the trace. Never
note()a value from a secret global variable. Metadata is visible to everyone who can open the work item.