How to continue on error in a java callout

Not applicable

I have a java callout policy in my api proxy. My requirement is to continue with the flow even if there is any exception during the java callout. I have set the parameter continueOnError to true but it's jumping to default fault rule. My understanding is that it would have continued even after the java callout policy fails. Appreciate your inputs here. TIA

1 2 3,374
2 REPLIES 2

Not applicable
The java callout throws this error - {"fault":"{\"detail\":{\"errorcode\":\"flow.execution.ExecutionReturnedFailure\"},\"faultstring\":\"flow.execution.ExecutionReturnedFailure\"}"}

If your Java callout throws an exception, the proxy flow will transition to fault status.

My advice is for you, if you do not wish a thrown exception to result in a Fault status in the proxy: design the Java callout so that it does not throw. Catch exceptions and set a context variable.

Example code:

import org.apache.commons.lang.exception.ExceptionUtils;


public class MyCallout implements Execution {
  private static String varName(String s) {return "mycallout_" + s;}

  public ExecutionResult execute(MessageContext msgCtxt, ExecutionContext exeCtxt) {
    try {
        msgCtxt.removeVariable(varName("exception"));
        // do callout things here
    }
    catch (Exception e){
        String error = e.toString();
        msgCtxt.setVariable(varName("exception"), error);
        msgCtxt.setVariable(varName("stacktrace"), ExceptionUtils.getStackTrace(e));
    }
    return ExecutionResult.SUCCESS;
  }
}

Then in the proxy flow, after the Java callout runs, you can test the context variable "mycallout_exception". If it is non-null, then you have an exception condition.