cancel
Showing results for 
Search instead for 
Did you mean: 

CPI error "java.lang.StringIndexOutOfBoundsException" in Groovy Script

StefanK
Explorer
0 Kudos
557

Hello experts,

I have encountered the following error in a Groovy script. The error appears in several parts of the code:

com.sap.it.rt.adapter.http.api.exception.HttpResponseException: An internal server error occurred: java.lang.StringIndexOutOfBoundsException: String index out of range: 2 at line 82 in script1.groovy.
Code:
String test_type = TERST_PNR.substring(1, 2);
com.sap.it.rt.adapter.http.api.exception.HttpResponseException: An internal server error occurred: java.lang.StringIndexOutOfBoundsException: String index out of range: -2 at line 171 in script1.groovy.
Code:
if (xmlstr.substring(getXMLTag_startindex2 - 2, getXMLTag_startindex2 - 1).equals("/"))
com.sap.it.rt.adapter.http.api.exception.HttpResponseException: An internal server error occurred: java.lang.StringIndexOutOfBoundsException: String index out of range: -145 at line 195 in script1.groovy.
Code:
String tagtext = xmlstr.substring(getXMLTag_startindex2, getXMLTag_endindex1);
com.sap.it.rt.adapter.http.api.exception.HttpResponseException: An internal server error occurred: java.lang.StringIndexOutOfBoundsException: String index out of range: 3 at line 144 in script1.groovy.
Code:
"  <p1:SYSID>" + SNDPRN.substring(0, 3) + "</p1:SYSID>\r\n"

In each code section, the "substring" method is used. The error does not always occur. Three percent of all messages encounter this error.

Strangely enough, when I restart the messages in the ERP system, they are transmitted without errors. I have no chance to trace the message and reproduce the error

Has anyone experiences with this error? How can it be resolved? Is there a way to log the payload without enabling the trace?

Thank you in advance for your support!

Best regards,

Stefan

Accepted Solutions (1)

Accepted Solutions (1)

Bibhu
Participant

Always check the input string before performing the substring operation 

Can you try logging the payload if there is an error? 

import com.sap.gateway.ip.core.customdev.util.Message

def Message processData(Message message) {
    def payload = message.getBody(java.lang.String) as String
    def messageLog = messageLogFactory.getMessageLog(message)
    
    try {
        // code that could potentially throw an exception 
        String test_type = payload.substring(1, 2) 
        
    } catch (Exception e) {  
        //Log the payload and the exception details
        if (messageLog != null) {
            //Log the payload as an attachment in the message logs
            messageLog.addAttachmentAsString("Payload with Error", payload, "text/plain")
            
            //Log the exception message and type in the message properties
            messageLog.setStringProperty("ExceptionType", e.getClass().getName())
            messageLog.setStringProperty("ExceptionMessage", e.message)
        }

        // Re-throw the exception or handle it accordingly
        throw new RuntimeException("Error during processing: " + e.getClass().getName() + " - " + e.message)
    }

    return message
}

 

Answers (0)