mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-11-05 08:57:12 +08:00
优化:优化校验工具类的使用及部分模板文本写法
1.优化校验工具类,支持传入 {} 模板文本
2.校验工具类增加 throwIf 重载方法,适合于 boolean 类型参数的情况
3.优化一些模板文本的写法
4.优化一些其他细节
This commit is contained in:
@@ -64,7 +64,7 @@ public class GlobalExceptionHandler {
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
@ExceptionHandler(Exception.class)
|
||||
public R handleException(Exception e, HttpServletRequest request) {
|
||||
log.error("请求地址'{}',发生未知异常", request.getRequestURI(), e);
|
||||
log.error("请求地址 [{}],发生未知异常。", request.getRequestURI(), e);
|
||||
LogContextHolder.setException(e);
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
@@ -75,7 +75,7 @@ public class GlobalExceptionHandler {
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
@ExceptionHandler(RuntimeException.class)
|
||||
public R handleRuntimeException(RuntimeException e, HttpServletRequest request) {
|
||||
log.error("请求地址'{}',发生系统异常", request.getRequestURI(), e);
|
||||
log.error("请求地址 [{}],发生系统异常。", request.getRequestURI(), e);
|
||||
LogContextHolder.setException(e);
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
@@ -86,7 +86,7 @@ public class GlobalExceptionHandler {
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
@ExceptionHandler(ServiceException.class)
|
||||
public R handleServiceException(ServiceException e, HttpServletRequest request) {
|
||||
log.error("请求地址'{}',发生业务异常", request.getRequestURI(), e);
|
||||
log.error("请求地址 [{}],发生业务异常。", request.getRequestURI(), e);
|
||||
LogContextHolder.setErrorMsg(e.getMessage());
|
||||
return R.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
|
||||
}
|
||||
@@ -97,7 +97,7 @@ public class GlobalExceptionHandler {
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
@ExceptionHandler(BadRequestException.class)
|
||||
public R handleBadRequestException(BadRequestException e, HttpServletRequest request) {
|
||||
log.warn("请求地址'{}',自定义验证失败", request.getRequestURI(), e);
|
||||
log.warn("请求地址 [{}],自定义验证失败。", request.getRequestURI(), e);
|
||||
LogContextHolder.setErrorMsg(e.getMessage());
|
||||
return R.fail(HttpStatus.BAD_REQUEST.value(), e.getMessage());
|
||||
}
|
||||
@@ -108,7 +108,7 @@ public class GlobalExceptionHandler {
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
@ExceptionHandler(BindException.class)
|
||||
public R handleBindException(BindException e, HttpServletRequest request) {
|
||||
log.warn("请求地址'{}',参数验证失败", request.getRequestURI(), e);
|
||||
log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e);
|
||||
String errorMsg = StreamUtils.join(e.getAllErrors(), DefaultMessageSourceResolvable::getDefaultMessage, ",");
|
||||
LogContextHolder.setErrorMsg(errorMsg);
|
||||
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
|
||||
@@ -120,7 +120,7 @@ public class GlobalExceptionHandler {
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
@ExceptionHandler(ConstraintViolationException.class)
|
||||
public R constraintViolationException(ConstraintViolationException e, HttpServletRequest request) {
|
||||
log.warn("请求地址'{}',参数验证失败", request.getRequestURI(), e);
|
||||
log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e);
|
||||
String errorMsg = StreamUtils.join(e.getConstraintViolations(), ConstraintViolation::getMessage, ",");
|
||||
LogContextHolder.setErrorMsg(errorMsg);
|
||||
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
|
||||
@@ -132,7 +132,7 @@ public class GlobalExceptionHandler {
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) {
|
||||
log.warn("请求地址'{}',参数验证失败", request.getRequestURI(), e);
|
||||
log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e);
|
||||
String errorMsg = ExceptionUtils
|
||||
.exToNull(() -> Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage());
|
||||
LogContextHolder.setErrorMsg(errorMsg);
|
||||
@@ -146,11 +146,10 @@ public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
||||
public R handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e,
|
||||
HttpServletRequest request) {
|
||||
String subMsg = StrUtil.format("参数名:'{}',期望参数类型:'{}'", e.getName(), e.getParameter().getParameterType());
|
||||
log.warn("请求地址'{}',参数转换失败。方法:'{}',{}", request.getRequestURI(),
|
||||
Objects.requireNonNull(e.getParameter().getMethod()).getName(), subMsg, e);
|
||||
LogContextHolder.setErrorMsg(subMsg);
|
||||
return R.fail(HttpStatus.BAD_REQUEST.value(), subMsg);
|
||||
String errorMsg = StrUtil.format("参数名:[{}],期望参数类型:[{}]", e.getName(), e.getParameter().getParameterType());
|
||||
log.warn("请求地址 [{}],参数转换失败,{}。", request.getRequestURI(), errorMsg, e);
|
||||
LogContextHolder.setErrorMsg(errorMsg);
|
||||
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,7 +159,7 @@ public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
||||
public R handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
|
||||
LogContextHolder.setErrorMsg(e.getMessage());
|
||||
log.error("请求地址'{}',不支持'{}'请求", request.getRequestURI(), e.getMethod());
|
||||
log.error("请求地址 [{}],不支持 [{}] 请求", request.getRequestURI(), e.getMethod());
|
||||
return R.fail(HttpStatus.METHOD_NOT_ALLOWED.value(), e.getMessage());
|
||||
}
|
||||
|
||||
@@ -170,9 +169,9 @@ public class GlobalExceptionHandler {
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
@ExceptionHandler(MaxUploadSizeExceededException.class)
|
||||
public R handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e, HttpServletRequest request) {
|
||||
log.warn("请求地址'{}',上传文件失败,文件大小超过限制", request.getRequestURI(), e);
|
||||
log.warn("请求地址 [{}],上传文件失败,文件大小超过限制。", request.getRequestURI(), e);
|
||||
String sizeLimit = StrUtil.subBetween(e.getMessage(), "The maximum size ", " for");
|
||||
String errorMsg = String.format("请上传小于 %s MB 的文件", NumberUtil.parseLong(sizeLimit) / 1024 / 1024);
|
||||
String errorMsg = String.format("请上传小于 %sMB 的文件", NumberUtil.parseLong(sizeLimit) / 1024 / 1024);
|
||||
LogContextHolder.setErrorMsg(errorMsg);
|
||||
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
|
||||
}
|
||||
@@ -183,7 +182,7 @@ public class GlobalExceptionHandler {
|
||||
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
||||
@ExceptionHandler(NotLoginException.class)
|
||||
public R handleNotLoginException(NotLoginException e, HttpServletRequest request) {
|
||||
log.error("请求地址'{}',认证失败,无法访问系统资源", request.getRequestURI(), e);
|
||||
log.error("请求地址 [{}],认证失败,无法访问系统资源。", request.getRequestURI(), e);
|
||||
|
||||
String errorMsg;
|
||||
switch (e.getType()) {
|
||||
@@ -208,7 +207,7 @@ public class GlobalExceptionHandler {
|
||||
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||
@ExceptionHandler(NotPermissionException.class)
|
||||
public R handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
|
||||
log.error("请求地址'{}',权限码校验失败'{}'", request.getRequestURI(), e);
|
||||
log.error("请求地址 [{}],权限码校验失败。", request.getRequestURI(), e);
|
||||
return R.fail(HttpStatus.FORBIDDEN.value(), "没有访问权限,请联系管理员授权");
|
||||
}
|
||||
|
||||
@@ -218,7 +217,7 @@ public class GlobalExceptionHandler {
|
||||
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||
@ExceptionHandler(NotRoleException.class)
|
||||
public R handleNotRoleException(NotRoleException e, HttpServletRequest request) {
|
||||
log.error("请求地址'{}',角色权限校验失败'{}'", request.getRequestURI(), e);
|
||||
log.error("请求地址 [{}],角色权限校验失败。", request.getRequestURI(), e);
|
||||
return R.fail(HttpStatus.FORBIDDEN.value(), "没有访问权限,请联系管理员授权");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user