Many who use DataWeave face the problem of incorrect data structure while processing the collection of different records. When detecting an incorrect structure, we have to perform appropriate actions based on business needs: replace incorrect data with the predefined default data, decline an incorrect structure, or throw an exception.
In this blog post, we will explore the try function — which is part of a standard dw::Runtime module to optimize error handling that is embedded in DataWeave version 2.0 and above.
Typical use case
As a typical use-case, let’s look at the simple collection of registration dates to make sure that the dates coming from the source have the correct format. We are expecting the format where the date of the month comes first, the month second, and the year third: “dd-MM-yyyy.”
[ { name: "Tom", date: "21-01-2021" }, { name: "Pete", date: "19-04-2021" }, { name: "John", date: "06-15-2021" } ]
The third record has an invalid date and — for simplicity — we would like to detect and replace it with a custom error message.
DataWeave’s try function
DataWeave’s try function evaluates the delegate function and returns an object with success: true and result if the delegate function succeeds, or an object with success: false and error if the delegate function throws an exception.
To demonstrate try functionality, we will catch an obvious error and perform appropriate actions when the dw interpreter throws an exception. Let’s generate a classic division by zero error:
Figure 1. Division by 0
After that, we use the try function to intercept an error and prevent the interpreter from stopping due to an error exception:
Figure 2. “try” function intercepts an error
And now, let’s fix the error but keep using the try function:
Figure 3. try function treats successful results
As shown in figures 1 and 2, if the try function returns an object with the first key “success” set to “false”, then the “error” key is set to the value of an error object. Otherwise, if the “success” key equals “true,” the second key is “success” with the value equal to the result of the guarded function.
Let’s add a match operator to return the result when the guarded function returns error shown in figure 3, otherwise it returns the success message shown in figure 4.
Figure 3. Using try and match operators to process unsuccessful try results
Figure 4. Using try and match operators to process successful try results
We can unify try to check any function, not just a simple mathematical expression by creating the “guard” function which will accept any expression and provide successful or unsuccessful results:
Figure 5. Extrapolating code into a function to guard against errors
How many times have you had your DataWeave code fail because one or few records were malformed?
Let’s apply the “guard” function on the entire collection to capture an individual error.
We will take a collection of dates and names, processing it against the “guard” function:
Figure 6. Processing collection using “guard” function
This example illustrates how the try function can guard any type of collection against incorrect data, generating any kind of output results to make sure that DataWeave code will not fail due to inconsistent incoming data.
Figure 7 illustrate the complete DataWeawe code for this blog post.
%dw 2.0 output application/dw fun guard (fn, errorMessage) = dw::Runtime::try(fn) match { case tr if (tr.success) -> tr.result else -> errorMessage // error message to be shown on success: "false" } var array = [ { name: "Tom", date: "21-01-2021" }, { name: "Pete", date: "19-04-2021" }, { name: "John", date: "06-15-2021" } ] --- array map guard( ( () -> $.date as Date { format: "dd-MM-yyyy" } ), "$($.date) is incorrect date" )
Figure 7. Final DataWeave code
Summary
In this blog post, we’ve seen just one of many error-handling examples that can be performed with DataWeave.
You can learn more DataWeave best practices in the instructor-led Anypoint Platform Development: DataWeave 2.0 course available online or in person. Visit our training homepage for more information about class availability and to discover other classes we have on offer.