Exceptionhandling at policy level is throwing generic error

Not applicable

Hi ,

I am working on exception handling at policy level sample.I have created a proxy with no endpoint.

I have called a backend service which throws HTTP Status code 404.I have attached a RaiseFaultPolicy to the proxy endpoint.When backend call got failed ,it has to call RaiseFaultPolicy based on condition written.But I am getting a generic error .Could you please help on this .I wanted to display the error message prepared in RaiseFaultPolicy.

Here are details.

Proxy : exceptionhandling-policyleve

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <APIProxy revision="2" name="ExceptionHandling-PolicyLevel"> <Basepaths>/exceptionhandling-policylevel</Basepaths> <ConfigurationVersion majorVersion="4" minorVersion="0"/> <CreatedAt>1480511423220</CreatedAt> <CreatedBy>nvraghavendra254@gmail.com</CreatedBy> <Description/> <DisplayName>ExceptionHandling-PolicyLevel</DisplayName> <LastModifiedAt>1480666142732</LastModifiedAt> <LastModifiedBy>nvraghavendra254@gmail.com</LastModifiedBy> <Policies> <Policy>Service-Callout-1</Policy> <Policy>Service_callout_Error</Policy> </Policies> <ProxyEndpoints> <ProxyEndpoint>default</ProxyEndpoint> </ProxyEndpoints> <Resources/> <Spec/> <TargetServers/> <TargetEndpoints/> <validate>false</validate> </APIProxy>

Proxy endpoint:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ProxyEndpoint name="default"> <Description/> <FaultRules> <FaultRule name="Service_callout_Error"> <Step> <Name>Service_callout_Error</Name> <Condition>(Response.status.code = 404)or(calloutResponse.message.content Like "*Resource not found*")</Condition> </Step> </FaultRule> </FaultRules> <PreFlow name="PreFlow"> <Request> <Step> <Name>Service-Callout-1</Name> </Step> </Request> <Response/> </PreFlow> <PostFlow name="PostFlow"> <Request/> <Response/> </PostFlow> <Flows/> <HTTPProxyConnection> <BasePath>/exceptionhandling-policylevel</BasePath> <Properties/> <VirtualHost>default</VirtualHost> </HTTPProxyConnection> <RouteRule name="noroute"/> </ProxyEndpoint>

Service callout policy calls backend(Service-Callout-1):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-Callout-1"> <DisplayName>Service Callout-1</DisplayName> <Properties/> <Request clearPayload="true" variable="myRequest"> <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables> </Request> <Response>calloutResponse</Response> <HTTPTargetConnection> <Properties/> <URL>http://www.thomas-bayer.com/sqlrest/CUSTOMER/44444444444444</URL> </HTTPTargetConnection> </ServiceCallout>

RaiseFaultPolicy(Service_callout_Error):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <RaiseFault async="false" continueOnError="false" enabled="true" name="Service_callout_Error"> <DisplayName>Service_callout_Error</DisplayName> <Properties/> <FaultResponse> <Set> <Headers/> <Payload contentType="text/plain"/> <StatusCode>500</StatusCode> <ReasonPhrase>Server Error</ReasonPhrase> </Set> </FaultResponse> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> </RaiseFault>

Response I am getting :

{"fault":{"faultstring":"Execution of ServiceCallout Service-Callout-1 failed. Reason: ResponseCode 404 is treated as error","detail":{"errorcode":"steps.servicecallout.ExecutionFailed"}}}
0 9 716
9 REPLIES 9

Hi @veeraraghavendra

Please find the code below:

1) Service Callout policy: I have included a property - success.codes with 4xx as well so that Edge treats it as a success and proceeds with the flow. More about this and other properties here

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-Callout-1">
    <DisplayName>Service Callout-1</DisplayName>
    <Properties/>
    <Request clearPayload="true" variable="myRequest">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    </Request>
    <Response>calloutResponse</Response>
    <HTTPTargetConnection>
        <URL>http://www.thomas-bayer.com/sqlrest/CUSTOMER/44444444444444</URL>
        <Properties>
            <Property name="success.codes">1xx,2xx,3xx,4xx</Property>
        </Properties>
    </HTTPTargetConnection>
</ServiceCallout>

2) No change to your Raise Fault policy

3) In your proxy endpoint, put a condition

<Request>
	<Step>
		<Name>Service-Callout-1</Name>
	</Step>
	<Step>
                <Name>Service_callout_Error</Name>
                <Condition>(calloutResponse.status.code == 404)</Condition>
	</Step>
 </Request>

Would recommend this as you can have customized error message for different status codes and also for different service callouts (if you have many) within the same proxy.

Try this out and let me know if this worked

@Sai Saran Vaidyanathan: I sai saran ,I have created a new proxy ,but I am getting error 'com.apigee.kernel.exceptions.spi.UncheckedException'.Here are details .

