Error Handling in Batch Job
Handling error in batch job becomes quite a tricky thing as we have to understand how batch job take care of different exception or error.
Any error occur inside batch job will never propagate to Main flow error handler – so below On Error Continue will not be called. But if error occurs in the Source section then it will come to Main Flow error handler.
In both Process Records and On Complete section if any error occurred, it will not be thrown to above/main flow. Main flow Error is never called by any error occurring in Batch Job processing.
Also while doing the configuration of Batch Job, Please select the Max Failed Records value accordingly
- maxFailedRecords – 0 – Stop processing when a failed record is found.
maxFailedRecords – -1 – Continue processing indefinitely, regardless of the number of failed records.
maxFailedRecords – integer – Continue processing until reaching maximum number of failed records.
To handle error within Batch Job
Different options we can go for as per our business need
1 – Add try catch activity in Batch Steps
This help in catching the actual error trace for the record and doing the required error handling
After catching the exception we can have two scenarios e.g. On error continue or On error propagate
On error continue – Choose this if we want to consume the error and resume the process after try-catch activity
So in below the execution will be like
-
-
- Raise error
- Logger for Try Catch
- After Try Catch
- Batch_Step_Failed_Records will be not called as the records in never marked as error
-
On error propagate – Choose this if we want to throw error at Batch Job level and want to stop the processing for the error record
So in below the execution will be like
-
-
- Raise error
- Logger for Try Catch
- Batch_Step_Failed_Records will be called as the records in marked as error
-
2- Add a batch step with accept policy as “ONLY_FAILURES” to know all those records which have failed in earlier batch steps. This will not provide the actual error trace but only provide the records which failed. Actual error trace will be in logs which can be analysed later
So in above image – Failed Records Logger – we can only see the payload record which is failed but not actual error associated with it
Similarly if we have to perform any bulk operation then we can add aggregator
Batch Aggregator with Aggregator as Streaming can collect all the failed records and perform the logic like sending the details in Email, DB or Queue (as per our error handling need)
Sample application – batch-job-sample-error-handling
Sample CSV – Sample
This use case is based on our previous tutorial – Process CSV file using Batch Job
will only failures step catch expression errors?
In batch for any error, it will go to immediate next batch step which has the acceptance policy of All or only failure.
Thanks,
Mulesy Team
Hi, please address my query, what happens if there is a failure in batch aggregator step. For example if one is uploading 200 records to Salesforce and 5 of them fail because of foreign key constraint. What happens then?
Cheers
Dann
That will not be shown in bulk job log at the salesforce end. In the mule it will not have any impact.