cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Is There a Script Example for Exception Handling in OData V4 Receiver Adapter on Cloud Integration

eakucuk
Explorer
2,024

The SAP documentation provides script examples for the OData V2 Receiver Adapter and the HTTP Receiver Adapter. Using these scripts, we are able to read the error body, which is added as an attachment named ODataV2_Adapter_Response_Body

I am currently using the OData V4 Adapter for the Purchase Order API. I attempted to write a similar script to the one used for the OData V2 adapter, but it did not work.

Is there a way to get the error body for the OData V4 adapter in Cloud Integration?

Reference to SAP Documentation: Handle Exceptions 

Thank you for your assistance.

Accepted Solutions (1)

Accepted Solutions (1)

RenatoMiranda
Explorer

Hi, I don't know if you found the answer already, but I'm facing the same issue, even with the same API. I had to change to HTTP Post, instead of using oData v4, so I could get the error response body.

emreanil
Explorer

Hi Miranda,

Yes, I found a solution. Here is the groovy script. You can use this script for Odata V4 Adapter error handling.

 

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import org.apache.http.StatusLine;
import org.apache.olingo.client.api.communication.ODataClientErrorException;
import org.apache.olingo.commons.api.ex.ODataError;
def Message processData(Message message) {
    // get a map of properties
    def map = message.getProperties();

 

    // get an exception java class instance
    def ex = map.get("CamelExceptionCaught");
    if (ex != null) {
        // ex               -> org.apache.camel.RuntimeCamelException
        // ex.getCause()    -> com.sap.gateway.core.ip.component.exceptions.OData4Exception
        //ex.getCause().getCause() -> com.sap.gateway.core.ip.component.exception.ODataProcessingException
        def odataException = ex.getCause().getCause();
        //message.setProperty("Exception for OData", odataException.getClass().getCanonicalName());
        // an odata v4 adapter throws an instance of com.sap.gateway.core.ip.component.exceptions.ODataProcessingException
        if (odataException.getClass().getCanonicalName().equals("com.sap.gw.core.ip.exception.ODataResponseParsingException") || odataException.getClass().getCanonicalName().equals("com.sap.gateway.core.ip.component.exception.ODataProcessingException")) {

 

            message.setProperty("http.response", message.getBody());
            message.setProperty("ODataProcessingException.Cause", odataException.getCause().getClass().getCanonicalName());
            if (odataException.getCause().getClass().getCanonicalName().equals("org.apache.olingo.client.api.communication.ODataClientErrorException")) {
                message.setProperty("http.statusLine", odataException.getCause().getStatusLine()); //To get HTTP status
                message.setProperty("http.statusCode", odataException.getCause().getStatusLine().getStatusCode().toString()); //To get HTTP status code

 

                message.setProperty("ODataError", odataException.getCause().getODataError());
                message.setProperty("ODataError.Message", odataException.getCause().getODataError().getMessage());
                message.setProperty("ODataError.Code", odataException.getCause().getODataError().getCode());
                message.setProperty("ODataError.InnerError", odataException.getCause().getODataError().getInnerError().toString());
            } else if (odataException.getCause().getClass().getCanonicalName().equals("org.apache.olingo.client.api.communication.ODataServerErrorException")) {
                message.setProperty("http.statusLine", odataException.getCause().getMessage());
            }

 

        }
    }

 

    return message;
}

 

 

 

 

 

RenatoMiranda
Explorer
Thanks for the reply. It realy helped. I just had to change a few things.
RenatoMiranda
Explorer
... continuing... the line "def odataException = ex.getCause().getCause();" I changed to "def odataException = ex.getCause();". The error list I get using this code " odataException.getCause().getODataError().getDetails().each { detail -> // Adiciona a mensagem de erro à lista message.setProperty("ODataError.Detail", detail.getMessage()) }"

Answers (1)

Answers (1)

RenatoMiranda
Explorer

Finaly, I created this groovy that gets the response body Json.

import groovy.json.*;
import com.sap.gateway.ip.core.customdev.util.Message;
def Message processData(Message message) {
// get a map of properties
def map = message.getProperties();

// get an exception java class instance
def ex = map.get("CamelExceptionCaught");
if (ex != null) {
def odataException = ex.getCause();
if (odataException.getClass().getCanonicalName().equals("com.sap.gw.core.ip.exception.ODataResponseParsingException") ||
odataException.getClass().getCanonicalName().equals("com.sap.gateway.core.ip.component.exception.ODataProcessingException")) {
if (odataException.getCause().getClass().getCanonicalName().equals("org.apache.olingo.client.api.communication.ODataClientErrorException")) {
// Convert payload to JSON
def jsonBuilder = new JsonBuilder(odataException.getCause().getODataError());
message.setBody(jsonBuilder.toPrettyString());
message.setHeader("CamelHttpResponseCode", "200");
}
}
}

return message;
}

 

This is the return:

RenatoMiranda_0-1736952834049.png

 

emreanil
Explorer

Thank you for your contributions 🙂

marcelom_bovo
Participant

Hi,

Thanks, it worked for me, but I had to revert the code bellow

def odataException = ex.getCause();

to

def odataException = ex.getCause().getCause();

 

RenatoMiranda
Explorer
0 Kudos
Yes marcelom_bovo, depends on the odataException returned. But I'm glad that worked for you too.