Comparing a Variable against a Custom Attribute Value (list)

Not applicable

What I want to do is compare my variable (myVariable) against my custom attribute (comma-delimited list of strings) in the proxy pre-flow endpoint.

I created a JS policy to loop over my custom attribute values and compare it against myVariable. There's also the use of wildcards and other bits of logic which is why I went with the JS policy here.

If there is no match, I would like to throw a custom error and prevent the request from continuing. What is the best way to throw this error?

Solved Solved
2 5 2,115
2 ACCEPTED SOLUTIONS

akoo
Participant V

In JS, you can leverage "throw customTextHere" and that will exit the JS while taking you to Fault Handling flow of Proxy or Target. From the Fault Handling flow, you can include additional policies/logic to check for values you set in your JS (e.g., error code, error message, status code, etc.). and Raise Fault accordingly.

View solution in original post

Continuing with the idea suggested by @Alex Koo, The JS code might look like this:

var attrlist = ["blue", "red", "green"];
var value = context.getVariable('mySpecialVariable');
var found = attrlist.indexOf(value);
context.setVariable('found.value', found);

And then, in your proxy flow, you might have a stanza with a condition that looks like this:

      <Request>
        <Step>
          <Name>Javascript-checkAttributeList</Name>
        </Step>
        <Step>
          <Name>RaiseFault-AttributeNotMatched</Name>
          <Condition><![CDATA[found.value < 0]]></Condition>
        </Step>
        <Step>
          ...
        </Step>

This way you'd simply set a variable in JavaScript and then evaluate it in the flow Condition.

View solution in original post

5 REPLIES 5

akoo
Participant V

In JS, you can leverage "throw customTextHere" and that will exit the JS while taking you to Fault Handling flow of Proxy or Target. From the Fault Handling flow, you can include additional policies/logic to check for values you set in your JS (e.g., error code, error message, status code, etc.). and Raise Fault accordingly.

Continuing with the idea suggested by @Alex Koo, The JS code might look like this:

var attrlist = ["blue", "red", "green"];
var value = context.getVariable('mySpecialVariable');
var found = attrlist.indexOf(value);
context.setVariable('found.value', found);

And then, in your proxy flow, you might have a stanza with a condition that looks like this:

      <Request>
        <Step>
          <Name>Javascript-checkAttributeList</Name>
        </Step>
        <Step>
          <Name>RaiseFault-AttributeNotMatched</Name>
          <Condition><![CDATA[found.value < 0]]></Condition>
        </Step>
        <Step>
          ...
        </Step>

This way you'd simply set a variable in JavaScript and then evaluate it in the flow Condition.

Apigee doesn't accept '<' character inside the Condition xml. It throws Invalid XML error.

Very true!

You need to use CDATA or the GreaterThan operator.

Thanks @Dino-at-Google for updating the answer. That worked.