package com.northconcepts.exception;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Map;
import java.util.TreeMap;
public class SystemException extends RuntimeException {
private static final long serialVersionUID = 1L;
public static SystemException wrap(Throwable exception, ErrorCode errorCode) {
if (exception instanceof SystemException) {
SystemException se = (SystemException)exception;
if (errorCode != null && errorCode != se.getErrorCode()) {
return new SystemException(exception.getMessage(), exception, errorCode);
}
return se;
} else {
return new SystemException(exception.getMessage(), exception, errorCode);
}
}
public static SystemException wrap(Throwable exception) {
return wrap(exception, null);
}
private ErrorCode errorCode;
private final Map<String,Object> properties = new TreeMap<String,Object>();
public SystemException(ErrorCode errorCode) {
this.errorCode = errorCode;
}
public SystemException(String message, ErrorCode errorCode) {
super(message);
this.errorCode = errorCode;
}
public SystemException(Throwable cause, ErrorCode errorCode) {
super(cause);
this.errorCode = errorCode;
}
public SystemException(String message, Throwable cause, ErrorCode errorCode) {
super(message, cause);
this.errorCode = errorCode;
}
public ErrorCode getErrorCode() {
return errorCode;
}
public SystemException setErrorCode(ErrorCode errorCode) {
this.errorCode = errorCode;
return this;
}
public Map<String, Object> getProperties() {
return properties;
}
@SuppressWarnings("unchecked")
public <T> T get(String name) {
return (T)properties.get(name);
}
public SystemException set(String name, Object value) {
properties.put(name, value);
return this;
}
//重写Runtime方法。这里打印虚拟机错误信息,注意PrintStream要同步,不然异常并非时会乱掉
public void printStackTrace(PrintStream s) {
synchronized (s) {
printStackTrace(new PrintWriter(s));
}
}
//把出异常时的参数也 打印出来,方便调试
public void printStackTrace(PrintWriter s) {
synchronized (s) {
s.println(this);
s.println("\t-------------------------------");
if (errorCode != null) {
s.println("\t" + errorCode + ":" + errorCode.getClass().getName());
}
for (String key : properties.keySet()) {
s.println("\t" + key + "=[" + properties.get(key) + "]");
}
s.println("\t-------------------------------");
StackTraceElement[] trace = getStackTrace();
for (int i=0; i < trace.length; i++)
s.println("\tat " + trace[i]);
Throwable ourCause = getCause();
if (ourCause != null) {
ourCause.printStackTrace(s);
}
s.flush();
}
}
}
public class SystemExceptionExample2 {
private static final int MIN_LENGTH = 10;
public static void main(String[] args) {
validate("email", "abc");
}
public static void validate(String field, String value) {
if (value == null || value.length() < MIN_LENGTH) {
throw new SystemException("发生错误!",ValidationCode.VALUE_TOO_SHORT)
.set("field", field).set("value", value).set("min-length", MIN_LENGTH);
}
}
}
public static void main(String[] args) {
System.out.println(getUserText(ValidationCode.VALUE_TOO_SHORT));
}
public static String getUserText(ErrorCode errorCode) {
if (errorCode == null) {
return null;
}
String key = errorCode.getClass().getSimpleName() + "__" + errorCode;
ResourceBundle bundle = ResourceBundle.getBundle("com.northconcepts.exception.example.exceptions");
return bundle.getString(key);
}
throw SystemException.wrap(e,PaymentCode.CREDIT_CARD_EXPIRED).set("fileName", "aaa");
418




被折叠的 条评论
为什么被折叠?



