Can someone point me to an example of having EDGE return a 400 error (with message) from within a pre-flow JavaScript policy?
Answer by Alex Koo
·
Oct 07, 2015 at 11:16 PM
The following is the most typical pattern for JS error checking:
try{ < ..JS CODE..> } catch (err) { throw 'Error in JavaScript'; }
and then you can further refine the error message code and other details in Fault Rules. You could also refine the error message further in the JS itself, like below:
try{ < ..JS CODE..> } catch (err) { setResponse(outputFormat, "500", "Internal Server Error", "Unknown Error Occurred", err.message); } function setResponse(errorStatus, errorReason, errorMessage, errorDetail) { var errorContent = ""; var json = {}; json.Error = {}; json.Error.Message = errorMessage; json.Error.Detail = errorDetail; errorContent = JSON.stringify(json); context.setVariable("error.status.code", errorStatus); context.setVariable("error.reason.phrase", errorReason); context.setVariable("error.header.Content-Type", "application/json"); context.setVariable("error.content", errorContent); }
@Alex Koo, can you describe how you would use these error messages in the Fault Flow? I tried putting in a "<Condition>(error.content = "some_error_message")</Condition>" but that doesn't seem to work.
Try checking for values in any of those variables, e.g., error.status.code != "". Then in the policy with the condition, you can format the error message to your liking.
Answer by williamking · Oct 28, 2015 at 08:12 PM
Thanks for the suggestions @Claudius Mbemba and @Alex Koo.
I ended up setting a variable in the JS policy and then conditionally running a "Raise Fault" policy based on that variable. Eg:
JS-Policy:
context.setVariable("triggerError", "true");
Pre-Flow:
<Step> <FaultRules/> <Name>JS-Policy</Name> </Step> <Step> <Condition>triggerError equals "true"</Condition> <FaultRules/> <Name>RaiseFault-Policy</Name> </Step>
Hi @williamking, While your solution is succinct, it will not catch errors at the meta-JS level. When the JS code fails, itself, then the Edge engine will send flow control to the Fault section of your proxy. Flow control will not continue to the next Step in the flow, and so your condition will not be checked.
Answer by Cladius Fernando
·
Oct 07, 2015 at 05:09 PM
Hi @williamking, you can try something along these lines:
//Create the payload var response = {}; response.message = "Bad request. Mandatory params missing."; response.code = 49; //Any custom values that you need in the error message //Set the payload response = JSON.stringify(response); context.setVariable("response.content", response); //Set the http response code context.setVariable("response.status.code", 400); //Set the http response phrase context.setVariable("response.reason.phrase", "Bad Request");
Let me know if this took care of your problem.
Thanks @Cladius Fernando. I let you know when I am able to test that. Does setting the status code trigger an immediate return after that policy?
@williamking, I did not notice the "pre-flow" (assuming request) part of your question. The above snippet will only work in the response flow. If you want to return control from a JS policy in the request flow, you might want to explore setting a custom-error apigee flow variable from your JS. Additionally, to return the flow from the JS script you would need to throw an error and ensure that the JS policy has "continueOnError" set to false. You can then have a FaultRule to handle the payload with the condition based on this flow variable. Makes sense?