The cookbook samples aren't very good. I'm simply trying to pass some simple error info like error code, text from Java class to edge policy. Not working. Any samples out there?
Solved! Go to Solution.
If you want to return error code/message back from the Java class, set it to the MessageContext in Exception block.
In the following example, I am returning a custom "error_code" and "error_msg" back from the java class.
catch (Exception e) { messageContext.setVariable("error_code", 9999); messageContext.setVariable("error_msg", "City info is missing"); return ExecutionResult.ABORT; }
Then update the RaiseFault policy to use the error code and error message -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <RaiseFault name="Fault.MissingCityName"> <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables> <FaultResponse> <Set> <Headers/> <Payload contentType="application/xml"> <error> <httpResponseCode>409</httpResponseCode> <errorCode>{error_code}</errorCode> <errorMessage>{error_msg}</errorMessage> </error> </Payload> <StatusCode>409</StatusCode> <ReasonPhrase>Conflict</ReasonPhrase> </Set> </FaultResponse> </RaiseFault>
If you want to return error code/message back from the Java class, set it to the MessageContext in Exception block.
In the following example, I am returning a custom "error_code" and "error_msg" back from the java class.
catch (Exception e) { messageContext.setVariable("error_code", 9999); messageContext.setVariable("error_msg", "City info is missing"); return ExecutionResult.ABORT; }
Then update the RaiseFault policy to use the error code and error message -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <RaiseFault name="Fault.MissingCityName"> <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables> <FaultResponse> <Set> <Headers/> <Payload contentType="application/xml"> <error> <httpResponseCode>409</httpResponseCode> <errorCode>{error_code}</errorCode> <errorMessage>{error_msg}</errorMessage> </error> </Payload> <StatusCode>409</StatusCode> <ReasonPhrase>Conflict</ReasonPhrase> </Set> </FaultResponse> </RaiseFault>
Great illustration, Sudhee. Just a note to point out that the Codehaus Jackson library (jackson-all-1.9.11.jar) is available to the Java callout. This means that your code can also serialize a complex JSON object into a context variable, in addition to "simple" variables like strings and integers.