Search code examples
mulemule-studiomule-componentmule-esb

Mulesoft - How to find Http error payload in exception object?


How to find Http error payload in exception object ?

System 1 -> Call's Mule server . Mule server -> Docusign / some one else.

Docusign / some one returns 400 error with json payload (includes errorCode etc)

I need to return the exact details back to System 1.

how will i do this?

1) I did try to add all codes in success codes 200..599, then even error is having 200 ok . This will cause more problem, because I have to add lot of choice routing as some flows are having more data conversion.

2) Ideal soln -> Raise exception as usual. This has to be caught in choice exception. Now I have to send the same status_code and the json response back to the caller.

Issue -? how can i find the payload returned by HTTP in exception object. Where is stored in exception object.


Solution

  • Here is the sample- one way of handling this kind of scenario , use validation Component to throw exception and capture in your mainflow either by exceptionHandling or by APIKIT ExceptionHandling

     <flow name="routeexceptionFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
        <logger level="INFO" doc:name="Logger"/>
        <http:request config-ref="HTTP_Request_Configuration" path="/in" method="POST" doc:name="HTTP">
            <http:success-status-code-validator values="200..599"/>
        </http:request>
        <validation:is-false config-ref="Validation_Configuration" message="#[payload]" exceptionClass="nz.co.exception.BadRequestException" expression="#[message.inboundProperties['http.status'] ==400]" doc:name="Validate Bad Request"/>
        <validation:is-false config-ref="Validation_Configuration" message="#[payload]" expression="#[message.inboundProperties['http.status'] !=200]" doc:name="Capture all other Exceptions"/>
        <choice-exception-strategy doc:name="Choice Exception Strategy">
            <catch-exception-strategy when="#[exception.causeMatches('nz.co.exception.BadRequestException')]" doc:name="Catch Exception Strategy">
                <set-payload value="#[exception.message]" doc:name="Set Payload"/>
                <logger level="INFO" doc:name="Logger"/>
            </catch-exception-strategy>
            <catch-exception-strategy doc:name="Catch Exception Strategy">
                <logger message="****log all other exception****" level="INFO" doc:name="Logger"/>
            </catch-exception-strategy>
        </choice-exception-strategy>
    </flow>
    

    Add this class in your java folder

      package nz.co.exception;
    
       public class BadRequestException extends Exception {
       private static final long serialVersionUID = 8546839078847602529L;
    
        public BadRequestException() {
        super();
        }
    
        public BadRequestException(String message) {
        super(message);
        }
    
       public BadRequestException(Throwable t) {
        super(t);
       }
    
      public BadRequestException(String message, Throwable t) {
        super(message, t);
      }
    }
    

    In your project if you are using APIKIT Router , then instead of using exceptionHandling as above, add directly nz.co.exception.BadRequestException in 400 ReturnCode apikitExceptionHandling.