com.midcu.common.web.GlobalExceptionController Maven / Gradle / Ivy
The newest version!
package com.midcu.common.web;
import cn.dev33.satoken.exception.DisableServiceException;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.midcu.common.res.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.resource.NoResourceFoundException;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionController {
@ExceptionHandler(Throwable.class)
public JsonResult handleThrowable(Throwable e) {
log.error("请求发生错误:", e);
return JsonResult.Status("请求发生错误!", 500, false);
}
@ExceptionHandler(NoResourceFoundException.class)
public JsonResult handleNoResourceFoundException(NoResourceFoundException e) {
return JsonResult.Status("404!", 404, false);
}
@ExceptionHandler(NotLoginException.class)
public JsonResult handleNotLoginException(NotLoginException e) {
return JsonResult.Status(e.getMessage(), 410, false);
}
@ExceptionHandler(DisableServiceException.class)
public JsonResult handleDisableLoginException(DisableServiceException e) {
return JsonResult.Bad("账号被封禁:" + e.getDisableTime() + "秒后解封");
}
@ExceptionHandler(NotRoleException.class)
public JsonResult handleNotRoleException(NotRoleException e) {
return JsonResult.Bad("没有访问权限!");
}
@ExceptionHandler(NotPermissionException.class)
public JsonResult handleNotPermissionException(NotPermissionException e) {
return JsonResult.Bad("没有访问权限!");
}
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public JsonResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
return JsonResult.Bad("请求参数不正确!");
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public JsonResult handleHttpRequestMethodNotSupportedException() {
return JsonResult.Bad("请求方法不支持!");
}
@ExceptionHandler(BindException.class)
public JsonResult handleBindException(BindException e) {
StringBuilder msg = new StringBuilder();
for (ObjectError error : e.getBindingResult().getAllErrors()) {
String message = error.getDefaultMessage();
if (StringUtils.hasText(message) && message.startsWith("Failed to convert value")) {
msg.append("请求参数类型有误!");
} else {
msg.append(message);
}
}
return JsonResult.Bad(msg.toString());
}
@ExceptionHandler(IllegalArgumentException.class)
public JsonResult handleIllegalArgumentException(IllegalArgumentException e) {
return JsonResult.Bad(e.getMessage());
}
@ExceptionHandler(IllegalStateException.class)
public JsonResult handleIllegalStateException(IllegalStateException e) {
return JsonResult.Bad(e.getMessage());
}
@ExceptionHandler(MaxUploadSizeExceededException.class)
public JsonResult handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
return JsonResult.Bad("文件大小超出限制!");
}
}