【优化】优化异常打印,不使用e.printStackTrace方式

pull/103/head^2
xuyuxiang 2023-03-23 14:13:00 +08:00 committed by 小诺
parent 3febc21536
commit 60049933ae
11 changed files with 41 additions and 40 deletions

View File

@ -16,6 +16,7 @@ import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem; import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import vip.xiaonuo.common.util.CommonServletUtil; import vip.xiaonuo.common.util.CommonServletUtil;
import java.util.List; import java.util.List;
@ -26,6 +27,7 @@ import java.util.List;
* @author xuyuxiang * @author xuyuxiang
* @date 2021/12/18 14:43 * @date 2021/12/18 14:43
*/ */
@Slf4j
public class CommonPageRequest { public class CommonPageRequest {
private static final String PAGE_SIZE_PARAM_NAME = "size"; private static final String PAGE_SIZE_PARAM_NAME = "size";
@ -53,7 +55,7 @@ public class CommonPageRequest {
size = PAGE_SIZE_MAX_VALUE; size = PAGE_SIZE_MAX_VALUE;
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 分页条数转换异常:", e);
size = 20; size = 20;
} }
} }
@ -64,7 +66,7 @@ public class CommonPageRequest {
try { try {
page = Convert.toInt(pageString); page = Convert.toInt(pageString);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 分页页数转换异常:", e);
page = 1; page = 1;
} }
} }

View File

