How to intimate error occurred without using the raise fault policy?

Not applicable

Hello,

I want to indicate APIgee to enter into error flow whenever some conditions are matching.

But I don't want to use any raise fault policy to do that.

Let us say, I am using one javascript policy to evaluate some parameter.

Then can I set error.state=PROXY_REQ_FLOW or some other value or myJavascriptPolicyName.faile=true inside the javascript to enter into error state.

Which will indicate some error occurred and I can enter into fault handling mode.

I can set some variable to true like isJavaScriptFailed =true and then check that variable in the preflow/postflow condition to execute raise fault poilcy. But is there any direct way, so I can skipt the raise fault and handle the error directly in the fault rules.

Please let me know, what are the parameters that get set to indicate error occured for out of box policies.

Thank you,

Ziaur

0 7 692
7 REPLIES 7

sidd-harth
Participant V

Hi @Ziaur Raheman Khan, each policy in Apigee has continueOnError="false" tag by default. When it is Set to false it returns an error when the policy fails. This is expected behavior for most policies.

So when you use false and if there is an error in policy, it will enter error.flow directly & you can use Fault rules.

- or if you are asking how to return error from JS policy, have a look at below article -

https://community.apigee.com/questions/11381/correct-way-to-return-error-from-javascript-policy.html

@Siddharth Barahalikar thanks for the answer. Yes, continueOnError = false variable is there. My java script will raise error if let us say one request param is absent or null.

SO for script this is not a error scenario. SO I have to set some variable saying error occurred and use that var in step condition.

But I do not want to take this in 2 steps. I want direct entry to fault rules(error flow) once the variable setup is done.

In the link, "throw error" might come handy.

Please let me know if you have any other idea.

Yes. Did you do that? did you throw the error from within JavaScript? And then use the Condition in the fault rule?

and? What happened? did it work for you?

@Dino-at-Google Yes, I tried that, I used the throw lines with setting up the fault.name

context.setVariable("fault.name","MyCustomErrorName");

throw 'Error occured ';

But the issue is once the throw error executes, fault.name is set to ScriptExecutionFailed.

So if in my flow there are 2 js, doing two diff functionalities then how do I set specific errors as both the fault.name would be ScriptExecutionFailed.

And I can do this using setVariable in JS and then condition matching in faultRule.

And I do not want specific condition check in fault rule as we are having a lot of custom conditions.I am using fault.name as my key customizing the errors in one fault Rule.

Is there any way to overwrite the fault.name to my custom phrases?

Thank you in adavence.

sidd-harth
Participant V

I don't think we can override the default fault.name.

In Fault rules, we can make use of Outer Conditions & Inner Conditions to catch faults of same policy type.

User an "outer" condition to catch the "fault.name" and then use an "inner" condition to differentiate by checking for the "policy-name.failed" = true.

{policy-type}.{policy-name}.failed=true
javascript.{policy-name}.failed=true
<FaultRule name="java-script-error">
<!-- This condition catches a JS ScriptExecutionFailed in *any* JS policy -->
    <Condition>(fault.name = "ScriptExecutionFailed")</Condition>
    <Step>
        <Condition>(javascript.JS-Random.failed = true)</Condition>
        <Name>AM-Set-Custom-Error</Name>
    </Step>
    <Step>
        <Condition>(javascript.JS-Logger.failed = true)</Condition>
        <Name>AM-Set-Error</Name>
    </Step>
</FaultRule>

After Outer Condition ScriptExecutionFailed is true, it will check the Inner conditions & will execute only one of them based on the name of the JS policy.

Check this docs,

https://docs.apigee.com/api-platform/fundamentals/fault-handling#bestpracticesforfaulthandling

Hi @Siddharth Barahalikar Yes I can do that.. But if the list is long!! then we got few condition checking which is bit time consuming .

Is there any variable in apigee which store the policyName generating error? so if I print the variable like

context.getVariable('apigee.policy.FailedPolicy.Name')

then may be I can reduce it.

Letme know your suggestion.

Our aim is to remove condition matcing as much as possible.

Thanks in advance


Not applicable

I had to reduce the if else or step condition matching for my Org proxies.

So I was thinking of customizing the "fault.name" and proceed. But we cannot override the fault.name any where in the edge.

So I had used one JS in the fault handling policies. Where I am using one more container which will carry the fault.name if oob policy fails and in case of customizing the fault.name it will carry my custom value.

I could not eliminate the condition matching but I reduced them to one & instead of assigning the fault.name in my JS I am assigning one more variable and caching them inside.

So if I have 4 js and each of them would be having the "CustomFault.name" variable with diff values.

In faultRule, I will use the below JS in step1 and proceed for customizing the error response.

var customFaultName = context.getVariable("CustomFault.name");

var policyFaultName = context.getVariable("fault.name");

if (customFaultName)

{

context.setVariable("flow.FaultName",customFaultName);

} else

{

context.setVariable("flow.FaultName",policyFaultName);

}

Please let me know your thought.

Thank you,

Ziaur