0

I am using Anypoint 6.1 and Mule 3.8.1 and am looking at extra error handling to the global exception handling created by the API kit.

I am looking to add:

  • 408 Request Timeout
  • 429 Too many requests
  • 500 Internal Server Error
  • 503 Service unavailable

What is the best approach to handle these errors?

I have looked to use the API Kit exception handling but I cannot find an expected type to use for the above. Does anyone know where I can find the expected types to use for these errors?

XML API Kit Exception mappings:

<apikit:mapping-exception-strategy name="apiKitGlobalExceptionMapping">
    <apikit:mapping statusCode="400">
        <apikit:exception value="org.mule.module.apikit.exception.BadRequestException" />
        <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
        <set-payload value="{ &quot;message&quot;: &quot;Bad request&quot; }" doc:name="Set Payload"/>
    </apikit:mapping>      
    <apikit:mapping statusCode="404">
        <apikit:exception value="org.mule.module.apikit.exception.NotFoundException" />
        <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
        <set-payload value="{ &quot;message&quot;: &quot;Resource not found&quot; }" doc:name="Set Payload"/>
    </apikit:mapping>
    <apikit:mapping statusCode="405">
        <apikit:exception value="org.mule.module.apikit.exception.MethodNotAllowedException" />
        <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
        <set-payload value="{ &quot;message&quot;: &quot;Method not allowed&quot; }" doc:name="Set Payload"/>
    </apikit:mapping>
    <apikit:mapping statusCode="406">
        <apikit:exception value="org.mule.module.apikit.exception.NotAcceptableException" />
        <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
        <set-payload value="{ &quot;message&quot;: &quot;Not Acceptable&quot; }" doc:name="Set Payload"/>
    </apikit:mapping>    
    <apikit:mapping statusCode="408">
        <apikit:exception value="java.util.concurrent.TimeoutException"/>
        <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
        <set-payload value="{ &quot;message&quot;: &quot;Request Timeout&quot; }" doc:name="Set Payload"/>
    </apikit:mapping>  
    <apikit:mapping statusCode="415">
        <apikit:exception value="org.mule.module.apikit.exception.UnsupportedMediaTypeException" />
        <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
        <set-payload value="{ &quot;message&quot;: &quot;Unsupported media type&quot; }" doc:name="Set Payload"/>
    </apikit:mapping>  
    <apikit:mapping statusCode="429">
        <apikit:exception value="java.lang.Exception"/>
        <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
        <set-payload value="{ &quot;message&quot;: &quot;Unsupported media type&quot; }" doc:name="Set Payload"/>
    </apikit:mapping>  
    <apikit:mapping statusCode="500">
        <apikit:exception value="java.lang.Exception"/>
        <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
        <set-payload value="{ &quot;message&quot;: &quot;Unsupported media type&quot; }" doc:name="Set Payload"/>
    </apikit:mapping>  
    <apikit:mapping statusCode="503">
        <apikit:exception value="java.lang.Exception"/>
        <set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
        <set-payload value="{ &quot;message&quot;: &quot;Unsupported media type&quot; }" doc:name="Set Payload"/>
    </apikit:mapping>          
</apikit:mapping-exception-strategy>

Thanks

user3165854
  • 1,505
  • 8
  • 48
  • 100

1 Answers1

0

APIKIT matches the exception based on the apikit:exception value defined. So, it should be 1:1 for the status codes to the exception class defined. In your case, if you want to set 500, then have it to return only when the exception matches org.mule.module.apikit.exception.InternalServerErrorException

Alagappan
  • 66
  • 2
  • There are other exceptions that can be interpreted as 500 like java.lang.Exception. You need to decide which one map to each code. – aled Oct 15 '18 at 12:20