How to interrupt JS policy execution?

Hello

I have a JS policy to prepare a backend call with advanced mapping.

To manage errors, I implemented the solution suggested here by @williamking .

However, to provide to the caller a better experience with meaningful response message in case of error (missing mandatory fields, etc..), I define more than one try catch section, like :

 

try {
    source = JSON.parse(context.proxyRequest.content);
} catch (e) {
    setErrorOn("400","400.1","Received content is not JSON or not valid","BadRequest");
}

try {
    anotherCheckOrTransformation(source);
catch(e) {
    setErrorOn("400","400.2","Error description about transformation","BadRequest")
}

 

The problem is: if the first "step" failed, it is catched, managed ... but the code continue to run.

I added a flag and conditions, but I do not like this option...

 

var noError = true;


if (noError) {
  try {
      source = JSON.parse(context.proxyRequest.content);
  } catch (e) {
      setErrorOn("400","400.1","Received content is not JSON or not valid","BadRequest");
      noError = false
  }
}

if (noError) {
  try {
      anotherCheckOrTransformation(source);
  catch(e) {
      setErrorOn("400","400.2","Error description about transformation","BadRequest")
      noError = false;
  }
}

 

 

Do you know how I could interrupt my code execution (right after line 4 of my first example)?

Thanks 

 

Solved Solved
0 2 311
1 ACCEPTED SOLUTION

You could try throwing an Error.

 

try {
  source = JSON.parse(context.proxyRequest.content);
} catch (e) {
  throw new Error("whatever you like here");
}

 

That "throw new Error" will cause the policy to be interrupted, and the proxy flow will enter into Fault state. 

And then in the Proxy endpoint, you will want Fault Rules that handle that fault. The Fault rule should look like this: 


<FaultRule name="script-error">
  <Step>
    <Name>AM-Customize-Error-Message</Name>
  </Step>
  <Condition>fault.name = "ScriptExecutionFailed"</Condition>
</FaultRule>
 

View solution in original post

2 REPLIES 2

You could try throwing an Error.

 

try {
  source = JSON.parse(context.proxyRequest.content);
} catch (e) {
  throw new Error("whatever you like here");
}

 

That "throw new Error" will cause the policy to be interrupted, and the proxy flow will enter into Fault state. 

And then in the Proxy endpoint, you will want Fault Rules that handle that fault. The Fault rule should look like this: 


<FaultRule name="script-error">
  <Step>
    <Name>AM-Customize-Error-Message</Name>
  </Step>
  <Condition>fault.name = "ScriptExecutionFailed"</Condition>
</FaultRule>
 

Thanks, Dino. It works exactly as I expected.

I added some output variables in the try catch:

try {
    source = JSON.parse(context.proxyRequest.content);
} catch (e) {
    context.setVariable("errorCode", 400);
    context.setVariable("errorMessage","Invalid JSON request");
    throw new Error("BadRequest");
}

And then, used {errorCode} and {errorMessage} in the assignMessage policy.

Thanks