Exit JavaScript Policy in non-erroneous flow

Following this old question- https://www.googlecloudcommunity.com/gc/Apigee/Exit-JavaScript-Policy/td-p/28783 I had the same issue:

I'd like to skip the rest of the JS code and Continue to the Target: specifically I'm performing some configuration validation and if it fails I'd like to skip the rest of the JS code and not invoke the httpClient request:

if (!validateConfig(config)) {
// do not process if config is not valid
print("Skip processing as config is not valid");
return;
}
httpClient.send(req, onComplete);

The 'return' statement doesn't work, as mentioned. 

I saw this post- https://cloud5systems.com/blog/how-to-quickly-return-a-200-ok-from-apigee-edge/ and this question- https://www.googlecloudcommunity.com/gc/Apigee/How-to-interrupt-JS-policy-execution/td-p/425644 but in both of those solutions it uses "Raise Fault" policy and I'd prefer not to throw an error, and also would like the request to continue to the Target end-point.

Thanks

Solved Solved
0 2 214
1 ACCEPTED SOLUTION

Hi, and thanks for your question.

You can do what you want by restructuring your JS code . One option is to invert the boolean test, and/or use an else clause, like this:

 

if (validateConfig(config)) {
  // process only if the config is valid
  httpClient.send(req, onComplete);
}
// the else clause is optional
else {
  print("Skip processing as config is not valid");
}

 

Another option is to use a function to encapsulate your original logic. This might look like this:

 

function doTheWork() {
  var config = ...; 
  if (!validateConfig(config)) {
    // do not process if config is not valid
    print("Skip processing as config is not valid");
    return;
  }
  httpClient.send(req, onComplete);
}

doTheWork();

 

View solution in original post

2 REPLIES 2

Hi, and thanks for your question.

You can do what you want by restructuring your JS code . One option is to invert the boolean test, and/or use an else clause, like this:

 

if (validateConfig(config)) {
  // process only if the config is valid
  httpClient.send(req, onComplete);
}
// the else clause is optional
else {
  print("Skip processing as config is not valid");
}

 

Another option is to use a function to encapsulate your original logic. This might look like this:

 

function doTheWork() {
  var config = ...; 
  if (!validateConfig(config)) {
    // do not process if config is not valid
    print("Skip processing as config is not valid");
    return;
  }
  httpClient.send(req, onComplete);
}

doTheWork();

 

Thanks! I used the second option-  a function to encapsulate the original logic- and it works well