How to handle requests to not responding target?

Not applicable

If I have proxy that has target endpoint like literally https://non-existing-site.com, Apigee always responds with 404 and don't apply Javascript policy on target response flow. How to handle the situations if my target doesn't respond for a long time (like never) or doesn't respond via HTTP at all?

1 2 833
2 REPLIES 2

What happens in the event of a timeout or un-reachable target:

The target flow enters fault state. This means the steps you have configured in the Target response flow will not run.

You have several ways of handling this:

  • Set the target properties to treat a 500 or 400 as a non-error. Do this like so:

    <TargetEndpoint name="default">
      <HTTPTargetConnection>
        <URL>http://non-existing-site.com</URL>
        <Properties>
          <Property name="success.codes">2xx,3xx,4xx,5xx</Property>
        </Properties>
      </HTTPTargetConnection>
    </TargetEndpoint>
    	

    For more information on this, see the reference.

  • Set up a set of FaultRules to handle the error. For example:

    <TargetEndpoint name='default'>
      <Description>default target endpoint</Description>
      <FaultRules>
        <FaultRule name='handle-all-faults'>
          <!-- This FaultRule always catches all uncaught faults. -->
          <Step>
            <Name>JS-HandleFault</Name>
          </Step>
        </FaultRule>
      </FaultRules>
    ....
    	

    You can read more about fault handling here. In the case of handling faults in a JS policy, you might want to do something like this:

    var handled = context.getVariable('fault_handled');
    if ( ! handled ) {
      if (response.status.code == 404){
        response.content = JSON.stringify({
          error: 404,
          message: 'not found'
        });
      }
      else {
        response.content = JSON.stringify({
          error: response.status.code,
          message: 'unknown error'
        });
      }
      context.setVariable('fault_handled', true);
    }
    

5869-screen-shot-2017-11-03-at-191303.png

Thanks, Dino,

I used the second option. But when I try to make request to non-existing server (e.g. https://example999.com), I got 404 on the step selected on screenshot. In this case I cannot handle it on the following JS step and return 500 error to user to indicate something went wrong (404 isn't pretty obvious here, because there's no server at all, not the resource on server).

Does this makes sense or 404 in this case is ok?