fail
The fail statement terminates the script immediately with a failure status. It accepts an optional string message that must appear on the same line.
fail # Failure exit, default message
fail "Invalid input" # Failure exit with message
Behaviour
- The script stops executing at the
failstatement - The
Datacontext is returned to the host with all modifications made up to that point - The message is reported to the host as an error
- The host environment treats the script as having failed
Message Must Be on the Same Line
The message is part of the fail statement syntax. If placed on a separate line, it becomes an independent string expression statement - not a fail message.
# CORRECT
fail "Missing required field"
# INCORRECT - "Missing required field" is a separate statement
fail
"Missing required field"
Intended Use
Use fail for business logic validation errors and rule violations - situations where the script cannot produce a valid result. It is not intended for debugging.
if Data.orders is null or Data.orders is not array then
fail "Orders must be an array"
end
if AnyByField(Data.orders, "amount", "<", 0) then
fail "Negative order amounts are not allowed"
end
Comparison with return
| Keyword | Script Succeeds? | Message Severity |
|---|---|---|
return | Yes | Info |
fail | No | Error |
Both keywords return the Data context to the host with all mutations applied. See return for the success counterpart.