Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Handle JS Policy Timeout Error

I have set timelimit 1 sec in JS policy

timeLimit="1000" 

continueOnError="true" //also checked with false

When a JavaScript file takes too long to execute, I encounter a timeout error in Postman, and the subsequent policies do not execute. I've included a try-catch block in the API call within the JavaScript file, but it's not triggering as expected. I want to ensure that all policies run even if the JavaScript policy times out. I also tried using RaiseFault to handle this situation, but it isn’t working. Additionally, I need to set those errors in a variable so I can use them in subsequent policies based on certain conditions. I am working on Apigee Edge. FaultRules is also not working.

JS policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="true" continueOnError="true" enabled="true" timeLimit="1000" name="JS-AuthHandler">
<DisplayName>JS-AuthHandler</DisplayName>
<Properties/>
<ResourceURL>jsc://AuthHandler.js</ResourceURL>
</Javascript>


Error:

{
    "fault": {
        "faultstring""Execution of JS-AuthHandler failed with error: Javascript runtime exceeded limit of 1000ms",
        "detail": {
            "errorcode""steps.javascript.ScriptExecutionFailed"
        }
    }
}


For capturing the 'api.timeout.error' , we tried FaultRule even it is not going inside it because next policy/line not executing due timeout error. 
 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="true" continueOnError="true" enabled="true" timeLimit="1000" name="JS-AuthHandler">
<DisplayName>JS-AuthHandler</DisplayName>
<FaultRules>
<FaultRule name="JavaScript Policy Faults">
<Step>
<Name>RF-HandleJsError</Name>
<Condition>(fault.name Matches "ScriptExecutionFailed") </Condition>
</Step>
<!--<Condition>(javascript.JavaScript-1.failed = true) </Condition>-->
</FaultRule>
</FaultRules>
<Properties/>
<ResourceURL>jsc://AuthHandler.js</ResourceURL>
</Javascript>
 
Even tried to keep FaultRule code in PreFLow also, but not working.
In short whenever any error is coming in JS policy then it is terminating the pipeline on JS policy only, not executing the remaining steps/policies. But here, we want to handle this timeout error, so we can retry auth call again.
0 2 206
2 REPLIES 2

That's  not the right way to do it. You cannot define a FaultRule inside the policy. 

I don't know why, sometimes the UI provides a template that includes FaultRules inside the policy.  But in any case, it does not belong there.

You can insert FaultRules in the ProxyEndpoint or in the TargetEndpoint. Check the documentation on that. Your JS policy should be as simple as this:  

 

<Javascript timeLimit="1000" name="JS-AuthHandler">
  <ResourceURL>jsc://AuthHandler.js</ResourceURL>
</Javascript>

 

 

Thanks for the response.

 

We have to add retry logic for OAuth call written in the Js file then after target end point will call.

I have added FaultRule in ProxyEndpoint but it is not working. As i am getting timeout error from OAuth call not from target end point. How can i do this?