NodeJS ScriptTarget and Faulthandling

Hi!

In my API I am using a ScriptTarget that points to a Node script and I'm having some problems with fault handling. I expected that I could add a DefaultFaultRule to the target and that any response with an error code being returned from the Node script (e.g. a 404) would be handled there.

Is this possible? I would prefer this because I already have common logic for fault handling in the proxy.

Are there best practices for fault handling together with Node Targets?

Thanks in advance,

Daniel

2 4 521
4 REPLIES 4

Not applicable

Hello @Daniel Kropshofer,

If the flow is not getting into the fault rule, then you can try the following, in order to go to fault rule.

1. Set one variable with some specific value in your node logic

2. After the response received from node, place one javascript policy and in that check the specific value of the variable and if the condition meets, thow exception. You can also set specific parameters that you may want to use in the fault rule.

If you do so, the flow would come to the fault rule as you required. Here is a how we raise exception from javascript:

if (myCondition) {

var exceptionName = "MyError";

context.setVariable("errorId", "This is my Error Id");
context.setVariable("errorDetail", "This is my Error Description");
context.setVariable("errorCode", "My error Code");
context.setVariable("exceptionName",exceptionName);

throw exceptionName;
}

And then in the Fault Rule, you can use the parameter exceptionNamein the fault rule like this (Please note that I am setting parameter exceptionName also in the context):

<FaultRules enforceAlways="true">
	<FaultRule enforceAlways="true" name="GenericFaultHandling">
		<Condition>(exceptionName = "MyError")</Condition>
		<Step>
			<Name>Your own fault rule</Name>
		</Step>
	</FaultRule>
</FaultRules>

Thank's a lot @Meghdeep Basu for your quick response and your suggestion! Well yes, I guess this would work. And in fact I already help myself with a work-around by having a JS Policy in the PostFlow of the target that looks for particular response codes in the target response and throws an exception when applicable.

But in fact I was wondering how the standard behaviour works - respectively on what grounds an error is thrown when the response from the target returns. Actually I was expecting that this would work also with ScriptTargets:

<ScriptTarget>
  <Properties>
    <Property name="success.codes">2xx</Property>
  </Properties>
  <ResourceURL>node://node_backend.js</ResourceURL>
</ScriptTarget>

Maybe somebody know more on how Apigee identifies an error when a response from a target returns?

does anyone still have an idea how to solve this with Apigee's standard fault handling?

@Daniel Kropshofer

Fundamentally, NodeJS target is not part of proxy. Remember, You are using Node.JS as a target endpoint. So, It's actually your target server. Fault rules are related to API Proxy endpoint. So, Any error in target has to be handled in target alone if your target doesn't return success code. As @Meghdeep Basu said, set variables in response / using modules like Apigee Access so that you can handle the faults in proxy response flow if target returns other than default success codes.

Regarding, How Apigee identifies an error & goes into error flow when a response from a target returns , See relevant discussion here. Find more about success.codes property here.

Hope it helps. Keep us posted if any.