How to create custom error message when our message flow hit fault sequence

When we use WSO2 ESB fault sequence may hit due to different reasons. In such cases we normally send error message as defined in fault sequence. In this blog i will explain how we can modify fault sequence to send customized error to client.

To try this i created simple proxy which directly go to fault sequence. In your case it can happen due to some other reason

<proxy name="testp"
transports="https http"
startOnLoad="true"
trace="disable">
<target>
<inSequence>
<sequence key="fault"/>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
</proxy>

Inside fault sequence log message and call my custom sequence named convert.

<sequence name="fault">
<log>
<property name="Inside fault sequence============" expression="get-property('test')"/>
</log>
<sequence key="convert"/>
<drop/>
</sequence>

My convert sequence will generate custom error message and send it to client application.

<sequence name="convert">
<payloadFactory media-type="xml">
<format>
<am:fault xmlns:am="http://wso2.org/apimanager">
<am:code>$1</am:code>
<am:type>Status report</am:type>
<am:message>Runtime Error</am:message>
<am:description>$2</am:description>
</am:fault>
</format>
<args>
<arg evaluator="xml" expression="$ctx:ERROR_CODE"/>
<arg evaluator="xml" expression="$ctx:ERROR_MESSAGE"/>
</args>
</payloadFactory>
<property name="RESPONSE" value="true"/>
<header name="To" action="remove"/>
<property name="HTTP_SC" value="555" scope="axis2"/>
<property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
<property name="ContentType" scope="axis2" action="remove"/>
<property name="Authorization" scope="transport" action="remove"/>
<property name="Access-Control-Allow-Origin" value="*" scope="transport"/>
<property name="Host" scope="transport" action="remove"/>
<property name="Accept" scope="transport" action="remove"/>
<property name="messageType" value="application/json" scope="axis2"/>
<send/>
</sequence>

Client will recieve following error message as we difined in fault sequence.

Status Code: 555
Accept-Encoding: gzip, deflate
Accept-Language: null
Connection: keep-alive
Content-Type: application/json
Date: Tue, 20 Oct 2015 06:19:51 GMT
Server: WSO2-PassThrough-HTTP
Transfer-Encoding: chunked
access-control-allow-origin: *

No comments:

Post a Comment

Empowering the Future of API Management: Unveiling the Journey of WSO2 API Platform for Kubernetes (APK) Project and the Anticipated Alpha Release

  Introduction In the ever-evolving realm of API management, our journey embarked on the APK project eight months ago, and now, with great a...