diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/GlobalErrorController.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/GlobalErrorController.java index 0f3a1b9f1f70762bed114bd4500034111623c184..3b248158079fa725ee437b5a2f236054a6e2fc66 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/GlobalErrorController.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/GlobalErrorController.java @@ -1,28 +1,34 @@ -package com.ruoyi.common.exception; - -import org.springframework.boot.web.servlet.error.ErrorController; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; - -import com.ruoyi.common.core.domain.R; - -@RestController -public class GlobalErrorController implements ErrorController -{ - private static final String ERROR_PATH = "/error"; - - @RequestMapping(value = ERROR_PATH) - @ResponseStatus(value = HttpStatus.NOT_FOUND) - public R handleError() - { - return R.error(404, "request resource not found."); - } - - @Override - public String getErrorPath() - { - return ERROR_PATH; - } +package com.ruoyi.common.exception; + +import com.ruoyi.common.core.domain.R; +import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController; +import org.springframework.boot.web.servlet.error.ErrorAttributes; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + +/** + * @author zmr + * @author lucas + */ +@RestController +public class GlobalErrorController extends AbstractErrorController { + private static final String ERROR_PATH = "/error"; + + public GlobalErrorController(ErrorAttributes errorAttributes) { + super(errorAttributes); + } + + @RequestMapping(value = ERROR_PATH) + public R error(HttpServletRequest request) { + HttpStatus status = getStatus(request); + return R.error(status.value(), status.getReasonPhrase()); + } + + @Override + public String getErrorPath() { + return ERROR_PATH; + } } \ No newline at end of file diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/GlobalExceptionHandler.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/GlobalExceptionHandler.java index b0464f8ceff04fc213f88e725f0d773a645d6311..faaf2fa24b79c9a88ff194773ca1c14fb5793826 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/GlobalExceptionHandler.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/GlobalExceptionHandler.java @@ -1,80 +1,82 @@ -package com.ruoyi.common.exception; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.dao.DuplicateKeyException; -import org.springframework.http.HttpStatus; -import org.springframework.web.HttpRequestMethodNotSupportedException; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestControllerAdvice; - -import com.ruoyi.common.core.domain.R; - -/** - * 异常处理器 - */ -@RestControllerAdvice -public class GlobalExceptionHandler -{ - private Logger logger = LoggerFactory.getLogger(getClass()); - - /** - * 请求方式不支持 - */ - @ExceptionHandler({HttpRequestMethodNotSupportedException.class}) - @ResponseStatus(code=HttpStatus.METHOD_NOT_ALLOWED) - public R handleException(HttpRequestMethodNotSupportedException e) - { - logger.error(e.getMessage(), e); - return R.error("不支持' " + e.getMethod() + "'请求"); - } - - /** - * 拦截未知的运行时异常 - */ - @ExceptionHandler(RuntimeException.class) - public R notFount(RuntimeException e) - { - logger.error("运行时异常:", e); - return R.error("运行时异常:" + e.getMessage()); - } - - /** - * 处理自定义异常 - */ - @ExceptionHandler(RuoyiException.class) - public R handleWindException(RuoyiException e) - { - return R.error(e.getCode(), e.getMessage()); - } - - @ExceptionHandler(DuplicateKeyException.class) - public R handleDuplicateKeyException(DuplicateKeyException e) - { - logger.error(e.getMessage(), e); - return R.error("数据库中已存在该记录"); - } - - @ExceptionHandler(Exception.class) - public R handleException(Exception e) - { - logger.error(e.getMessage(), e); - return R.error("服务器错误,请联系管理员"); - } - - // 捕捉UnauthorizedException - @ExceptionHandler(UnauthorizedException.class) - @ResponseStatus(code=HttpStatus.UNAUTHORIZED) - public R handle401(UnauthorizedException e) - { - return R.error(401, e.getMessage()); - } - - // 验证码错误 - @ExceptionHandler(ValidateCodeException.class) - public R handleCaptcha(ValidateCodeException e) - { - return R.error(e.getMessage()); - } +package com.ruoyi.common.exception; + +import com.ruoyi.common.core.domain.R; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.http.HttpStatus; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * 异常处理器 + * @author zmr + * @author lucas + */ +@RestControllerAdvice +public class GlobalExceptionHandler { + private Logger logger = LoggerFactory.getLogger(getClass()); + + /** + * 请求方式不支持 + */ + @ExceptionHandler({HttpRequestMethodNotSupportedException.class}) + @ResponseStatus(code = HttpStatus.METHOD_NOT_ALLOWED) + public R handleException(HttpRequestMethodNotSupportedException e) { + logger.error(e.getMessage(), e); + return R.error("不支持' " + e.getMethod() + "'请求"); + } + + /** + * 拦截未知的运行时异常 + */ + @ExceptionHandler(RuntimeException.class) + public R notFount(RuntimeException e) { + if (AnnotationUtils.findAnnotation + (e.getClass(), ResponseStatus.class) != null) { + throw e; + } + logger.error("运行时异常:", e); + return R.error("运行时异常:" + e.getMessage()); + } + + /** + * 处理自定义异常 + */ + @ExceptionHandler(RuoyiException.class) + public R handleWindException(RuoyiException e) { + return R.error(e.getCode(), e.getMessage()); + } + + @ExceptionHandler(DuplicateKeyException.class) + public R handleDuplicateKeyException(DuplicateKeyException e) { + logger.error(e.getMessage(), e); + return R.error("数据库中已存在该记录"); + } + + @ExceptionHandler(Exception.class) + public R handleException(Exception e) throws Exception { + logger.error(e.getMessage(), e); + return R.error("服务器错误,请联系管理员"); + } + +// /** +// * 捕获并处理未授权异常 +// * +// * @param e 未授权异常 +// * @return 统一封装的结果类, 含有代码code和提示信息msg +// */ +// @ExceptionHandler(UnauthorizedException.class) +// public R handle401(UnauthorizedException e) { +// return R.error(401, e.getMessage()); +// } + + // 验证码错误 + @ExceptionHandler(ValidateCodeException.class) + public R handleCaptcha(ValidateCodeException e) { + return R.error(e.getMessage()); + } } \ No newline at end of file diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/RuoyiException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/RuoyiException.java index 8aa43eb9dd1e5990d4766340a9777a303492db01..83f624a9a2c85dc525674a29b22976930321955b 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/RuoyiException.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/RuoyiException.java @@ -1,57 +1,53 @@ -package com.ruoyi.common.exception; - -public class RuoyiException extends RuntimeException -{ - // - private static final long serialVersionUID = 3640068073161175965L; - - private String msg; - - private int code = 500; - - public RuoyiException(String msg) - { - super(msg); - this.msg = msg; - } - - public RuoyiException(String msg, Throwable e) - { - super(msg, e); - this.msg = msg; - } - - public RuoyiException(String msg, int code) - { - super(msg); - this.msg = msg; - this.code = code; - } - - public RuoyiException(String msg, int code, Throwable e) - { - super(msg, e); - this.msg = msg; - this.code = code; - } - - public String getMsg() - { - return msg; - } - - public void setMsg(String msg) - { - this.msg = msg; - } - - public int getCode() - { - return code; - } - - public void setCode(int code) - { - this.code = code; - } +package com.ruoyi.common.exception; + +/** + * ruoyi异常类 + * @author zmr + * @author lucas + */ +public class RuoyiException extends RuntimeException { + // + private static final long serialVersionUID = 3640068073161175965L; + + private String msg; + + private int code = 500; + + public RuoyiException(String msg) { + super(msg); + this.msg = msg; + } + + public RuoyiException(String msg, Throwable e) { + super(msg, e); + this.msg = msg; + } + + public RuoyiException(String msg, int code) { + super(msg); + this.msg = msg; + this.code = code; + } + + public RuoyiException(String msg, int code, Throwable e) { + super(msg, e); + this.msg = msg; + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } } \ No newline at end of file diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/UnauthorizedException.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/UnauthorizedException.java index 80e426bb0a415d9b7f5368d6e809fc99da3bd126..d34a254957b2b4ea104c270bb718da8b2e60e3a2 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/UnauthorizedException.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/exception/UnauthorizedException.java @@ -1,18 +1,23 @@ -package com.ruoyi.common.exception; -public class UnauthorizedException extends RuntimeException -{ - private static final String DEFAULT_MSG = "unauthorized"; - - // - private static final long serialVersionUID = 3885400551304383736L; - - public UnauthorizedException(String msg) - { - super(msg); - } - - public UnauthorizedException() - { - super(DEFAULT_MSG); - } +package com.ruoyi.common.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * @author zmr + * @author lucas + */ +@ResponseStatus(code = HttpStatus.UNAUTHORIZED, reason = "Unauthorized") +public class UnauthorizedException extends RuntimeException { + private static final String DEFAULT_MSG = "unauthorized"; + + private static final long serialVersionUID = 3885400551304383736L; + + public UnauthorizedException(String msg) { + super(msg); + } + + public UnauthorizedException() { + super(DEFAULT_MSG); + } } \ No newline at end of file