I call several WebServices which return each a different exception type:
try {
//calling first WS1 here
} catch (my.package1.FaultWS_Exception e) {
StringBuilder sb = getMessageFromException(e);
log(sb);
}
try {
//calling first WS2 here
} catch (my.package2.FaultWS_Exception e) {
StringBuilder sb = getMessageFromException(e);
log(sb);
}
my getMessageFromException
simply returns a pretty printed message from the exception.
But as my exceptions are in different packages (each method is in a different WS), I had to create different getMessageFromException()
functions for each exception:
private StringBuilder getMessageFromException(my.package1.FaultWS_Exception e) {
StringBuilder sb = new StringBuilder();
if (e.getFaultInfo() != null) { //returns my.package1.FaultWS
sb.append(e.getFaultInfo().getDescfault());
sb.append("\n");
sb.append(e.getFaultInfo().getCodefault());
sb.append("\n");
} else if (e.getMessage() != null) {
sb.append(e.getMessage());
sb.append("\n");
}
return sb;
}
private StringBuilder getMessageFromException(my.package2.FaultWS_Exception e) {
StringBuilder sb = new StringBuilder();
if (e.getFaultInfo() != null) { //returns my.package2.FaultWS
sb.append(e.getFaultInfo().getDescfault());
sb.append("\n");
sb.append(e.getFaultInfo().getCodefault());
sb.append("\n");
} else if (e.getMessage() != null) {
sb.append(e.getMessage());
sb.append("\n");
}
return sb;
}
This code looks very ugly and a bit repetitive (I have 4 different signatures so far), is there a way to refactor it ? knowing that the Exceptions are defined the same, except from the package name.