A 401 from a ServiceCallout Policy results in an error being sent to client. Can I override it?

Hi

I have a Service Call Out Policy and it executes successfully but it actually returned below json

{"code":401,"reason":"Unauthorized","message":"Authentication Failed"}

But the error which I am seeing on Gateway is below

{
    "fault": {
        "faultstring": "Execution of ServiceCallout AuthenticateUser failed. Reason: ResponseCode 401 is treated as error",
        "detail": {
            "errorcode": "steps.servicecallout.ExecutionFailed"
        }
}
}

I have to use RaiseFault policy if I get code 401 from ServiceCallOut and provide appropriate message but it is returning different error.

Please help

Thanks

1 6 2,176
6 REPLIES 6

Not applicable

If any policy fails, then the message in Apigee Edge transfers to "Fault state". A 401 returned from a ServiceCallout is normally treated as a Fault, so your message is moving to fault state. As an API Proxy designer, you have control over how to handle such faults, with FaultRules. The process is described in good detail, here.

In short, you can insert a handler that will over-write that message, with your own status code and payload.

If you share the code, I may be able to help further.

Also, don't forget that it is possible to configure the ServiceCallout so that a 401 is not treated as an error, in which case the flow will not enter fault state.

You can use the success.codes property on the HttpTargetConnection to do this.

In that case you'd follow the ServiceCallout policy with a RaiseFault wrapped in a Condition, like this:

<Flow name='myflow'>
  <Request>
    <Step><Name>ServiceCallout-1</Name></Step>
    <Step>
      <Condition>ServiceCallout-1.response.status != 200</Condition>
      <Name>RaiseFault-MyCustomFault</Name>
    </Step>
     ...

I just want to mention that we have an example of this error handling pattern in the Learn Edge series on GitHub. Learn Edge proxies are easy to deploy and run, just follow the README file.

Thanks, Will! that's helpful.

Hi @MEGHDEEP BASU

I already have a RF Policy which gives proper message when Service Callout returns status code other than 200 as @Dino suggested below.

Somehow it was behaving correclty before but suddenly it has started giving error even if Service Callout executes fine and it return 401.

Attached is the bundle for your reference

Thanks for your help in advance

legacy-api-v1-document-rev1-2016-08-28.zip

The bundle itself seems to be fine. I can see that the raisefault policy is getting triggered when a 401 is returned from the service callout. I don't see a reason why it would not work. Did you always have the 'continueOnError' set to true in the service callout. That would make sure the flow proceeds even if the service callout fails and then the raise fault policy gets triggered.

A better approach would be to have fault rules defined for your proxy instead of calling the raise fault like a conditional policy. That way it would always trigger depending on the fault rule condition you have set.