perf[exception]: format the exception log

This commit is contained in:
godotg
2023-01-06 22:29:52 +08:00
parent d0aa341c06
commit 5ef04e0b17
10 changed files with 85 additions and 72 deletions
@@ -32,7 +32,6 @@ import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.registration.ProtocolModule;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -109,8 +108,7 @@ public class Consumer implements IConsumer {
throw new ErrorResponseException((Error) responsePacket);
}
if (answerClass != null && answerClass != responsePacket.getClass()) {
throw new UnexpectedProtocolException(StringUtils.format("client expect protocol:[{}], but found protocol:[{}]"
, answerClass, responsePacket.getClass().getName()));
throw new UnexpectedProtocolException("client expect protocol:[{}], but found protocol:[{}]", answerClass, responsePacket.getClass().getName());
}
var syncAnswer = new SyncAnswer<>((T) responsePacket, clientSignalAttachment);
@@ -118,8 +116,7 @@ public class Consumer implements IConsumer {
loadBalancer.afterLoadBalancer(session, packet, clientSignalAttachment);
return syncAnswer;
} catch (TimeoutException e) {
throw new NetTimeOutException(StringUtils.format("syncAsk timeout exception, ask:[{}], attachment:[{}]"
, JsonUtils.object2String(packet), JsonUtils.object2String(clientSignalAttachment)));
throw new NetTimeOutException("syncAsk timeout exception, ask:[{}], attachment:[{}]", JsonUtils.object2String(packet), JsonUtils.object2String(clientSignalAttachment));
} finally {
SignalBridge.removeSignalAttachment(clientSignalAttachment);
}
@@ -205,12 +205,12 @@ public class Router implements IRouter {
throw new ErrorResponseException((Error) responsePacket);
}
if (answerClass != null && answerClass != responsePacket.getClass()) {
throw new UnexpectedProtocolException(StringUtils.format("client expect protocol:[{}], but found protocol:[{}]", answerClass, responsePacket.getClass().getName()));
throw new UnexpectedProtocolException("client expect protocol:[{}], but found protocol:[{}]", answerClass, responsePacket.getClass().getName());
}
return new SyncAnswer<>((T) responsePacket, clientSignalAttachment);
} catch (TimeoutException e) {
throw new NetTimeOutException(StringUtils.format("syncAsk timeout exception, ask:[{}], attachment:[{}]", JsonUtils.object2String(packet), JsonUtils.object2String(clientSignalAttachment)));
throw new NetTimeOutException("syncAsk timeout exception, ask:[{}], attachment:[{}]", JsonUtils.object2String(packet), JsonUtils.object2String(clientSignalAttachment));
} finally {
SignalBridge.removeSignalAttachment(clientSignalAttachment);
}
@@ -240,7 +240,7 @@ public class Router implements IRouter {
// 因此超时的情况,返回的是null
.completeOnTimeout(null, DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS).thenApply(answer -> {
if (answer == null) {
throw new NetTimeOutException(StringUtils.format("async ask [{}] timeout exception", packet.getClass().getSimpleName()));
throw new NetTimeOutException("async ask [{}] timeout exception", packet.getClass().getSimpleName());
}
if (answer.protocolId() == Error.errorProtocolId()) {
@@ -276,7 +276,7 @@ public class Router implements IRouter {
asyncAnswer.setFuturePacket((T) answer);
asyncAnswer.consume();
} catch (Throwable throwable1) {
logger.error("异步回调方法[ask:{}][answer:{}]错误", packet.getClass().getSimpleName(), answer.getClass().getSimpleName(), throwable1);
logger.error("Asynchronous callback method [ask:{}][answer:{}] error", packet.getClass().getSimpleName(), answer.getClass().getSimpleName(), throwable1);
} finally {
if (serverSignalAttachment != null) {
serverReceiveSignalAttachmentThreadLocal.set(null);
@@ -13,14 +13,16 @@
package com.zfoo.net.router.exception;
import com.zfoo.protocol.exception.RunException;
/**
* @author godotg
* @version 3.0
*/
public class NetTimeOutException extends RuntimeException {
public class NetTimeOutException extends RunException {
public NetTimeOutException(String s) {
super(s);
public NetTimeOutException(String template, Object... args) {
super(template, args);
}
}
}
@@ -21,23 +21,8 @@ import com.zfoo.protocol.exception.RunException;
*/
public class UnexpectedProtocolException extends RunException {
public UnexpectedProtocolException(Throwable cause) {
super(cause);
}
public UnexpectedProtocolException(String message) {
super(message);
}
public UnexpectedProtocolException(String template, Object... args) {
super(template, args);
}
public UnexpectedProtocolException(Throwable cause, String message) {
super(cause, message);
}
public UnexpectedProtocolException(Throwable cause, String template, Object... args) {
super(cause, template, args);
}
}
@@ -123,7 +123,7 @@ public abstract class PacketBus {
ReflectionUtils.makeAccessible(receiverField);
ReflectionUtils.setField(receiverField, protocolRegistration, enhanceReceiverDefinition);
} catch (Throwable t) {
throw new RunException(t, "Registration protocol [class:{}] unknown exception", packetClazz.getSimpleName());
throw new RunException("Registration protocol [class:{}] unknown exception", packetClazz.getSimpleName(), t);
}
}
}
@@ -28,16 +28,48 @@ public class RunException extends RuntimeException {
super(message);
}
public RunException(String message, Throwable cause) {
super(message, cause);
}
public RunException(String template, Object arg1, Throwable cause) {
super(StringUtils.format(template, arg1), cause);
}
public RunException(String template, Object arg1, Object arg2, Throwable cause) {
super(StringUtils.format(template, arg1, arg2), cause);
}
public RunException(String template, Object arg1, Object arg2, Object arg3, Throwable cause) {
super(StringUtils.format(template, arg1, arg2, arg3), cause);
}
public RunException(String template, Object arg1, Object arg2, Object arg3, Object arg4, Throwable cause) {
super(StringUtils.format(template, arg1, arg2, arg3, arg4), cause);
}
public RunException(String template, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Throwable cause) {
super(StringUtils.format(template, arg1, arg2, arg3, arg4, arg5), cause);
}
public RunException(String template, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Throwable cause) {
super(StringUtils.format(template, arg1, arg2, arg3, arg4, arg5, arg6), cause);
}
public RunException(String template, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Throwable cause) {
super(StringUtils.format(template, arg1, arg2, arg3, arg4, arg5, arg6, arg7), cause);
}
public RunException(String template, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Throwable cause) {
super(StringUtils.format(template, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8), cause);
}
public RunException(String template, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Object arg9, Throwable cause) {
super(StringUtils.format(template, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9), cause);
}
public RunException(String template, Object... args) {
super(StringUtils.format(template, args));
}
public RunException(Throwable cause, String message) {
super(message, cause);
}
public RunException(Throwable cause, String template, Object... args) {
super(StringUtils.format(template, args), cause);
}
}
@@ -47,7 +47,7 @@ public abstract class DomUtils {
try {
return MAPPER.readValue(xml, clazz);
} catch (IOException e) {
throw new RunException(e, "将xml字符串[xml:{}]转换为对象[{}]异常", xml, clazz);
throw new RunException("将xml字符串[xml:{}]转换为对象[{}]异常", xml, clazz, e);
}
}
@@ -57,7 +57,7 @@ public abstract class DomUtils {
var sr = f.createXMLStreamReader(new FileInputStream(xmlFile));
return MAPPER.readValue(sr, clazz);
} catch (Exception e) {
throw new RunException(e, "将xml文件[xml:{}]转换为对象[{}]异常", xmlFile, clazz);
throw new RunException("将xml文件[xml:{}]转换为对象[{}]异常", xmlFile, clazz, e);
}
}
@@ -65,7 +65,7 @@ public abstract class DomUtils {
try {
return MAPPER.readValue(xmlInputStream, clazz);
} catch (Exception e) {
throw new RunException(e, "将xmlInputStream转换为对象[{}]异常", clazz);
throw new RunException("将xmlInputStream转换为对象[{}]异常", clazz, e);
}
}
@@ -70,7 +70,7 @@ public abstract class JsonUtils {
try {
return MAPPER.readValue(json, clazz);
} catch (Exception e) {
throw new RunException(e, "将json字符串[json:{}]转换为对象[class:{}]时异常", json, clazz);
throw new RunException("将json字符串[json:{}]转换为对象[class:{}]时异常", json, clazz, e);
}
}
@@ -79,7 +79,7 @@ public abstract class JsonUtils {
try {
return MAPPER.writeValueAsString(object);
} catch (Exception e) {
throw new RunException(e, "将对象[object:{}]转换为json字符串时异常", object);
throw new RunException("将对象[object:{}]转换为json字符串时异常", object, e);
}
}
@@ -88,7 +88,7 @@ public abstract class JsonUtils {
try {
return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(object);
} catch (Exception e) {
throw new RunException(e, "将对象[object:{}]转换为json字符串时异常", object);
throw new RunException("将对象[object:{}]转换为json字符串时异常", object, e);
}
}
@@ -99,7 +99,7 @@ public abstract class JsonUtils {
try {
return MAPPER_TURBO.readValue(json, clazz);
} catch (Exception e) {
throw new RunException(e, "将json字符串[json:{}]转换为对象[class:{}]时异常", json, clazz);
throw new RunException("将json字符串[json:{}]转换为对象[class:{}]时异常", json, clazz, e);
}
}
@@ -110,7 +110,7 @@ public abstract class JsonUtils {
try {
return MAPPER_TURBO.writeValueAsString(object);
} catch (Exception e) {
throw new RunException(e, "将对象[object:{}]转换为json字符串时异常", object);
throw new RunException("将对象[object:{}]转换为json字符串时异常", object, e);
}
}
@@ -119,7 +119,7 @@ public abstract class JsonUtils {
try {
return MAPPER.readValue(json, collectionType);
} catch (Exception e) {
throw new RunException(e, "将json字符串[json:{}]转换为List[{}]异常", json, clazz);
throw new RunException("将json字符串[json:{}]转换为List[{}]异常", json, clazz, e);
}
}
@@ -129,7 +129,7 @@ public abstract class JsonUtils {
try {
return MAPPER.readValue(json, collectionType);
} catch (Exception e) {
throw new RunException(e, "将json字符串[json:{}]转换为Set[{}]异常", json, clazz);
throw new RunException("将json字符串[json:{}]转换为Set[{}]异常", json, clazz, e);
}
}
@@ -138,7 +138,7 @@ public abstract class JsonUtils {
var ct = MAPPER.getTypeFactory().constructCollectionType(collectionType, elementType);
return MAPPER.readValue(json, ct);
} catch (IOException e) {
throw new RunException(e, "将json字符串[json:{}]转换为Collection[{}]异常", json, collectionType);
throw new RunException("将json字符串[json:{}]转换为Collection[{}]异常", json, collectionType, e);
}
}
@@ -147,7 +147,7 @@ public abstract class JsonUtils {
try {
return MAPPER.readValue(json, mapType);
} catch (Exception e) {
throw new RunException(e, "将json字符串[json:{}]转换为Map[[key:{}], [value:{}]]异常", json, kClazz, vClazz);
throw new RunException("将json字符串[json:{}]转换为Map[[key:{}], [value:{}]]异常", json, kClazz, vClazz, e);
}
}
@@ -156,7 +156,7 @@ public abstract class JsonUtils {
try {
return MAPPER.readValue(json, arrayType);
} catch (Exception e) {
throw new RunException(e, "将json字符串[{}]转换为数组[array:{}]异常", json, clazz);
throw new RunException("将json字符串[{}]转换为数组[array:{}]异常", json, clazz, e);
}
}
@@ -190,7 +190,7 @@ public abstract class JsonUtils {
}
}
} catch (IOException e) {
throw new RunException(e, "将json字符串[json:{}]转换为jsonTree[nodeName:{}]异常", json, nodeName);
throw new RunException("将json字符串[json:{}]转换为jsonTree[nodeName:{}]异常", json, nodeName, e);
}
return null;
}
@@ -216,7 +216,7 @@ public abstract class JsonUtils {
}
return jsonMap;
} catch (IOException e) {
throw new RunException(e, "将json字符串[json:{}]转换为jsonMap异常", json);
throw new RunException("将json字符串[json:{}]转换为jsonMap异常", json, e);
}
}
}
@@ -23,9 +23,7 @@ import org.apache.poi.ss.usermodel.WorkbookFactory;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author meiwei666
@@ -107,7 +105,7 @@ public abstract class ExcelReader {
try {
return WorkbookFactory.create(input);
} catch (IOException e) {
throw new RunException(e, "Static resource [{}] is abnormal, and the file cannot be read", fileName);
throw new RunException("Static resource [{}] is abnormal, and the file cannot be read", fileName, e);
}
}
}
@@ -94,7 +94,7 @@ public class ResourceInterpreter {
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, instance, value);
} catch (Exception e) {
throw new RunException(e, "Unable to convert [content:{}] to property [field:{}] in Excel resource [class:{}]", content, field.getName(), instance.getClass().getSimpleName());
throw new RunException("Unable to convert [content:{}] to property [field:{}] in Excel resource [class:{}]", content, field.getName(), instance.getClass().getSimpleName(), e);
}
}
@@ -109,24 +109,23 @@ public class ResourceInterpreter {
}
}
}
return fieldList.stream().filter(it1->
fieldMap.keySet().stream().anyMatch(it->{
var ans=false;
if(it1.isAnnotationPresent(ExcelFieldName.class)){
ans|=it.equals(it1.getAnnotation(ExcelFieldName.class).value());
}
ans|=it.equals(it1.getName());
return ans;
})).map(it1->{
String excelFieldName;
List<String> list=null;
if(it1.isAnnotationPresent(ExcelFieldName.class)){
list=fieldMap.keySet().stream().filter(it->it.equals(it1.getAnnotation(ExcelFieldName.class).value())).collect(Collectors.toList());
return fieldList.stream().filter(it1 -> fieldMap.keySet().stream().anyMatch(it -> {
var ans = false;
if (it1.isAnnotationPresent(ExcelFieldName.class)) {
ans |= it.equals(it1.getAnnotation(ExcelFieldName.class).value());
}
if(list==null||list.size()==0){
list=fieldMap.keySet().stream().filter(it->it.equals(it1.getName())).collect(Collectors.toList());
ans |= it.equals(it1.getName());
return ans;
})).map(it1 -> {
String excelFieldName;
List<String> list = null;
if (it1.isAnnotationPresent(ExcelFieldName.class)) {
list = fieldMap.keySet().stream().filter(it -> it.equals(it1.getAnnotation(ExcelFieldName.class).value())).collect(Collectors.toList());
}
excelFieldName=list.get(0);
if (list == null || list.size() == 0) {
list = fieldMap.keySet().stream().filter(it -> it.equals(it1.getName())).collect(Collectors.toList());
}
excelFieldName = list.get(0);
return new FieldInfo(fieldMap.get(excelFieldName), it1);
}).collect(Collectors.toList());
}