Need help to configure fault rule within policy

Hi,

I am configuring fault within the policy as show below code.if any problems comes during service call it should execute configured fault(i.e service-callout-fault).the below code throwing the fault like unreachable host but it is not executing configured fault.

Please suggest me what is wrong in below configuration.

<ServiceCallout async="false" continueOnError="false" enabled="true" name="service-callout">
    <DisplayName>Service Callout</DisplayName>
    <FaultRules>
      <FaultRule>          
          <Step>
                <Name>service-callout-fault</Name>              
            </Step>
      </FaultRule>
  </FaultRules>
    <Properties/>
    <Request clearPayload="true" variable="Request">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    </Request>
    <Response>Response</Response>
  <Timeout>3000</Timeout>
    <HTTPTargetConnection>              
      <URL>http://0.0122.45.567/maps/api/geocode/json</URL>
    </HTTPTargetConnection>
</ServiceCallout>
Solved Solved
0 12 2,461
1 ACCEPTED SOLUTION

@Veerendra Deshpande You can attach FaultRules to the following entities in an API proxy configuration:

- ProxyEndpoint: Enables fault handling for all errors that occur in the ProxyEndpoint request and response flows.

- TargetEndpoint: Enables fault handling for all errors that occur in the TargetEndpoint request and response flows.

In the following example, I have added the fault rule to proxy end point. So my proxy-endpoint configuration looks like this -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
    <FaultRules>
        <FaultRule name="Service_callout_Error">
            <Step>
                <Name>Service_callout_Error</Name>
            </Step>
        </FaultRule>
    </FaultRules>
    <Description/>
    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>Service-Callout-1</Name>
            </Step>
        </Request>
        <Response/>
    </PreFlow>
    <Flows/>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <HTTPProxyConnection>
        <BasePath>/faultruletest</BasePath>
        <VirtualHost>default</VirtualHost>
        <VirtualHost>secure</VirtualHost>
    </HTTPProxyConnection>
    <RouteRule name="default">
        <TargetEndpoint>default</TargetEndpoint>
    </RouteRule>
</ProxyEndpoint>

Here the step "Service_callout_Error" is executed when there is an error in "Service-Callout-1". Ideally you define a <Condition> under <FaultRule> to customise the message based on the error.

You need to update your service call out policy configuration as well -

<?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>
	<FaultRules/>  
    <Properties/>
    <Request clearPayload="true" variable="myRequest">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    </Request>
    <Response>calloutResponse</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>http://12.3.4.5.com</URL>
    </HTTPTargetConnection>
</ServiceCallout>

Also add a new step (any policy) "Service_callout_Error" to handle the error. I am using a RaiseFault here -

<?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>
    <FaultRules/>
    <Properties/>
    <FaultResponse>
        <Set>
            <Headers/>
            <Payload contentType="text/plain">My Error </Payload>
            <StatusCode>530</StatusCode>
            <ReasonPhrase>Server Error</ReasonPhrase>
        </Set>
    </FaultResponse>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>

Similarly you can add fault handling in Target end point as well.

Hope this helps!

View solution in original post

12 REPLIES 12

Not applicable

Fault rules are configured in the flows; not inside policies. You should configure fault rule in the flow that uses this policy rather than the policy itself. Please take a look at the documentation below.

http://apigee.com/docs/api-services/content/fault-handling

Sample:

<FaultRules>
       <FaultRule name="FaultHandler">
            <Name>handle_fault_svc_callout</Name>
        </FaultRule>
     </FaultRules>
     <Flows>
       <Flow name="contact_external_network">
            <Request>
                <Name>service_callout</Name>
            </Request>
        </Flow>
    </Flows>

Thanks your reply.If we are not able configure fault in policy level, why we have "FaultRules" tag in every policy and which purpose we have to use this tag?

I do not think service callout has a faultrules tag

http://apigee.com/docs/api-services/reference/service-callout-policy#elements

not sure if this what you are referring to

When we create new service-callout policy,automatically xml will be generate for this policy.in that i saw "FaultReules" tag.

Let me take this as feedback.

Let me know what action taken for this feedback.

@Veerendra Deshpande You can attach FaultRules to the following entities in an API proxy configuration:

- ProxyEndpoint: Enables fault handling for all errors that occur in the ProxyEndpoint request and response flows.

- TargetEndpoint: Enables fault handling for all errors that occur in the TargetEndpoint request and response flows.

In the following example, I have added the fault rule to proxy end point. So my proxy-endpoint configuration looks like this -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
    <FaultRules>
        <FaultRule name="Service_callout_Error">
            <Step>
                <Name>Service_callout_Error</Name>
            </Step>
        </FaultRule>
    </FaultRules>
    <Description/>
    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>Service-Callout-1</Name>
            </Step>
        </Request>
        <Response/>
    </PreFlow>
    <Flows/>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <HTTPProxyConnection>
        <BasePath>/faultruletest</BasePath>
        <VirtualHost>default</VirtualHost>
        <VirtualHost>secure</VirtualHost>
    </HTTPProxyConnection>
    <RouteRule name="default">
        <TargetEndpoint>default</TargetEndpoint>
    </RouteRule>
</ProxyEndpoint>

Here the step "Service_callout_Error" is executed when there is an error in "Service-Callout-1". Ideally you define a <Condition> under <FaultRule> to customise the message based on the error.

You need to update your service call out policy configuration as well -

<?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>
	<FaultRules/>  
    <Properties/>
    <Request clearPayload="true" variable="myRequest">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    </Request>
    <Response>calloutResponse</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>http://12.3.4.5.com</URL>
    </HTTPTargetConnection>
</ServiceCallout>

Also add a new step (any policy) "Service_callout_Error" to handle the error. I am using a RaiseFault here -

<?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>
    <FaultRules/>
    <Properties/>
    <FaultResponse>
        <Set>
            <Headers/>
            <Payload contentType="text/plain">My Error </Payload>
            <StatusCode>530</StatusCode>
            <ReasonPhrase>Server Error</ReasonPhrase>
        </Set>
    </FaultResponse>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>

Similarly you can add fault handling in Target end point as well.

Hope this helps!

Sudheendra,if i configure like above, this fault will become generic fault.if i get any exception or fault in flow configured fault execute.My requirement is some fault has to execute only for particular policy got failed not for all policy.

You can specify a condition in your "FaultRule" so that it gets executed only on those scenarios.

I can put condition like "<Condition>fault.name = "ExecutionFailed"</Condition>" but this is applicable for "ServiceCallout" as well as "JavaCallout".Now my requirement is need to raise different fault for "SerivceCallout" and "JavaCallout" when it fails.

Right. Your conditional statement could include the fault name to identify the policy which failed. You could use this fault name in RaiseFault to customise your error response. Alternatively have multiple RaiseFault policies, each for handling a policy failure.

@Veerendra Deshpande, You can have a flag set in java to raise a different fault and for servicecallout you can check for status code using 'response.status.code'