17. Error Handling
Business functions report problems in two different ways, and knowing which is which is the difference between a process that fails loudly and one that fails silently.
Stopping a process
Throw a standard JavaScript Error. Its message is what the caller sees.
throw new Error("This client still has open orders and cannot be deleted.");Legacy process code called a global throwError(...). It does not exist — calling it fails with throwError is not defined. Use throw new Error(...).
Throwing inside a Before* process cancels the operation that triggered it. The record is not written and your message is returned to the caller, which makes it the right place for validation:
/** @event beforeCreate @entity orders */
if (!context.record.ClientId) {
throw new Error("An order must have a client.");
}
const client = await context.functions.getById("clients", context.record.ClientId);
if (!client) throw new Error("Client " + context.record.ClientId + " does not exist.");
if (client.CreditHold) throw new Error("Client " + client.Name + " is on credit hold.");In an After* process the write has already happened, so throwing reports the failure and stops the remaining nodes but does not roll anything back.
Write messages for the person who will see them — they surface in the UI:
// Unhelpful
throw new Error("validation failed");
// Useful
throw new Error("Amount must be greater than zero (received " + context.record.Amount + ").");Functions that fail without throwing
Most functions return a result object instead of raising. Ignore it and the process continues as though everything worked.
| Function | Failure looks like |
|---|---|
create | { success: false, message } |
update, delete | { success: false, message } |
createBulk, updateBulk, deleteBulk | { success: false, failureCount, results } |
getById | null |
http | { ok: false, status: 0, statusText, error } |
Notify, NotifyGroup, BroadcastAsync, SendEmailAsync | false |
SMSNotifyAsync, WhatsAppNotifyAsync | { success: false, error } |
GetFileAsync | null |
SendFunctionToUserAsync, BroadcastFunctionAsync | never reports failure — always true |
Only promptAgent throws on its own.
// Fails silently — the invoice may never have been created
await context.functions.create("invoices", values);
// Fails loudly
const result = await context.functions.create("invoices", values);
if (!result.success) throw new Error("Could not create invoice: " + result.message);Deciding what should stop the process
Not every failure deserves to abort. Separate the business operation from its side effects:
/** @event afterCreate @entity orders */
// Essential — if this fails the data is wrong
const lines = await context.functions.createBulk("orderlines", buildLines(context.record));
if (!lines.success) {
throw new Error("Order " + context.record.ID + " saved but " + lines.failureCount + " lines failed.");
}
// Best effort — a missing email should not break order processing
const sent = await context.functions.SendEmailAsync(customerEmail, "Order confirmed", body, null);
if (!sent) console.error("Confirmation email to " + customerEmail + " failed for order " + context.record.ID);try / catch
Use it to keep going when a step is allowed to fail:
try {
context.record.Summary = await context.functions.promptAgent("summarizer", context.record.Text);
} catch (e) {
console.error("Summarizer unavailable: " + e.message);
context.record.Summary = null;
}Catching and swallowing an error that should have stopped the process is worse than not catching it. If you cannot recover, re-throw with context added:
try {
await syncToExternalSystem(context.record);
} catch (e) {
throw new Error("CRM sync failed for order " + context.record.ID + ": " + e.message);
}Timeouts
A process gets 30 seconds unless you say otherwise. Exceeding it aborts the run mid-flight — work already committed stays committed.
/** @trigger Nightly reconciliation @event scheduled @schedule 0 2 * * * @timeout 300 */The usual causes are looping over many records, sending messages one at a time, and waiting on slow external calls. Raise @timeout where the work genuinely takes longer, and prefer bulk functions over loops.
Seeing what happened
log and console.log both write to your application log; console.warn and console.error record at higher levels. Log the values you branched on, not just that something failed:
log("Processing order " + context.record.ID + " for client " + context.record.ClientId);
const result = await context.functions.update("orders", context.record.ID, values);
if (!result.success) {
console.error("Update failed for order " + context.record.ID + ": " + result.message);
console.error(values);
}While stepping through a process, console.warn and console.error are not defined and calling either throws a TypeError that will not happen during a normal run. Use console.log or log in code you intend to debug.