Proxy .xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <APIProxy revision="1" name="ExceptionHandlingatServicecallOutpolicy"> <Basepaths>/exceptionhandlingatservicecalloutpolicy</Basepaths> <ConfigurationVersion majorVersion="4" minorVersion="0"/> <CreatedAt>1480924708504</CreatedAt> <CreatedBy>nvraghavendra254@gmail.com</CreatedBy> <Description/> <DisplayName>ExceptionHandlingatServicecallOutpolicy</DisplayName> <LastModifiedAt>1480925619945</LastModifiedAt> <LastModifiedBy>nvraghavendra254@gmail.com</LastModifiedBy> <Policies> <Policy>Raise-Fault-1</Policy> <Policy>Service-Callout-1</Policy> </Policies> <ProxyEndpoints> <ProxyEndpoint>default</ProxyEndpoint> </ProxyEndpoints> <Resources/> <Spec/> <TargetServers/> <TargetEndpoints> <TargetEndpoint>default</TargetEndpoint> </TargetEndpoints> <validate>false</validate> </APIProxy>

Proxy endpoint:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ProxyEndpoint name="default"> <Description/> <FaultRules> <FaultRule name="Service_callout_Error"> <Condition>(calloutResponse.status.code == 404)or(calloutResponse.message.content Like "*Resource not found*")</Condition> </FaultRule> </FaultRules> <PreFlow name="PreFlow"> <Request> <Step> <Name>Service-Callout-1</Name> </Step> <Step> <Name>Raise-Fault-1</Name> <Condition>(calloutResponse.status.code == 404)</Condition> </Step> </Request> <Response/> </PreFlow> <PostFlow name="PostFlow"> <Request/> <Response/> </PostFlow> <Flows/> <HTTPProxyConnection> <BasePath>/exceptionhandlingatservicecalloutpolicy</BasePath> <Properties/> <VirtualHost>default</VirtualHost> </HTTPProxyConnection> <RouteRule name="default"> <TargetEndpoint>default</TargetEndpoint> </RouteRule> </ProxyEndpoint>

Target endpoint:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <TargetEndpoint name="default"> <Description/> <FaultRules/> <PreFlow name="PreFlow"> <Request/> <Response/> </PreFlow> <PostFlow name="PostFlow"> <Request/> <Response/> </PostFlow> <Flows/> <HTTPTargetConnection> <Properties/> <URL>https://testurl.com</URL> </HTTPTargetConnection> </TargetEndpoint>

Service callout :Service-Callout-1

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-Callout-1"> <DisplayName>Service Callout-1</DisplayName> <Properties/> <Request clearPayload="true" variable="myRequest"> <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables> </Request> <Response>calloutResponse</Response> <HTTPTargetConnection> <Properties/> <URL>http://www.thomas-bayer.com/sqlrest/CUSTOMER/44444444444444</URL> <Properties> <Property name="success.codes">1xx,2xx,3xx,4xx</Property> </Properties> </HTTPTargetConnection> </ServiceCallout>

Raise fault policy:Service_callout_Error

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <RaiseFault async="false" continueOnError="false" enabled="true" name="Raise-Fault-1"> <DisplayName>Service_callout_Error</DisplayName> <Properties/> <FaultResponse> <Set> <Headers/> <Payload contentType="text/plain"/> <StatusCode>500</StatusCode> <ReasonPhrase>Server Error</ReasonPhrase> </Set> </FaultResponse> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> </RaiseFault>

Everything goes fine till raise fault policy ,even condition also met . But it is trhowing error .

Do we need to create a fault rule at the proxy level to handle the excpetion raised in policy or RaiseFaultpolicy at the policy level will be able to handle .please suggest me on this.

Hi @veeraraghavendra - Please remove the Fault Rules from the proxy end point. You don't need that as you have a Raise Fault policy already for that condition.

Let me know if that fixed the issue

NOTE: Please use the CODE formatter when pasting any code in the editor (the option with CODE in the editor) so that its more readable

Hi @Sai Saran Vaidyanathan,I have removed the fault rules ,But still getting error '

com.apigee.kernel.exceptions.spi.UncheckedException'

Please correct me ,if I did any thing wrong.

Hi @veeraraghavendra - Can you attach the bundle here ?

Hi @veeraraghavendra - I imported the bundle you attached and it works as expected. I didnt have to do any change. Are you using Cloud version or the OnPrem (private cloud) version ? If Private Cloud, which version are you on ?

Hi @Sai Saran Vaidyanathan ,I am using 'Private Cloud releases 4.16.01' .

I am using APIGEE private cloud.

@arghya das - something that can be checked ? The proxy bundle is attached above and the issue is coming on 4.16.01