how do you handle faults from javascript policy?

I am using javascript to parse query parameters that come as a comma separated list. I need to throw a fault if there are more than 5 elements in the comma separated list. How can this be done using the javascript policy?

I have tried the following code -

if (arr.length > 5)throw new Error("Maximum of five taskids can be given as comma separated value in the query parameter");

How do I handle the error thrown and raise a proper fault message, because the standard fault message would be

{"fault":"{"detail":{"errorcode":"steps.javascript.ScriptExecutionFailed"},"faultstring":"Execution of extractInputParams failed with error: Exception thrown from JavaScript : Error: Maximum of five taskids can be given as comma separated value in the query parameter (extractInputParams_js#42)"}"}

I do not want the policy name, script name etc to be shown, I only want the error message to be shown. Is there anyway to configure the fault rules for this and extract only the error message alone?

Solved Solved
0 5 3,655
1 ACCEPTED SOLUTION

Dear @Nagashree B ,

What you see is a "Javascript Script Execution Failed Error". As far as i know, You cannot raise faults directly from Javascript Policy. You need to use RaiseFault policy to raise custom faults. You can set a variable in javascript policy and check for same in step condition of raise fault policy to raise fault.

You can handle the error raised by raise fault policy using fault rules. Set Assign Message policy in fault rule to change the response. I have experimented with same & please find example below.

Below one Displays Message with 400 status code

http://apigee-perf-test.apigee.net/jsfault?param=o...

Below one doesn't

http://apigee-perf-test.apigee.net/jsfault?param=o...

Please find attached proxy for same.

Cheers,

Anil Sagar

View solution in original post

5 REPLIES 5

Dear @Nagashree B ,

What you see is a "Javascript Script Execution Failed Error". As far as i know, You cannot raise faults directly from Javascript Policy. You need to use RaiseFault policy to raise custom faults. You can set a variable in javascript policy and check for same in step condition of raise fault policy to raise fault.

You can handle the error raised by raise fault policy using fault rules. Set Assign Message policy in fault rule to change the response. I have experimented with same & please find example below.

Below one Displays Message with 400 status code

http://apigee-perf-test.apigee.net/jsfault?param=o...

Below one doesn't

http://apigee-perf-test.apigee.net/jsfault?param=o...

Please find attached proxy for same.

Cheers,

Anil Sagar

@Anil Sagar Thanks, this helps. Is there a way to break the execution of the javascript post setting the error. I tried the return statement, but that gives a compilation error. So, I am now using a workaround, if (inputParamError == false) for further code execution in the javascript.

Yes, You can use if else statement...

If paramArr > 5

set variable

else

execute remaining code

In the past I've used an assign message policy further down the flow that has a simple conditional check:

if (proxy.response.statusCode != 200) 

Then you can mediate the response from JavaScript to omit the stack and policy name.

ysharat
Participant II

Hi Anil,

You raising a fault when query parma > 5 in step. why its again going to fault rules and running that assign message policy?Can't we stop executing that assigned msg policy running and use only the response that is in RaiseFault-1 in step which also works, right? Let's say you if had to define tons of faults in fault rules it will go and check each condition which is time wast when we already raised a fault in step when query parm is greater than 5. Make sense? Pls advice