Author: Mohammad Mazhar Ansari

Usually, every middleware has some or other kind of alter notification implementation for error scenarios. We are trying to explore the option of generating Error Report to combine all errors occurred using MuleSoft.

Objective of this blog is to create a generic component which can be reused across all projects within MuleSoft.

MuleSoft Options to Notify Error Notification

  • CloudHub Notification and CloudHub Alter
  • Using Send EMail activity


  • Limited number of Alters can be configured
  • Individual mail will be sent for each alert which will cause multiple mails in Inbox

Overview of Error Reporting:

  • Whenever error occurs, it will create an error object and capture the key identifier for record along with error message
  • Error Object will be stored in Object Store (Object Store can be replaced by other persistent storage)
  • A scheduler will run on specified time interval and accumulate all the error and send the report as attachment


  • Reduced number of mails
  • Accumulated Report for the errors
  • Can be used to generate report and present to different teams
  • Run Application

Sample Mule Flow: 

This flow is to showcase uses of the error reporting component.

  • Scheduler will run every minute
  • Payload will be set as an Array with value from 1 to 100
  • Batch JOB Process data in batches and in parallel
  • Generate error will throw error for every odd record
  • Accumulate error will set the accumulate_error variable
   "objectType": "ObjectType",
   "keys": [
       "name": "Timestamp",
       "value": "2020-07-08T14:44:38.695Z"
       "name": "Flow Name",
       "value": "FlowName"
       "name": "Component Name",
       "value": "ComponentName"
       "name": "Primary Key",
       "value": "PrimaryKey"
   "error": {
     "description": "Error Description"
  • ObjectType, FlowName, ComponentName, PrimaryKey can be changed based on need also more name value elements can be added based on requirement
  • Notify Error will aggregate all the error Objects and generate error Report
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="" xmlns:batch=""
	xmlns:doc="" xmlns:xsi="" xsi:schemaLocation="">
	<flow name="batch-error-reportFlow" doc:id="7f68b313-86b1-4e28-9e4d-1f90a6b1b283" >
		<scheduler doc:name="Scheduler" doc:id="91408ed3-a06c-4187-af37-576f321d9df7" >
			<scheduling-strategy >
				<fixed-frequency frequency="1" timeUnit="MINUTES"/>
		<ee:transform doc:name="payload" doc:id="248821f4-81fd-42f3-940c-a1b743afcfd4" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/java
1 to 100]]></ee:set-payload>
		<batch:job jobName="batch-error-reportBatch_Job" doc:id="45260843-3555-4d6a-81ee-ae2f263599fc" >
			<batch:process-records >
				<batch:step name="Batch_Step" doc:id="fc5904ae-7726-4b89-a8a8-e78a6d5b147b" >
					<try doc:name="Try" doc:id="5df25f3d-c5bf-4ff8-bae6-a2f822de0ffa" >
						<ee:transform doc:name="generateError" doc:id="942677b6-050a-4e90-950e-9ec888729a99" >
							<ee:message >
								<ee:set-payload ><![CDATA[%dw 2.0
output application/java
if ((payload mod 2) == 0)
    payload * 2
    payload / 0]]></ee:set-payload>
						<error-handler >
							<on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" doc:id="595d45a3-021c-40b5-a5e7-be8fb7580f0e" type="ANY" >
								<ee:transform doc:name="accumulateError" doc:id="0e9af43f-70c3-40d8-9b05-f6bd6ce6ab91" >
									<ee:message />
									<ee:variables >
										<ee:set-variable variableName="accumulate_error" ><![CDATA[%dw 2.0
output application/json
	"objectType": "ObjectType",
			"name":     "Timestamp",
			"value":    now()
		},		{
			"name":     "Flow Name",
			"value":    "batch-error-reportFlow"
		},		{
			"name":     "Component Name",
			"value":    "Batch JoB"
			"name":     "Primary Key",
			"value":    payload 
		"description": write(error,'application/json')
								<flow-ref doc:name="accumulate-error-os" doc:id="caf43a4a-2829-4c2c-8bb4-c90db4b03d08" name="accumulate-error-os" />
			<batch:on-complete >
				<flow-ref doc:name="notify-errors" doc:id="17dfb5db-8a04-4dd1-b8cf-2ec43fe5eab3" name="notify-errors"/>

Error Reporting Flow:

  • Scheduler process will run on specified internal like every one hour
  • It will get all the data from Object Store (Persistent Store)
  • Combine data based on Object Type
  • Send mail and attach report
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:os=""
	xmlns:scripting="" xmlns:ee=""
	xmlns:context="" xmlns:tracking=""
	xmlns="" xmlns:doc=""
	xmlns:spring="" xmlns:xsi="" xsi:schemaLocation="">
	<!-- <email:smtp-config name="Email_SMTP" doc:name="Email SMTP" doc:id="1db316ac-7f26-4647-ac7d-821f8e363eb3" >
		<email:smtp-connection host="" port="465" user="" password="Sidbb@8520" />
	</email:smtp-config> -->
	<sub-flow name="accumulate-error-os" doc:id="a2560edc-bf0b-4991-9f99-a43d678a6959" >
		<try doc:name="Try" doc:id="11b9a9e6-dd4c-4558-98b3-fa1aba03e69b" >
			<os:store doc:name="store error obj" doc:id="599bf90d-23ed-4cb4-8dd5-33d61e467169" key='#["accumulate_error_" ++ uuid()]' failOnNullValue="false" objectStore="Object_store">
			<os:value><![CDATA[#[output application/java
"$(write(vars.accumulate_error, "application/json"))"]]]></os:value>
			<error-handler >
				<on-error-propagate enableNotifications="true" logException="true" doc:name="On Error Propagate" doc:id="c2af34bd-c4d3-43d8-87af-0de96ff4697c" >
					<logger level="ERROR" doc:name="Logger" doc:id="ef5d2e51-9a3a-4768-ad8b-e07eb09945c6" message="Unable to store data in Object Store"/>
	<flow name="notify-errors" doc:id="c16176c0-be34-4826-af0e-1378a58e9cc8" initialState="started">
        <!-- <scheduler doc:name="Scheduler: Fetch Error Info" doc:id="22a5436b-920c-4ae6-997b-53bab8911efe" >
            <scheduling-strategy >
				<fixed-frequency frequency="${scheduler.frequency}" timeUnit="${scheduler.timeUnit}" />
		<!-- <db:select doc:name="Retrieve Object and Emails" doc:id="3f0a103b-8608-4f14-95c3-e7a5f632f4b0" config-ref="Database_Config" target="muleEmails" transactionalAction="NOT_SUPPORTED">
		</db:select> -->
		<scheduler doc:name="Scheduler" doc:id="41d205a0-9a9b-4fb0-8030-4a912b636794" >
			<scheduling-strategy >
				<fixed-frequency frequency="3" timeUnit="MINUTES"/>
		<os:retrieve-all doc:name="get all error objects" doc:id="9e09e34b-065f-4625-b0c8-a7812624e222" targetValue='#[%dw 2.0
output application/json
flatten((payload..) map ((item, index) -&gt;
     read (item, "application/json")
)) groupBy ((item, index) -&gt; item.objectType )]' objectStore="Object_store" target="retrievedErrors"/>
		<choice doc:name="Choice" doc:id="33a0d64d-50cb-4b35-8d41-6513a74096eb">
					<when expression="#[sizeOf(vars.retrievedErrors) &gt; 0]">
						<foreach doc:name="For Each" doc:id="4598b775-6077-4b08-8c76-3087f23b426a" collection="#[vars.retrievedErrors]">
			<ee:transform doc:name="payload" doc:id="f3bb60ba-98d7-4441-8259-cf5be9b38f0c">
					<ee:set-payload><![CDATA[%dw 2.0
output application/csv
fun arrToObj(arr) = arr reduce ((env, obj={}) -> obj ++ env)
fun combineKeys(keys) = (arrToObj (keys map ((item, index) ->
    str: ++ ": " ++ item.value
)).*str joinBy " | ")
fun nameValuePairToJson(keys) = (keys map ((item, index) ->
    "$(": item.value
payload[0] map ((item, index) -> arrToObj(nameValuePairToJson(flatten(item.keys))) ++ {
    ErrorDesc: item.error.description
					<ee:set-variable variableName="objectType"><![CDATA[%dw 2.0
output application/json
payload pluck ($$)]]></ee:set-variable>

			<set-variable value='#["ErrorReport_" ++ (now() as String {format: "yyyyMMddHHmmssSSS"}) ++ ".csv"]' doc:name="Attachment fileName" doc:id="d3881afb-400c-4330-979b-75a7f4e3c6d7" variableName="fileName" />
			<email:send doc:name="Send Error Report via Email" doc:id="4355b577-981c-429f-a666-a2a6640c2bcf" fromAddress="${mail.from}" subject="Error Report" config-ref="Gmail_SMTP">
					<email:to-address value="${}" />
					<email:to-address value="${}" />
					<email:content><![CDATA[#[%dw 2.0
output text/plain
"Failed Records logs are added in attachments please refer same."]]]></email:content>
				<email:attachments><![CDATA[#[%dw 2.0
output text/csv
          '$(vars.fileName)': payload
				<logger level="INFO" doc:name="Logger" doc:id="a302abc7-6af0-4f00-82b7-7a492fa84c37" message="All Records are successfully processed." />
		<os:clear doc:name="Clear" doc:id="21171d9e-1474-4206-9203-3a0695c0bcb0" objectStore="Object_store" />
		<error-handler >
			<on-error-propagate enableNotifications="true" logException="true" doc:name="On Error Propagate" doc:id="3ec241cd-ae72-4096-add5-41fa1f0aeed1" >
				<logger level="ERROR" doc:name="Logger" doc:id="6e026028-0381-4840-bd05-fb8d5edb8fa3" message="Unable to notify error"/>

Sample Error Report:

Leave a Comment