Exit JavaScript Policy

I have a JavaScript policy with a potentially large number of conditional checks. Once one condition is true, there's a few lines a code and that's it. When coding something like that, I typically exit/return from the function at the spot.

Example:

function someFunc() {

  if(checkA) {
    console.log('check a');
    return;
  }

  if(checkB) {
    console.log('check b');
    return;
  }
}

However, if I add a `return` statement like the above to my policy, saving that policy results in an error:

Compilation of JavaScript jsc://someScript.js failed with error:Compilation error: invalid return (JS_SomeJSPolicy#38). Line:38, column:9. Source is:return;

Is there anyway to exit a JavaScript policy?

3 9 1,525
9 REPLIES 9

Not applicable

You can set a boolean flag and add a condition (if statement) to skip rest of the block in the script.

Dear @williamking,

I tried your code in my cloud org, but I didn't get any compilation errors. I invoked the function someFunc() as follows:

someFunc();

Of course, I did get the error "console" is not defined while executing the API call. When I used "print" instead of console.log, it worked fine.

Can you please check/show us how you are invoking the function "someFunc()" or assigning the return value of "someFunc()" to some variable ?

Regards,

Amar

I'm not calling `someFunc()` in a policy. That was an example of how I would typically code. In the policy itself would only be the conditional statements.

if(someBool) {
  // do this
  return;
}

@williamking,

I modified my Javascript code to remove the invocation of the function "someFunc()", made it same as yours and saved the Proxy. It didn't give any compilation errors.

Can you please let me know if you are trying this in cloud org or Private cloud ?

If it is cloud org, can you please share the org name, API proxy name ?

If it is Private Cloud, can you please let me know the Private Cloud version and share the API bundle ?

This will help me to reproduce the problem and investigate this issue.

Regards,

Amar

@AMAR DEVEGOWDA

If you can verify that the following should work as a JavaScript policy in the preflow of a Target Endpoint, then I will open a support ticket requesting help:

var isDeveloperApp = context.getVariable('app.appType') === 'Developer';
if(isDeveloperApp) {
  return;
}

Just wanted to bring this up one last time in the forums before opening a ticket.

The code below is the extent of my JS policy and is running in "cloud org" environment.

var resp = context.getVariable('svcCalloutResponse.content');


var respObj = JSON.parse(resp);
var count = respObj.entities.length;


if(count === 0) {
  // return 404
  print('not found');
  return;
}


if(count > 1) {
  // return error & notify everyone about weirdness
  print('ERROR: Too many found ' + count);
  return;
}


If it is not deployed, simply saving it will not result in any errors. However, if it is deployed, or if you try to deploy it after save, you get this:

Error Deploying Revision 4 to devCompilation of JavaScript 

jsc://HandleCalloutResponse.js failed with error:
Compilation error: invalid return (Handle_Callout_Response#10). Line:10, column:9. Source is:return;. 

Context Revision:4;
APIProxy:My-Test-Proxy;
Organization:myorganziation;
Environment:dev .

Hello,

Sorry to dig up this old subject, but I'm facing the same kind of issue.

I need to be able to "stop" a JS policy run before the end, and I'd like to avoid multiple if for simplicity and risk-reducing and performance purposes...

I tried "return;", and "throw new Error()" but it does not work as expected:

- return does not work at all

- throw new Error() will generate an Error whereas I just wanted to stop the JS execution...

 

Thanks

Sorry to dig up this old subject,

This is a six-year old question. please raise a new one. Provide the details in YOUR CASE. If you want you can hyperlink to this old question, but you need to provide your own details.

Ok, I will. Thanks.