Node JS - Raise exception

asharma377
Participant V

We are implementing the exception handling framework. much on the lines discussed here

https://community.apigee.com/articles/23724/an-error-handling-pattern-for-apigee-proxies.html

https://community.apigee.com/articles/47312/an-improved-pattern-for-fault-handling.html

However, There are cases where we are having target calls being made and we want to raise a fault from node js (basically setting http status error codes) but they are not caught in the raise fault policy. To overcome this we are now using Java script to intercept the response from node js code and then raise the fault as discussed in article below. But, facing some issues.

https://community.apigee.com/questions/33074/nodejs-scripttarget-and-faulthandling.html

1. How do i break out of the node js code to Javascript. throwException doesn't seem to work as it gives scriptexecution error.

if(errorCondition){

var exceptionName = "MyError";
apigee.setVariable(resp,"exceptionName",exceptionName);
console.log('logging the exception'+exceptionName)
throw exceptionName;
//resp.end(JSON.stringify("Missing id parameter"));

}

/*Nonerror code follows */

{

I alternately can use response.end with some blank message as i would be setting appropriate mesage/codes in raise fault but that doesn't seem to be the best way to break out of error condition. Any suggestions.

2. Also, Is it the optimized way to do so? This needs to be done for all the node js code that we will be writing so thinking from framework perspective.

thanks,

aakash

0 3 497
3 REPLIES 3

Not applicable

Given that you're leveraging Node.js and Express.js to deal with the request/response, I'd leverage either res.send or res.end as you mentioned in your message. The only twist that, I'd add to it is to set a response code that triggers fault flow, so you fault rules execute. So something like this, should work:

res.status(400).end(); 

Also, make sure that success.codes don't list 4XX. If this doesn't trigger a fault flow, then definitely use a JS policy or Fault rule to throw the fault. Please reply to confirm if you had to do so, to inform Docs team to document this issue.

0 0 0

asharma377
Participant V

Thanks@Diego Zuluaga ! node js doesn't generate error state so this will not work.

res.status(400).end();

There is a discussion on this thread below. javascript intercept seems to be the best

https://community.apigee.com/questions/33074/nodejs-scripttarget-and-faulthandling.html

0 1 0

Not applicable

Agreed. Hence the need for the JS Policy.

@docs we should capture this workaround.

0 0 0
Top Labels in this Space
Top Solution Authors