Problem
My script failed when try lacked an explicit import from dw::Runtime. The runtime reported Unable to resolve reference of: try during execution. The documentation describes try as a function that returns a Result object for handling errors docs.
Input
{
"order": { "id": "ORD-2001", "total": "12.5O" }
}
Working configuration
%dw 2.0
// try() lives in dw::Runtime — not imported by default
import try from dw::Runtime
// RFC 7807 media type is accepted as-is by the JSON writer
output application/problem+json
var attempt = try(() -> payload.order.total as Number)
---
if (attempt.success) { total: attempt.result }
else {
"type": "https://example.com/problems/invalid-total",
"title": "Invalid order total",
"status": 400,
"detail": attempt.error.message,
"instance": "/orders/" ++ payload.order.id,
"errorKind": attempt.error.kind
}
Output
{
"type": "https://example.com/problems/invalid-total",
"title": "Invalid order total",
"status": 400,
"detail": "Cannot coerce String (12.5O) to Number",
"instance": "/orders/ORD-2001",
"errorKind": "InvalidNumberException"
}
The detail field contains the string Cannot coerce String (12.5O) to Number. The errorKind field holds the value InvalidNumberException.
The trap
The missing import causes a resolution failure when the script executes. Running fail_noimport.dwl produces an exit code of 255 and reports Unable to resolve reference of: try.
What I do now
I always add import try from dw::Runtime at the top of every script that uses the function. I verify the import exists before running the transformation to prevent resolution errors.
Runs shown: DataWeave CLI 2.12.2
104 DataWeave patterns + 8 Exchange modules with 208 MUnit tests: github.com/shakarbisetty/mulesoft-cookbook | 60-second walkthroughs: youtube.com/@SanThaParv


Top comments (0)