| 12 | import java.util.regex.Pattern; |
| 13 | |
| 14 | public class BaseException extends RuntimeException { |
| 15 | private static final Logger logger = LogManager.getLogger(BaseException.class); |
| 16 | |
| 17 | private static Map<String, String> codeMap = new HashMap<>(); |
| 18 | |
| 19 | private BaseException(String message) { |
| 20 | super(message); |
| 21 | } |
| 22 | |
| 23 | private BaseException(Throwable cause) { |
| 24 | super(cause); |
| 25 | } |
| 26 | |
| 27 | private BaseException(String message, Throwable cause) { |
| 28 | super(message, cause); |
| 29 | } |
| 30 | |
| 31 | public static BaseException errorCode(String code) throws BaseException { |
| 32 | String message = codeMap.get(code); |
| 33 | if (message == null) { |
| 34 | throw new BaseException("错误编码【" + code + "】未定义。"); |
| 35 | } |
| 36 | return new BaseException(message); |
| 37 | } |
| 38 | |
| 39 | public static BaseException errorCode(String code, String... params) throws BaseException { |
| 40 | String message = codeMap.get(code); |
| 41 | if (message == null) { |
| 42 | throw new BaseException("错误编码【" + code + "】未定义。"); |
| 43 | } |
| 44 | message = bind(message, params); |
| 45 | return new BaseException(message); |
| 46 | } |
| 47 | |
| 48 | private static String bind(String s, String... binds) { |
| 49 | if (binds != null && binds.length > 0) { |
| 50 | Pattern pattern = Pattern.compile("%v", 34); |
| 51 | StringBuffer buff = new StringBuffer(); |
| 52 | Matcher m = pattern.matcher(s); |
| 53 | int i = 0; |
| 54 | |
| 55 | for(int len = binds.length; i < len && m.find(); ++i) { |
| 56 | m.appendReplacement(buff, binds[i]); |
| 57 | } |
| 58 | |
| 59 | m.appendTail(buff); |
| 60 | return buff.toString(); |
| 61 | } else { |
| 62 | return s; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public static BaseException errorCode(String code, Throwable cause) throws BaseException { |
| 67 | String message = codeMap.get(code); |
| 68 | if (message == null) { |
| 69 | throw new BaseException("错误编码【" + code + "】未定义。"); |
| 70 | } |
| 71 | return new BaseException(message, cause); |
nothing calls this directly
no outgoing calls
no test coverage detected