refactor: 钉钉异常信息推送异常栈的详情

11111
winter 12 months ago
parent 1a18105096
commit d155efe947

@ -16,6 +16,8 @@ import org.springframework.web.method.annotation.MethodArgumentTypeMismatchExcep
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
@ -77,7 +79,7 @@ public class GlobalExceptionHandler {
{
String requestURI = request.getRequestURI();
log.error("请求地址'{}',发生未知异常.", requestURI, e);
notificationService.send("RuntimeException 告警: " + e.getMessage());
notificationService.send("RuntimeException 告警: " + getCausesAsString(e, 5));
return R.error(ErrorCode.SYSTEM_ERROR);
}
@ -90,7 +92,7 @@ public class GlobalExceptionHandler {
String requestURI = request.getRequestURI();
log.error("请求地址'{}',发生系统异常.", requestURI, e);
notificationService.send("Exception 告警: " + e.getMessage());
notificationService.send("Exception 告警: " + getCausesAsString(e, 5));
return R.error(e.getMessage());
}
@ -115,5 +117,10 @@ public class GlobalExceptionHandler {
String message = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
return R.error(message);
}
private static String getCausesAsString(Throwable throwable, int maxDepth) {
return Stream.iterate(throwable, Throwable::getCause)
.limit(maxDepth)
.map(e -> e.getClass().getSimpleName() + ": " + e.getMessage())
.collect(Collectors.joining(" <- "));
}
}

Loading…
Cancel
Save