
org.ioc.commons.flowcontrol.exceptionparser.ErrorCodeException Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ioc-commons-ext Show documentation
Show all versions of ioc-commons-ext Show documentation
Library which contains some extension classes for the library IOC-Commons.
It provides some interface definitions and some tool classes which are non-dependent from the used technology;
i.e. the classes and interfaces from this library do not depend on the choosen ioc-commons-implementation library.
The newest version!
package org.ioc.commons.flowcontrol.exceptionparser;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.ioc.commons.utils.Format;
public abstract class ErrorCodeException extends Exception {
public interface NewInstanceResolver> {
EX newInstance(E errorCode, String exceptionMessage);
}
/**
* Serial no.
*/
private static final long serialVersionUID = 7517651865520807989L;
private String detailedInfo;
public ErrorCodeException() {
}
public ErrorCodeException(String prefix, Enum> errorCode) {
super(errorCodeToString(prefix, errorCode));
}
public ErrorCodeException(String prefix, Enum> errorCode, String detailedInfo) {
super(errorCodeToString(prefix, errorCode) + " " + ensureNoBrackets(detailedInfo));
this.detailedInfo = detailedInfo;
}
public ErrorCodeException(String prefix, Enum> errorCode, Throwable cause) {
super(errorCodeToString(prefix, errorCode), cause);
}
public ErrorCodeException(String prefix, Enum> errorCode, String detailedInfo, Throwable cause) {
super(errorCodeToString(prefix, errorCode) + " " + ensureNoBrackets(detailedInfo), cause);
this.detailedInfo = detailedInfo;
}
protected static String ensureNoBrackets(String text) {
return text != null ? text.replace('[', '(').replace(']', ')') : text;
}
protected static String errorCodeToString(String prefix, Enum> errorCode) {
return Format.substitute("[{0}{1}:{2}]", prefix, errorCode.ordinal(), errorCode.name());
}
public abstract Enum> getErrorCode();
public String getDetailedInfo() {
return this.detailedInfo;
}
protected static > EX fromMessage(Class exceptionClass,
Class enumClass, String prefix, String message, NewInstanceResolver newInstanceResolver) {
String javaPattern = Format.substitute(".*\\Q[{0}\\d*\\:.*\\Q]\\E", prefix);
Pattern pattern = Pattern.compile(javaPattern);
Matcher matcher = pattern.matcher(message);
if (matcher.find()) {
try {
String stringCode = matcher.group(0);
stringCode = stringCode.substring(stringCode.indexOf("[" + prefix));
String exceptionMessage = message.substring(matcher.start() + matcher.group(0).length());
String[] parts = stringCode.split(":");
E errorCode = (E) Enum.valueOf(enumClass, parts[1].substring(0, parts[1].length() - 1));
return newInstanceResolver.newInstance(errorCode, exceptionMessage);
} catch (Exception e) {
System.err.println("Error parsing exception: " + e);
return null;
}
} else {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy