cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CPI Error Handling - Read error code in exception subprocess - CamelHttpResponseCode

madhu_reddy22
Participant
0 Kudos

Hi,

I am trying to read the CamelHttpResponseCode property in the content modifier in an exception subprocess. Below is the screenshot.

I get an error "Unknown function: CamelHttpResponseCode". When I use ${property.CamelHttpResponseCode} it returns empty value.

I also have tried to use a groovy script to get the response body and response code. But I do not understand how to access these attributes in Content Modifier. Please note that I can get the exception message by using ${exception.message} in the content modifier. But my requirement is to get the response code. Appreciate your help on this.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
                
                // get a map of iflow properties
                def map = message.getProperties();
                
                // get an exception java class instance
                def ex = map.get("CamelExceptionCaught");
                if (ex!=null) {
                                
                                // an http adapter throws an instance of org.apache.camel.component.ahc.AhcOperationFailedException
                                if (ex.getClass().getCanonicalName().equals("org.apache.camel.component.ahc.AhcOperationFailedException")) {
                                                
                                                // save the http error response as a message attachment 
                                                def messageLog = messageLogFactory.getMessageLog(message);
                                                messageLog.addAttachmentAsString("http.ResponseBody", ex.getResponseBody(), "text/plain");


                                                // copy the http error response to an iflow's property
                                                message.setProperty("http.ResponseBody",ex.getResponseBody());


                                                // copy the http error response to the message body
                                                message.setBody(ex.getResponseBody());


                                                // copy the value of http error code (i.e. 500) to a property
                                                message.setProperty("http.StatusCode",ex.getStatusCode());


                                                // copy the value of http error text (i.e. "Internal Server Error") to a property
                                                message.setProperty("http.StatusText",ex.getStatusText());
                                                
                                }
                }


                return message;
}

Thank you,

Madhu

Accepted Solutions (1)

Accepted Solutions (1)

r_herrmann
Active Contributor

Hi Madhu,

in Exception Subprocess the "CamelHttpResponseCode" isn't available. In general the right approach would be to parse the Exception object which you can retrieve from the property "CamelExceptionCaught". So in general you approach using the Groovy script was good, but in your specific case that won't work. As you can see in your script, the script checks the class type of the Exception (ex.getClass().getCanonicalName().equals("org.apache.camel.component.ahc.AhcOperationFailedException")) - that is done because not all Exception types contain methods/properties that return/contain the HTTP response status code. Unfortunately the SuccessFactors channel doesn't throw exception of type "AhcOperationFailedException" but it throws "com.sap.it.rt.adapter.sfsf.wsclient.SfsfWsClientFaultException".

And the "com.sap.it.rt.adapter.sfsf.wsclient.SfsfWsClientFaultException" exception type does not contain any information about the HTTP status code (at least not accessable via a method/property).

Long story short: When using the SuccessFactors channel type it (currently/for now) isn't possible to retrieve the HTTP status code in an Exception Subprocess.

madhu_reddy22
Participant
0 Kudos

Thank you Raffael. Is there a way that we can read the error response payload(not the Camel exception) instead of the exception message? For example if we do the same call via postman then the response has a definite structure. In the example below the response structure has a code and a message. The {in.body} holds the original message and not the error response payload after the call is made.

r_herrmann
Active Contributor
0 Kudos

Hi Madhu,

Inside the exception process you can only work with the things given by the exception object. The message body unfortunately doesn't give you access to the response body. The only option I can think of, is using an http adapter instead of the sf adapter. When using http adapter you get another exception object which gives you access to the server's response. But at the same time it comes with the huge downside, that you have to implement all the logic, which the sf adapter is doing for you, yourself.

madhu_reddy22
Participant
0 Kudos

Thanks a bunch for the clarification Raffael.

philippeaddor
Active Participant

Hi guys,

I just came accross this old quesiton when Googling for something related. I would say the statement "in Exception Subprocess the CamelHttpResponseCode isn't available" doesn't hold true anymore nowadays (I guess SAP improved it). As far as I can tell, I have this header (and the exception in the ${Body}) available in the Exception SP for reading and writing.

Best regards,

Philippe

Thank You Phillippe.

Is working for me.

Answers (1)

Answers (1)

madhu_reddy22
Participant

Update to the original post: I have used ${header.CamelHttpResponseCode} . This variable works in the main flow but not in the exception subprocess.

Thank you,

Madhu