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

pull/98/head
xuyuxiang 2023-03-23 14:13:00 +08:00
parent d1e8ca972f
commit 8f2fa66647
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 com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import vip.xiaonuo.common.util.CommonServletUtil;
import java.util.List;
@ -26,6 +27,7 @@ import java.util.List;
* @author xuyuxiang
* @date 2021/12/18 14:43
*/
@Slf4j
public class CommonPageRequest {
private static final String PAGE_SIZE_PARAM_NAME = "size";
@ -53,7 +55,7 @@ public class CommonPageRequest {
size = PAGE_SIZE_MAX_VALUE;
}
} catch (Exception e) {
e.printStackTrace();
log.error(">>> 分页条数转换异常:", e);
size = 20;
}
}
@ -64,7 +66,7 @@ public class CommonPageRequest {
try {
page = Convert.toInt(pageString);
} catch (Exception e) {
e.printStackTrace();
log.error(">>> 分页页数转换异常:", e);
page = 1;
}
}

View File

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

View File

@ -56,16 +56,16 @@ public class CommonIpAddressUtil {
try {
cBuff = Searcher.loadContentFromFile(dbPath);
} catch (Exception e) {
e.printStackTrace();
throw new CommonException("CommonIpAddressUtil初始化失败,原因:", e.getMessage());
log.error(">>> CommonIpAddressUtil初始化异常", e);
throw new CommonException("CommonIpAddressUtil初始化异常");
}
// 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。
try {
searcher = Searcher.newWithBuffer(cBuff);
} catch (Exception e) {
e.printStackTrace();
throw new CommonException("CommonIpAddressUtil初始化失败,原因:", e.getMessage());
log.error(">>> CommonIpAddressUtil初始化异常", e);
throw new CommonException("CommonIpAddressUtil初始化异常");
}
}
@ -83,7 +83,7 @@ public class CommonIpAddressUtil {
String remoteHost = ServletUtil.getClientIP(request);
return LOCAL_REMOTE_HOST.equals(remoteHost) ? Ipv4Util.LOCAL_IP : remoteHost;
} catch (Exception e) {
e.printStackTrace();
log.error(">>> 获取客户端ip异常", e);
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("DOWN", downSpeed + (downSpeed.endsWith("B")?"/S":"B/S"));
} catch (Exception e) {
log.info(">>> 网络测速失败,原因:");
e.printStackTrace();
log.info(">>> 网络测速失败:", e);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
log.info(">>> 网络测速失败:", e);
}
}
Optional.ofNullable(pro).ifPresent(Process::destroy);
@ -119,7 +118,7 @@ public class CommonNetWorkInfoUtil {
arr[1] = tx;
}
} catch (Exception e) {
e.printStackTrace();
log.error(">>> 网络测速异常:", e);
}
return arr;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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