Reference Exception Strategy
Abstract
• The main motto of this PPT is How to use
Reference Exception Strategy in our
applications.
Introduction
• You can create one or more global exception
strategies to reuse in flows throughout your
entire Mule application. First, create a global
exception strategy, then add a Reference
Exception Strategy to a flow to apply the error
handling behavior of a specific global
exception strategy.
Example
.mflow
• <?xml version="1.0" encoding="UTF-8"?>
• <mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:json="http://www.mulesoft.org/schema/mule/json" 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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.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="8087" doc:name="HTTP
Listener Configuration"/>
• <flow name="JsonSchemaValidatorFlow">
• <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
• <json:validate-schema schemaLocation="Schema.json" doc:name="Validate JSON Schema"/>
• <logger message="--Valid input" level="INFO" doc:name="Logger"/>
• <exception-strategy ref="JsonSchemaValidatorCatch_Exception_Strategy" doc:name="Reference Exception
Strategy"/>
• </flow>
• <catch-exception-strategy name="JsonSchemaValidatorCatch_Exception_Strategy">
• <set-property propertyName="http.status" value="400" doc:name="Property"/>
• <set-payload value="Invalid input request" doc:name="Set Payload"/>
• </catch-exception-strategy>
• </mule>
• Place the schema file in src/main/resources
• Output:
• If the input is valid input:
• INFO 2016-12-22 09:23:32,157
[[JsonSchemaValidator].HTTP_Listener_Config
uration.worker.02]
org.mule.api.processor.LoggerMessageProcess
or: --Valid input
• If the input is invalid input:
• ERROR 2016-12-22 09:23:15,139 [[JsonSchemaValidator].HTTP_Listener_Configuration.worker.02] org.mule.exception.DefaultMessagingExceptionStrategy:
• ********************************************************************************
• Message : Json content is not compliant with schema
• com.github.fge.jsonschema.core.report.ListProcessingReport: failure
• --- BEGIN MESSAGES ---
• error: object has missing required properties (["firstName"])
• level: "error"
• schema: {"loadingURI":"file:/C:/Users/sivachandra.mandalap/AnypointStudio/workspace1/.mule/apps/JsonSchemaValidator/classes/Schema.json#","pointer":""}
• instance: {"pointer":""}
• domain: "validation"
• keyword: "required"
• required: ["firstName","lastName"]
• missing: ["firstName"]
• --- END MESSAGES ---
• Payload : {"lastName":"def","age":10}
• Payload Type : java.lang.String
• Element : /JsonSchemaValidatorFlow/processors/0 @ JsonSchemaValidator
• --------------------------------------------------------------------------------
• Root Exception stack trace:
• org.mule.module.json.validation.JsonSchemaValidationException: Json content is not compliant with schema
• com.github.fge.jsonschema.core.report.ListProcessingReport: failure
• --- BEGIN MESSAGES ---
• error: object has missing required properties (["firstName"])
• level: "error"
• schema: {"loadingURI":"file:/C:/Users/sivachandra.mandalap/AnypointStudio/workspace1/.mule/apps/JsonSchemaValidator/classes/Schema.json#","pointer":""}
• instance: {"pointer":""}
• domain: "validation"
• keyword: "required"
• required: ["firstName","lastName"]
• missing: ["firstName"]
• --- END MESSAGES ---
• at org.mule.module.json.validation.JsonSchemaValidator.validate(JsonSchemaValidator.java:286)
• at org.mule.module.json.validation.ValidateJsonSchemaMessageProcessor.process(ValidateJsonSchemaMessageProcessor.java:46)
• at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
• at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:108)
• at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
• Flow of execution:
1. URL to trigger the service from browser
Valid Request:
• Invalid Request:
References
• https://docs.mulesoft.com/mule-user-
guide/v/3.7/reference-exception-strategy

Reference exception strategy

  • 1.
  • 2.
    Abstract • The mainmotto of this PPT is How to use Reference Exception Strategy in our applications.
  • 3.
    Introduction • You cancreate one or more global exception strategies to reuse in flows throughout your entire Mule application. First, create a global exception strategy, then add a Reference Exception Strategy to a flow to apply the error handling behavior of a specific global exception strategy.
  • 4.
  • 5.
    .mflow • <?xml version="1.0"encoding="UTF-8"?> • <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" 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/json http://www.mulesoft.org/schema/mule/json/current/mule-json.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="8087" doc:name="HTTP Listener Configuration"/> • <flow name="JsonSchemaValidatorFlow"> • <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/> • <json:validate-schema schemaLocation="Schema.json" doc:name="Validate JSON Schema"/> • <logger message="--Valid input" level="INFO" doc:name="Logger"/> • <exception-strategy ref="JsonSchemaValidatorCatch_Exception_Strategy" doc:name="Reference Exception Strategy"/> • </flow> • <catch-exception-strategy name="JsonSchemaValidatorCatch_Exception_Strategy"> • <set-property propertyName="http.status" value="400" doc:name="Property"/> • <set-payload value="Invalid input request" doc:name="Set Payload"/> • </catch-exception-strategy> • </mule>
  • 6.
    • Place theschema file in src/main/resources
  • 7.
    • Output: • Ifthe input is valid input: • INFO 2016-12-22 09:23:32,157 [[JsonSchemaValidator].HTTP_Listener_Config uration.worker.02] org.mule.api.processor.LoggerMessageProcess or: --Valid input
  • 8.
    • If theinput is invalid input: • ERROR 2016-12-22 09:23:15,139 [[JsonSchemaValidator].HTTP_Listener_Configuration.worker.02] org.mule.exception.DefaultMessagingExceptionStrategy: • ******************************************************************************** • Message : Json content is not compliant with schema • com.github.fge.jsonschema.core.report.ListProcessingReport: failure • --- BEGIN MESSAGES --- • error: object has missing required properties (["firstName"]) • level: "error" • schema: {"loadingURI":"file:/C:/Users/sivachandra.mandalap/AnypointStudio/workspace1/.mule/apps/JsonSchemaValidator/classes/Schema.json#","pointer":""} • instance: {"pointer":""} • domain: "validation" • keyword: "required" • required: ["firstName","lastName"] • missing: ["firstName"] • --- END MESSAGES --- • Payload : {"lastName":"def","age":10} • Payload Type : java.lang.String • Element : /JsonSchemaValidatorFlow/processors/0 @ JsonSchemaValidator • -------------------------------------------------------------------------------- • Root Exception stack trace: • org.mule.module.json.validation.JsonSchemaValidationException: Json content is not compliant with schema • com.github.fge.jsonschema.core.report.ListProcessingReport: failure • --- BEGIN MESSAGES --- • error: object has missing required properties (["firstName"]) • level: "error" • schema: {"loadingURI":"file:/C:/Users/sivachandra.mandalap/AnypointStudio/workspace1/.mule/apps/JsonSchemaValidator/classes/Schema.json#","pointer":""} • instance: {"pointer":""} • domain: "validation" • keyword: "required" • required: ["firstName","lastName"] • missing: ["firstName"] • --- END MESSAGES --- • at org.mule.module.json.validation.JsonSchemaValidator.validate(JsonSchemaValidator.java:286) • at org.mule.module.json.validation.ValidateJsonSchemaMessageProcessor.process(ValidateJsonSchemaMessageProcessor.java:46) • at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27) • at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:108) • at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
  • 9.
    • Flow ofexecution: 1. URL to trigger the service from browser Valid Request:
  • 10.
  • 11.