DataWeave Mapper Error Handling Using orElseTry

Author: Rahul Dusaje

Scenario: Whenever the data transformation happens through the map operation on the array, the records are processed individually. 

If the problem occurs for any object under transformation, the entire transformation fails for all the objects. Say, for instance, while processing the 10 records, the processing fails (due to DataWeave) for the 6th object – it will not get processed for the next 4 objects. Also, the previously successfully processed 5 objects will lose their new entity.

For example, we have an array with 3 objects:

If the id is typecast as a number but in the input, it comes as a string(Although, it should be handled at the RAML) but I have used it as an example for error generation: it throws the error entirely.

It makes sense to fail for the second object as the id provided is a string but transformation loses its significance for the first and the rest of the objects.

In order to handle this type of error: Try, orElseTry can be used within the mapper to process all the successful records and failed records in a single DataWeave.

Try and orElseTry is available in the DataWeave Runtime Library.

Code: 


%dw 2.0
import try, orElseTry from dw::Runtime
output application/json
---


(payload map (() -> {


   (try (()-> {
       id: $.id as Number,
       name: $.name,
       class: $.class
   }) orElseTry {


       "failureMessage": "object failed with id: " ++ $.id
   })
})).result

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.