{ Community }
  • Academy
  • Docs
  • Developers
  • Resources
    • Community Articles
    • Apigee on GitHub
    • Code Samples
    • Videos & eBooks
    • Accelerator Methodology
  • Support
  • Ask a Question
  • Spaces
    • Product Announcements
    • General
    • Edge/API Management
    • Developer Portal (Drupal-based)
    • Developer Portal (Integrated)
    • API Design
    • APIM on Istio
    • Extensions
    • Business of APIs
    • Academy/Certification
    • Adapter for Envoy
    • Analytics
    • Events
    • Hybrid
    • Integration (AWS, PCF, Etc.)
    • Microgateway
    • Monetization
    • Private Cloud Deployment
    • 日本語コミュニティ
    • Insights
    • IoT Apigee Link
    • BaaS/Usergrid
    • BaaS Transition/Migration
    • Apigee-127
    • New Customers
    • Topics
    • Questions
    • Articles
    • Ideas
    • Leaderboard
    • Badges
  • Log in
  • Sign up

Get answers, ideas, and support from the Apigee Community

  • Home /
  • Edge/API Management /
avatar image
3
Question by williamking · Oct 07, 2015 at 03:41 PM · 9.6k Views javascripterrors

Correct Way to Return Error from JavaScript Policy

Can someone point me to an example of having EDGE return a 400 error (with message) from within a pre-flow JavaScript policy?

Comment
Add comment
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Close

3 Answers

  • Sort: 
avatar image
7
Best Answer

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);


}
Comment
Add comment Show 2 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kevinwu · Feb 22, 2018 at 07:42 PM 0
Link

@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.

avatar image alexkoo ♦ kevinwu · Feb 22, 2018 at 07:52 PM 0
Link

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.

avatar image
3
Best Answer

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>
Comment
Add comment Show 2 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dino ♦♦   · Oct 29, 2015 at 12:40 AM 0
Link

I like this solution the best. Simple and clear.

avatar image Alex Koo ♦♦   · Apr 14, 2016 at 10:07 PM 0
Link

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.

avatar image
0

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.

Comment
Add comment Show 2 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image williamking · Oct 07, 2015 at 05:25 PM 0
Link

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?

avatar image Cladius Fernando williamking   · Oct 07, 2015 at 05:32 PM 0
Link

@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?

Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to return return any http error based on incoming request without hitting target url 1 Answer

What is the ResourceURL I have to pass if my resource(.jsc) is placed in An organizational level 8 Answers

How do I create a Request object in JavaScript and add it to the context for later use? 1 Answer

Best Practice for Cross Site Scripting(XSS) 2 Answers

How to parse XML request data using XPath notations in JavaScript 1 Answer

  • Products
    • Edge - APIs
    • Insights - Big Data
    • Plans
  • Developers
    • Overview
    • Documentation
  • Resources
    • Overview
    • Blog
    • Apigee Institute
    • Academy
    • Documentation
  • Company
    • Overview
    • Press
    • Customers
    • Partners
    • Team
    • Events
    • Careers
    • Contact Us
  • Support
    • Support Overview
    • Documentation
    • Status
    • Edge Support Portal
    • Privacy Policy
    • Terms & Conditions
© 2021 Apigee Corp. All rights reserved. - Apigee Community Terms of Use - Powered by AnswerHub
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Create an article
  • Post an idea
  • Spaces
  • Product Announcements
  • General
  • Edge/API Management
  • Developer Portal (Drupal-based)
  • Developer Portal (Integrated)
  • API Design
  • APIM on Istio
  • Extensions
  • Business of APIs
  • Academy/Certification
  • Adapter for Envoy
  • Analytics
  • Events
  • Hybrid
  • Integration (AWS, PCF, Etc.)
  • Microgateway
  • Monetization
  • Private Cloud Deployment
  • 日本語コミュニティ
  • Insights
  • IoT Apigee Link
  • BaaS/Usergrid
  • BaaS Transition/Migration
  • Apigee-127
  • New Customers
  • Explore
  • Topics
  • Questions
  • Articles
  • Ideas
  • Badges