增加异常反打实现,避免异常堆栈信息过长

This commit is contained in:
2024-09-24 09:56:27 +08:00
parent ca49d31260
commit a27f64dafa
3 changed files with 107 additions and 9 deletions

View File

@@ -8,7 +8,7 @@ import java.io.Serializable;
import java.util.Date;
/**
*
* 叫号桌型表
* @TableName tb_call_table
*/
@TableName(value ="tb_call_table")
@@ -74,6 +74,16 @@ public class TbCallTable implements Serializable {
*/
private Date updateTime;
/**
* 顺延号码数量
*/
private Integer isPostpone;
/**
* 顺延号码数量
*/
private Integer postponeNum;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
@@ -245,6 +255,34 @@ public class TbCallTable implements Serializable {
this.updateTime = updateTime;
}
/**
* 顺延号码数量
*/
public Integer getIsPostpone() {
return isPostpone;
}
/**
* 顺延号码数量
*/
public void setIsPostpone(Integer isPostpone) {
this.isPostpone = isPostpone;
}
/**
* 顺延号码数量
*/
public Integer getPostponeNum() {
return postponeNum;
}
/**
* 顺延号码数量
*/
public void setPostponeNum(Integer postponeNum) {
this.postponeNum = postponeNum;
}
@Override
public boolean equals(Object that) {
if (this == that) {
@@ -268,7 +306,9 @@ public class TbCallTable implements Serializable {
&& (this.getShopId() == null ? other.getShopId() == null : this.getShopId().equals(other.getShopId()))
&& (this.getQrcode() == null ? other.getQrcode() == null : this.getQrcode().equals(other.getQrcode()))
&& (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
&& (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime()));
&& (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime()))
&& (this.getIsPostpone() == null ? other.getIsPostpone() == null : this.getIsPostpone().equals(other.getIsPostpone()))
&& (this.getPostponeNum() == null ? other.getPostponeNum() == null : this.getPostponeNum().equals(other.getPostponeNum()));
}
@Override
@@ -287,6 +327,8 @@ public class TbCallTable implements Serializable {
result = prime * result + ((getQrcode() == null) ? 0 : getQrcode().hashCode());
result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode());
result = prime * result + ((getIsPostpone() == null) ? 0 : getIsPostpone().hashCode());
result = prime * result + ((getPostponeNum() == null) ? 0 : getPostponeNum().hashCode());
return result;
}
@@ -308,6 +350,8 @@ public class TbCallTable implements Serializable {
sb.append(", qrcode=").append(qrcode);
sb.append(", createTime=").append(createTime);
sb.append(", updateTime=").append(updateTime);
sb.append(", isPostpone=").append(isPostpone);
sb.append(", postponeNum=").append(postponeNum);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();

View File

@@ -2,6 +2,7 @@ package com.chaozhanggui.system.cashierservice.exception;
import com.chaozhanggui.system.cashierservice.sign.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -11,6 +12,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
* ControllerAdvice注解的含义是当异常抛到controller层时会拦截下来
*/
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
/**
@@ -26,6 +28,14 @@ public class GlobalExceptionHandler {
return Result.fail("系统内部错误");
}
@ResponseBody
@ExceptionHandler(NotPrintException.class)
public Result exceptionNotPrint(Exception e) {
log.warn("获取到不打印堆栈异常, 异常信息: {}", e.getMessage());
// 返回错误格式信息
return Result.fail(e.getMessage());
}
/**
* 使用ExceptionHandler注解声明处理TestException异常
*

View File

@@ -21,7 +21,7 @@ public class Utils {
public static <T> void catchErrNoReturn(Supplier<T> supplier) {
try {
supplier.get();
}catch (Exception e) {
} catch (Exception e) {
log.error("执行方法出现异常", e);
}
}
@@ -47,8 +47,8 @@ public class Utils {
}
}
public static<T> T runFunAndCheckKey(Supplier<T> supplier, StringRedisTemplate redisTemplate, String lockKey) {
try{
public static <T> T runFunAndCheckKey(Supplier<T> supplier, StringRedisTemplate redisTemplate, String lockKey) {
try {
// 创建线程id, 用作判断
String clientId = UUID.randomUUID().toString();
// 设置分布式锁
@@ -62,12 +62,12 @@ public class Utils {
lock = Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(lockKey, clientId, 30, TimeUnit.MILLISECONDS));
}
return supplier.get();
} catch (RuntimeException e){
} catch (RuntimeException e) {
log.info("执行出错:{}", e.getMessage());
throw e;
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally{
} finally {
redisTemplate.delete(lockKey);
}
}
@@ -110,11 +110,11 @@ public class Utils {
}
public static void checkValueUnReturn(Object data, String msg, Func0<?>... errFunc) {
if(data == null) {
if (data == null) {
throw new MsgException(msg);
}
if (data instanceof String && StrUtil.isBlank((String)data)) {
if (data instanceof String && StrUtil.isBlank((String) data)) {
throw new MsgException(msg);
}
if (ObjectUtil.isEmpty(data)) {
@@ -128,4 +128,48 @@ public class Utils {
throw new MsgException(msg);
}
}
public static void doPrintStack(Throwable t, int stackDepth, StringBuilder sb) {
StackTraceElement[] stackTraceElements = t.getStackTrace();
if (sb.lastIndexOf("\t") > -1) {
sb.deleteCharAt(sb.length() - 1);
sb.append("Caused: ");
}
sb.append(t.getClass().getName()).append(": ").append(t.getMessage()).append("\n\t");
for (int i = 0; i < stackDepth; ++i) {
if (i >= stackTraceElements.length) {
break;
}
StackTraceElement element = stackTraceElements[i];
sb.append(element.getClassName()).append("#").append(element.getMethodName()).append(":").append(element.getLineNumber()).append("\n\t");
}
}
/**
* 递归逆向打印堆栈及cause(即从最底层的异常开始往上打)
* @param t 原始异常 *
* @param causeDepth 需要递归打印的cause的最大深度 *
* @param counter 当前打印的cause的深度计数器(这里必须用引用类型,如果用基本数据类型,你对计数器的修改只能对当前栈帧可见,但是这个计数器,又必须在所有栈帧中可见,所以只能用引用类型) *
* @param stackDepth 每一个异常栈的打印深度 *
* @param sb 字符串构造器
*/
public static void recursiveReversePrintStackCause(Throwable t, int causeDepth, ForwardCounter counter, int stackDepth, StringBuilder sb) {
if (t == null) {
return;
}
if (t.getCause() != null) {
recursiveReversePrintStackCause(t.getCause(), causeDepth, counter, stackDepth, sb);
}
if (counter.i++ < causeDepth) {
doPrintStack(t, stackDepth, sb);
}
}
public static class ForwardCounter {
private Integer i;
public ForwardCounter(Integer i) {
this.i = i;
}
}
}