on 2020 Jun 22 4:15 PM
Hi experts,
I cannot catch the exception within a groovy script. I wrappep all the code up into a try-catch statement but the message still fails (red envelope in the message monitoring). After the catch statement I put "return message".
Have you ever experienced that? What could it be?
Thanks.
Cristian
Help others by sharing your knowledge.
AnswerRequest clarification before answering.
Hi ,
In CPI try catch works fine, i have observed no issues. Please check the below code.
to execute the below create a header with the value try , if you give length of try more than 10 , MPL goes to completed. else fail as per the try catch.
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
//Body
def messageLog = messageLogFactory.getMessageLog(message);
def body = message.getBody();
def map = message.getHeaders();
def head = map.get("head");
messageLog.setLongProperty("Logging#1", head.length())
try
{
if(head.length() > 10)
{
message.setHeader("YOU_ARE_RIGHT" , head);
}
else
throw new RuntimeException("head length is less than 10", e);
}
catch(Exception e)
{
message.setHeader("FAILE", "TRUE");
}
return message;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Dear Vijay Kumar Kapuganti,
thank you for the answer. What if the exception occurred not in the main script but in a function called within the main script? I put the try-catch block even in that function but I saw same behavior. My exception is a java.lang.NullPointerException and I'm not able to catch it. Maybe you can give me a hint.
Thanks.
Cristian
Hi ,
That is also working for me it seems. Please check the sample script.
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
def messageLog = messageLogFactory.getMessageLog(message);
String testInput = "KRISHNA";
String output = test(testInput, message);
messageLog.setStringProperty("FROM_CALLIG_METHOD", output)
//Body
/*
def body = message.getBody();
def map = message.getHeaders();
def head = map.get("head");
messageLog.setLongProperty("Logging#1", head.length())
try
{
if(head.length() > 10)
{
message.setHeader("YOU_ARE_RIGHT" , head);
}
else
throw new RuntimeException("head length is less than 10", e);
}
catch(Exception e)
{
message.setHeader("FAILE", "TRUE");
}*/
return message;
}
public String test(String input, Message message)
{
def head = input;
def messageLog = messageLogFactory.getMessageLog(message);
messageLog.setLongProperty("Logging#1", head.length())
try
{
if(head.length() > 10)
{
message.setHeader("YOU_ARE_RIGHT" , head);
}
else
throw new RuntimeException("head length is less than 10", e);
}
catch(Exception e)
{
message.setHeader("FAILE", "TRUE");
}
return head;
}
Dear Vijay Kumar Kapuganti,
I used both the try-catch block you showed but still it doesn't work. Maybe because the script is running within a subprocess with a standard exception process in it? Could the exception subprocess prevent the try-catch block in the script from working? If not, maybe it's just a problem with the tenant I'm working on.
Regards.
Cristian
User | Count |
---|---|
77 | |
22 | |
9 | |
7 | |
6 | |
6 | |
4 | |
4 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.