Using Shared Flows For Common Error Handling

A common task when developing multiple proxies is to provide a consistent method of error handling and error messaging throughout. This can be achieved with Shared Flows. Using the error handling technique outlined in this article (https://community.apigee.com/articles/23724/an-error-handling-pattern-for-apigee-proxies.html), we can abstract the AssignMessage, RaiseFault and other policies and conditions into a shared flow, to be used by other proxies. This results in a much easier and cleaner method of error handling.

I created an example of how to do this. It is available here: https://github.com/davidmehi/edge-shared-errorhandling-flow-example

Comments
kurtkanaskie
Staff

Hey @David Mehi nice post and accompanying code. I've done similar behind the scenes of this answer https://community.apigee.com/questions/40984/how-to-use-shared-flows-to-achieve-reuse-and-devel.html...

But it becomes a bit tedious adding all those Assign Messages. I'm considering an alternate approach of using a single Javascript callout to check the fault name and assign variables.

For example, this is WIP:

var faultName = context.getVariable ("fault.name");
var proxyName = context.getVariable ("apiproxy.name");

var responseCode;
var reasonPhrase;
var code;
var userMessage;
var systemMessage;

switch(faultName) {
    case "InvalidAccessToken" :
    case "invalid_access_token" :
        responseCode = "401";
        reasonPhrase = "Unauthorized";
        code = proxyName + ".401.001";
        userMessage = "Invalid Access Token";
        systemMessage = "Request new access token";
        break;
    
    case "access_token_expired" :
        responseCode = "401";
        reasonPhrase = "Unauthorized";
        code = proxyName + ".401.002";
        userMessage = "Access Token Expired";
        systemMessage = "Request new access token";
        break;
    
    // etc.
        
    default:
        responseCode = "500";
        reasonPhrase = "Internal Error";
        code = proxyName + ".500.001";
        userMessage = "Unknown server error";
        systemMessage = "Something went wrong in the proxy";
}

context.setVariable( "flow.errorStatusCode", responseCode );
context.setVariable( "flow.errorReasonPhrase", reasonPhrase );
context.setVariable( "flow.errorCode", code );
context.setVariable( "flow.errorUserMessage", userMessage );
context.setVariable( "flow.errorSystemMessage", systemMessage );

Then shape my standard response with an Assign Message in the ProxyDefaultFaultRule shared flow:

<Set>
   <Headers/>
   <Payload contentType="application/json">
   {
      "code": "{flow.errorCode}",
      "userMessage": "{flow.errorUserMessage}",
      "systemMessage": "{flow.errorSystemMessage}",
      "info": "http://developer.company.com/docs/errors#{flow.errorCode}" 
    }
    </Payload>
    <StatusCode>{flow.errorStatusCode}</StatusCode>
    <ReasonPhrase>{flow.errorReasonPhrase}</ReasonPhrase>
</Set><br>

Which results in a response like:

{
  "code": "proxyname.401.001",
  "userMessage": "Invalid Access Token",
  "systemMessage": "Request new access token",
  "info": "http://developer.company.com/docs/errors#proxyname.401.001"
}<br>
jineshnarenthak
New Member

After cloning https://github.com/davidmehi/edge-shared-errorhandling-flow-example, I am not able to import the code under Sharedflow in Apigee. Getting an error "Error uploading shared flow: Unable to read/find APIProxy contents." Also tried to import under API Proxy but doesn't work.

davidmehi
Staff

Note that this example was updated to include additional error handling techniques and information. The second RaiseFault policy in the shared flow has been removed and AssignMessage is used instead to set the response payload. If you are using this example, review the changed code and description to see how it may impact you. See the updated examples here: https://github.com/davidmehi/edge-shared-errorhandling-flow-example

Version history
Last update:
‎03-14-2017 01:40 PM
Updated by: