specifying a default exception strategy
TRANSCRIPT
Specifying a default exception strategy
Abstract
• The main motto of this PPT is How to use Specifying a default exception strategy in our applications.
Example
.mflow• <?xml version="1.0" encoding="UTF-8"?>
• <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
• xmlns:spring="http://www.springframework.org/schema/beans" • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"• xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-current.xsd• http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd• http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">• <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8088" doc:name="HTTP
Listener Configuration"/>• <flow name="ExceptionHandlingFlow">• <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>• <logger message="-Flow started" level="INFO" doc:name="Logger"/>• <set-property propertyName="token"
value="#[message.inboundProperties.'http.query.params'.token.toString()]" doc:name="Property"/>• <logger message="--Token value: ----#[message.outboundProperties.token]" level="INFO" doc:name="Logger"/>• <set-payload value="{• "token":"#[message.outboundProperties.token]"• }" doc:name="Set Payload"/>• </flow>• </mule>
• Global flow:• <?xml version="1.0" encoding="UTF-8"?>
• <mule xmlns:cluster="http://www.mulesoft.org/schema/mule/ee/cluster" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
• xmlns:spring="http://www.springframework.org/schema/beans" • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"• xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-
beans-current.xsd• http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd• http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">• <configuration defaultExceptionStrategy-ref="GlobalCatch_Exception_Strategy" doc:name="Configuration">• <http:config useTransportForUris="false"/>• </configuration>• <catch-exception-strategy name="GlobalCatch_Exception_Strategy">• <set-property propertyName="http.status" value="400" doc:name="Property"/>• <set-payload value="{• "message":"Bad Request"• }" doc:name="Set Payload"/>• </catch-exception-strategy>• </mule>
• Output:• If the input is valid input:• INFO 2016-12-23 10:03:37,163
[[ExceptionHandling].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: -Flow started
• INFO 2016-12-23 10:03:37,233 [[ExceptionHandling].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: --Token value: ----a1b2c3d4
• If the input is invalid input:
• INFO 2016-12-23 10:04:02,030 [[ExceptionHandling].HTTP_Listener_Configuration.worker.02] org.mule.api.processor.LoggerMessageProcessor: -Flow started
• ERROR 2016-12-23 10:04:02,072 [[ExceptionHandling].HTTP_Listener_Configuration.worker.02] org.mule.exception.CatchMessagingExceptionStrategy:
• ********************************************************************************• Message : Execution of the expression
"message.inboundProperties.'http.query.params'.token.toString()" failed. (org.mule.api.expression.ExpressionRuntimeException).
• Payload : {NullPayload}• Element XML : <set-property propertyName="token"
value="#[message.inboundProperties.'http.query.params'.token.toString()]" doc:name="Property"></set-property>
• Payload Type : org.mule.transport.NullPayload• Element : /ExceptionHandlingFlow/processors/1 @ ExceptionHandling:ExceptionHandling.xml:13
(Property)• --------------------------------------------------------------------------------• Root Exception stack trace:• java.lang.NullPointerException• at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)• at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)• at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
References
• https://docs.mulesoft.com/mule-user-guide/v/3.6/error-handling