Passing error from service call out

Not applicable

I am using a ServiceCallout policy.
When getting an error response from the called-out-service I want to return it to the client with a minor modification of the response fields.


What do I need to do to achieve that?

I've tried using FaultRule with AssignMessage but it looks like after the error response comes from the service I cant get its content in the Error Flow

0 2 1,123
2 REPLIES 2

@yuri abaev , Can you please post your fault rule condition & outcome of service callout ? What does trace say ?

adas
New Member

Should be fairly simple. Imagine this is your service callout policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="GetUserDetailsFromToken">
    <DisplayName>GetUserDetailsFromToken</DisplayName>
    <Properties/>
    <Request clearPayload="true" variable="myRequest">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
        <Set>
            <Headers>
                <Header name="Authorization">Bearer {clientRequest.jwtToken}</Header>
            </Headers>
        </Set>
    </Request>
    <Response>jwtUserInfo</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>https://tokenEndpoint.com/userinfo</URL>
    </HTTPTargetConnection>
</ServiceCallout>

If this is successful the jwtUserInfo response obect would get populated. If there's an error, you would get a 500 Error saying Error in Service Callout. Now, say you want to handle this error and give a meaningful error back to the client. You can make use of fault rules in this case:

    <FaultRules>
         <FaultRule name="InvalidJwtToken">
            <Step>
                <Name>RaiseFaultInvalidCredentials</Name>
            </Step>
            <Condition>jwtUserInfo.status.code != 200 AND clientRequest.jwtToken != null</Condition>
        </FaultRule>
        <FaultRule name="MissingAuthorization">
            <Step>
                <Name>RaiseFaultMissingAuthorization</Name>
            </Step>
            <Condition>request.header.authorization == null</Condition>
        </FaultRule>
    </FaultRules>

So here, we are checking for the status code of the jstUserInfo response object and raise a fault if its not a 200. The corresponding raise fault policy looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RaiseFault async="false" continueOnError="false" enabled="true" name="RaiseFaultInvalidCredentials">
    <DisplayName>RaiseFaultInvalidCredentials</DisplayName>
    <Properties/>
    <FaultResponse>
        <Set>
            <Headers/>
            <Payload contentType="text/plain"/>
            <StatusCode>jwtUserInfo.status.code</StatusCode>
            <ReasonPhrase>jwtUserInfo.reason.phrase</ReasonPhrase>
</Set> </FaultResponse> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> </RaiseFault>

You can also get the error and error description and set that in the payload if you want to. I hope this was helpful.