@ -15,6 +15,7 @@ package vip.xiaonuo.common.util;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.URLUtil; import cn.hutool.core.util.URLUtil;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
@ -26,6 +27,7 @@ import java.io.IOException;
* @author xuyuxiang * @author xuyuxiang
* @date 2020/8/5 21:45 * @date 2020/8/5 21:45
*/ */
@Slf4j
public class CommonDownloadUtil { public class CommonDownloadUtil {
/** /**
@ -55,7 +57,7 @@ public class CommonDownloadUtil {
response.setContentType("application/octet-stream;charset=UTF-8"); response.setContentType("application/octet-stream;charset=UTF-8");
IoUtil.write(response.getOutputStream(), true, fileBytes); IoUtil.write(response.getOutputStream(), true, fileBytes);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); log.error(">>> 文件下载异常:", e);
} }
} }
} }

View File

@ -56,16 +56,16 @@ public class CommonIpAddressUtil {
try { try {
cBuff = Searcher.loadContentFromFile(dbPath); cBuff = Searcher.loadContentFromFile(dbPath);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> CommonIpAddressUtil初始化异常", e);
throw new CommonException("CommonIpAddressUtil初始化失败,原因:", e.getMessage()); throw new CommonException("CommonIpAddressUtil初始化异常");
} }
// 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。 // 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。
try { try {
searcher = Searcher.newWithBuffer(cBuff); searcher = Searcher.newWithBuffer(cBuff);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> CommonIpAddressUtil初始化异常", e);
throw new CommonException("CommonIpAddressUtil初始化失败,原因:", e.getMessage()); throw new CommonException("CommonIpAddressUtil初始化异常");
} }
} }
@ -83,7 +83,7 @@ public class CommonIpAddressUtil {
String remoteHost = ServletUtil.getClientIP(request); String remoteHost = ServletUtil.getClientIP(request);
return LOCAL_REMOTE_HOST.equals(remoteHost) ? Ipv4Util.LOCAL_IP : remoteHost; return LOCAL_REMOTE_HOST.equals(remoteHost) ? Ipv4Util.LOCAL_IP : remoteHost;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 获取客户端ip异常", e);
return Ipv4Util.LOCAL_IP; return Ipv4Util.LOCAL_IP;
} }
} }

View File

@ -67,14 +67,13 @@ public class CommonNetWorkInfoUtil {
result.put("UP", upSpeed + (upSpeed.endsWith("B")?"/S":"B/S")); result.put("UP", upSpeed + (upSpeed.endsWith("B")?"/S":"B/S"));
result.put("DOWN", downSpeed + (downSpeed.endsWith("B")?"/S":"B/S")); result.put("DOWN", downSpeed + (downSpeed.endsWith("B")?"/S":"B/S"));
} catch (Exception e) { } catch (Exception e) {
log.info(">>> 网络测速失败,原因:"); log.info(">>> 网络测速失败:", e);
e.printStackTrace();
} finally { } finally {
if (input != null) { if (input != null) {
try { try {
input.close(); input.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); log.info(">>> 网络测速失败:", e);
} }
} }
Optional.ofNullable(pro).ifPresent(Process::destroy); Optional.ofNullable(pro).ifPresent(Process::destroy);
@ -119,7 +118,7 @@ public class CommonNetWorkInfoUtil {
arr[1] = tx; arr[1] = tx;
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 网络测速异常:", e);
} }
return arr; return arr;
} }

View File

@ -13,6 +13,7 @@
package vip.xiaonuo.common.util; package vip.xiaonuo.common.util;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import vip.xiaonuo.common.exception.CommonException; import vip.xiaonuo.common.exception.CommonException;
@ -27,6 +28,7 @@ import javax.servlet.http.HttpServletResponse;
* @author xuyuxiang * @author xuyuxiang
* @date 2020/3/30 15:09 * @date 2020/3/30 15:09
*/ */
@Slf4j
public class CommonServletUtil { public class CommonServletUtil {
/** /**
@ -66,7 +68,7 @@ public class CommonServletUtil {
try { try {
servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 非Web上下文无法获取Request", e);
throw new CommonException("非Web上下文无法获取Request"); throw new CommonException("非Web上下文无法获取Request");
} }
if (servletRequestAttributes == null) { if (servletRequestAttributes == null) {
@ -81,7 +83,7 @@ public class CommonServletUtil {
try { try {
servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 非Web上下文无法获取Response", e);
throw new CommonException("非Web上下文无法获取Response"); throw new CommonException("非Web上下文无法获取Response");
} }
if (servletRequestAttributes == null) { if (servletRequestAttributes == null) {

View File

@ -58,11 +58,10 @@ public class AuthExceptionUtil {
commonResult = CommonResult.error(saTokenException.getMessage()); commonResult = CommonResult.error(saTokenException.getMessage());
} else { } else {
// 未知异常才打印 // 未知异常才打印
e.printStackTrace(); log.error(">>> 服务器未知异常:{},请求地址:{}", e.getMessage(), SaHolder.getRequest().getUrl());
// 未知异常返回服务器异常此处不可能执行进入因为本方法处理的一定是SaToken的异常此处仅为安全性考虑 // 未知异常返回服务器异常此处不可能执行进入因为本方法处理的一定是SaToken的异常此处仅为安全性考虑
commonResult = CommonResult.error("服务器异常"); commonResult = CommonResult.error("服务器异常");
} }
log.error(">>> {},请求地址:{}", commonResult.getMsg(), SaHolder.getRequest().getUrl());
return commonResult; return commonResult;
} }
} }

View File

@ -528,7 +528,7 @@ public class BizUserServiceImpl extends ServiceImpl<BizUserMapper, BizUser> impl
.doWrite(bizUserExportResultList); .doWrite(bizUserExportResultList);
CommonDownloadUtil.download(tempFile, response); CommonDownloadUtil.download(tempFile, response);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 人员导出异常:", e);
CommonResponseUtil.renderError(response, "导出失败"); CommonResponseUtil.renderError(response, "导出失败");
} finally { } finally {
FileUtil.del(tempFile); FileUtil.del(tempFile);
@ -582,7 +582,7 @@ public class BizUserServiceImpl extends ServiceImpl<BizUserMapper, BizUser> impl
// 下载 // 下载
CommonDownloadUtil.download(resultFile, response); CommonDownloadUtil.download(resultFile, response);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 导出人员个人信息异常:", e);
CommonResponseUtil.renderError(response, "导出失败"); CommonResponseUtil.renderError(response, "导出失败");
} finally { } finally {
// 删除临时文件 // 删除临时文件

View File

@ -400,7 +400,7 @@ public class GenBasicServiceImpl extends ServiceImpl<GenBasicMapper, GenBasic> i
// 移动完毕删除临时目录 // 移动完毕删除临时目录
FileUtil.del(tempFolder); FileUtil.del(tempFolder);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 代码生成异常:", e);
throw new CommonException("代码生成异常"); throw new CommonException("代码生成异常");
} }
} }
@ -517,7 +517,7 @@ public class GenBasicServiceImpl extends ServiceImpl<GenBasicMapper, GenBasic> i
}); });
genBasicPreviewResult.setGenBasicCodeBackendResultList(genBasicCodeBackendResultList); genBasicPreviewResult.setGenBasicCodeBackendResultList(genBasicCodeBackendResultList);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 代码生成异常:", e);
throw new CommonException("代码生成异常"); throw new CommonException("代码生成异常");
} }
return genBasicPreviewResult; return genBasicPreviewResult;

View File

@ -530,7 +530,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
sysUser.getId()).set(SysUser::getAvatar, base64)); sysUser.getId()).set(SysUser::getAvatar, base64));
return base64; return base64;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); log.error(">>> 头像修改失败:", e);
throw new CommonException("头像修改失败用户id值为{}", sysUser.getId()); throw new CommonException("头像修改失败用户id值为{}", sysUser.getId());
} }
} }
@ -974,8 +974,8 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
byte[] bytes = IoUtil.readBytes(inputStream); byte[] bytes = IoUtil.readBytes(inputStream);
CommonDownloadUtil.download("SNOWY2.0系统B端用户导入模板.xlsx", bytes, response); CommonDownloadUtil.download("SNOWY2.0系统B端用户导入模板.xlsx", bytes, response);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 下载用户导入模板失败:", e);
CommonResponseUtil.renderError(response, "导出失败"); CommonResponseUtil.renderError(response, "下载用户导入模板失败");
} }
} }
@ -1008,8 +1008,8 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
.set("errorCount", errorCount) .set("errorCount", errorCount)
.set("errorDetail", errorDetail); .set("errorDetail", errorDetail);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 用户导入失败:", e);
throw new CommonException("文件导入失败"); throw new CommonException("用户导入失败");
} }
} }
@ -1134,7 +1134,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
// 返回成功 // 返回成功
return JSONUtil.createObj().set("success", true); return JSONUtil.createObj().set("success", true);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 数据导入异常:", e);
return JSONUtil.createObj().set("success", false).set("index", i + 1).set("msg", "数据导入异常"); return JSONUtil.createObj().set("success", false).set("index", i + 1).set("msg", "数据导入异常");
} }
} }
@ -1265,8 +1265,8 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
.doWrite(sysUserExportResultList); .doWrite(sysUserExportResultList);
CommonDownloadUtil.download(tempFile, response); CommonDownloadUtil.download(tempFile, response);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 用户导出失败:", e);
CommonResponseUtil.renderError(response, "导出失败"); CommonResponseUtil.renderError(response, "用户导出失败");
} finally { } finally {
FileUtil.del(tempFile); FileUtil.del(tempFile);
} }
@ -1319,7 +1319,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
// 下载 // 下载
CommonDownloadUtil.download(resultFile, response); CommonDownloadUtil.download(resultFile, response);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error(">>> 导出用户个人信息失败:", e);
CommonResponseUtil.renderError(response, "导出失败"); CommonResponseUtil.renderError(response, "导出失败");
} finally { } finally {
// 删除临时文件 // 删除临时文件

View File

@ -54,9 +54,9 @@ public class GlobalErrorViewController {
commonResult = CommonResult.error(); commonResult = CommonResult.error();
} }
Exception exception = (Exception) model; Exception exception = (Exception) model;
exception.printStackTrace(); log.error(">>> 服务器未知异常:", exception);
} }
log.error(">>> {}", commonResult.getMsg());
return commonResult; return commonResult;
} }
} }

View File

@ -69,17 +69,16 @@ public class GlobalExceptionUtil {
commonResult = CommonResult.get(HttpStatus.HTTP_BAD_METHOD, "请求方法仅支持GET或POST", null); commonResult = CommonResult.get(HttpStatus.HTTP_BAD_METHOD, "请求方法仅支持GET或POST", null);
} }
} else if (e instanceof HttpMessageNotReadableException) { } else if (e instanceof HttpMessageNotReadableException) {
log.error(">>> 参数传递格式异常:", e);
// 如果是参数传递格式不支持异常 415 // 如果是参数传递格式不支持异常 415
if (e.getMessage().contains("JSON parse error")) { if (e.getMessage().contains("JSON parse error")) {
e.printStackTrace();
//JSON格式转换错误特殊提示 //JSON格式转换错误特殊提示
commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, "参数格式错误", null); commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, "参数格式错误", null);
} else { } else {
commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, "请使用JSON方式传参", null); commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, "请使用JSON方式传参", null);
} }
} else if (e instanceof HttpMediaTypeNotSupportedException) { } else if (e instanceof HttpMediaTypeNotSupportedException) {
e.printStackTrace(); log.error(">>> 参数传递格式异常:", e);
// 如果是JSON参数格式错误异常 415 // 如果是JSON参数格式错误异常 415
commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, "参数格式错误", null); commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, "参数格式错误", null);
} else if (e instanceof MethodArgumentNotValidException) { } else if (e instanceof MethodArgumentNotValidException) {
@ -104,7 +103,7 @@ public class GlobalExceptionUtil {
commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, missingServletRequestParameterException.getMessage(), null); commonResult = CommonResult.get(HttpStatus.HTTP_UNSUPPORTED_TYPE, missingServletRequestParameterException.getMessage(), null);
} }
else if (e instanceof MultipartException) { else if (e instanceof MultipartException) {
log.error(">>> 文件上传参数异常:", e);
//文件上传错误特殊提示 //文件上传错误特殊提示
commonResult = CommonResult.error("请使用multipart/form-data方式上传文件"); commonResult = CommonResult.error("请使用multipart/form-data方式上传文件");
} else if (e instanceof MissingServletRequestPartException) { } else if (e instanceof MissingServletRequestPartException) {
@ -125,11 +124,11 @@ public class GlobalExceptionUtil {
CommonException commonException = (CommonException) secondCause; CommonException commonException = (CommonException) secondCause;
commonResult = CommonResult.get(commonException.getCode(), commonException.getMsg(), null); commonResult = CommonResult.get(commonException.getCode(), commonException.getMsg(), null);
} else { } else {
e.printStackTrace(); log.error(">>> 数据操作异常:", e);
commonResult = CommonResult.error("数据操作异常"); commonResult = CommonResult.error("数据操作异常");
} }
} else { } else {
e.printStackTrace(); log.error(">>> 数据操作异常:", e);
commonResult = CommonResult.error("数据操作异常"); commonResult = CommonResult.error("数据操作异常");
} }
} else if (e instanceof CommonException) { } else if (e instanceof CommonException) {
@ -139,12 +138,10 @@ public class GlobalExceptionUtil {
commonResult = CommonResult.get(commonException.getCode(), commonException.getMsg(), null); commonResult = CommonResult.get(commonException.getCode(), commonException.getMsg(), null);
} else { } else {
// 未知异常打印详情 // 未知异常打印详情
e.printStackTrace(); log.error(">>> 服务器未知异常:{}, 请求地址:{}", e, CommonServletUtil.getRequest().getRequestURL());
// 未知异常返回服务器异常 // 未知异常返回服务器异常
commonResult = CommonResult.error("服务器异常"); commonResult = CommonResult.error("服务器异常");
} }
log.error(">>> {},请求地址:{}", commonResult.getMsg(), CommonServletUtil.getRequest().getRequestURL());
return commonResult; return commonResult;
} }