我们发布啦
This commit is contained in:
27
crmeb/src/main/java/com/exception/ApiException.java
Normal file
27
crmeb/src/main/java/com/exception/ApiException.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.exception;
|
||||
|
||||
public class ApiException extends RuntimeException{
|
||||
|
||||
private ExceptionHandler exceptionHandler;
|
||||
|
||||
public ApiException(ExceptionHandler exceptionHandler) {
|
||||
super(exceptionHandler.getMessage());
|
||||
this.exceptionHandler = exceptionHandler;
|
||||
}
|
||||
|
||||
public ApiException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ApiException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public ApiException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public ExceptionHandler getErrorCode() {
|
||||
return exceptionHandler;
|
||||
}
|
||||
}
|
||||
26
crmeb/src/main/java/com/exception/CrmebException.java
Normal file
26
crmeb/src/main/java/com/exception/CrmebException.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.exception;
|
||||
|
||||
import com.common.CommonResult;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class CrmebException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CrmebException() {}
|
||||
|
||||
public CrmebException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
32
crmeb/src/main/java/com/exception/ExceptionCodeEnum.java
Normal file
32
crmeb/src/main/java/com/exception/ExceptionCodeEnum.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package com.exception;
|
||||
|
||||
public enum ExceptionCodeEnum implements ExceptionHandler{
|
||||
// 数据操作错误定义
|
||||
SUCCESS(200, "操作成功"),
|
||||
FAILED(500, "操作失败"),
|
||||
PRAM_NOT_MATCH(400, "参数不正确"),
|
||||
VALIDATE_FAILED(400, "参数检验失败"),
|
||||
UNAUTHORIZED(401, "未登录或token过期,请登录!"),
|
||||
FORBIDDEN(403, "没有相关权限"),
|
||||
NOT_FOUND(404, "没有找到相关数据"),
|
||||
ERROR(500, "系统异常"),
|
||||
;
|
||||
|
||||
private long code;
|
||||
private String message;
|
||||
|
||||
private ExceptionCodeEnum(long code, String message){
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
8
crmeb/src/main/java/com/exception/ExceptionHandler.java
Normal file
8
crmeb/src/main/java/com/exception/ExceptionHandler.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.exception;
|
||||
|
||||
public interface ExceptionHandler {
|
||||
|
||||
long getCode();
|
||||
|
||||
String getMessage();
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.exception;
|
||||
|
||||
import com.common.CommonResult;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Objects;
|
||||
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
// private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
|
||||
/**
|
||||
* 拦截表单参数校验
|
||||
*/
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
@ExceptionHandler({BindException.class})
|
||||
public CommonResult bindException(BindException e) {
|
||||
BindingResult bindingResult = e.getBindingResult();
|
||||
return CommonResult.failed(ExceptionCodeEnum.VALIDATE_FAILED, Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截JSON参数校验
|
||||
*/
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public CommonResult bindException(MethodArgumentNotValidException e) {
|
||||
BindingResult bindingResult = e.getBindingResult();
|
||||
return CommonResult.failed(ExceptionCodeEnum.VALIDATE_FAILED,Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截参数类型不正确
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
|
||||
public CommonResult bindException(HttpMediaTypeNotSupportedException e){
|
||||
return CommonResult.failed(ExceptionCodeEnum.PRAM_NOT_MATCH,Objects.requireNonNull(e.getMessage()));
|
||||
}
|
||||
|
||||
|
||||
//声明要捕获的异常
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseBody
|
||||
public <T> CommonResult<?> defaultExceptionHandler(Exception e) {
|
||||
e.printStackTrace();
|
||||
if(e instanceof CrmebException) {
|
||||
return CommonResult.failed(ExceptionCodeEnum.FAILED,Objects.requireNonNull(e.getMessage()));
|
||||
}
|
||||
if(e instanceof MissingServletRequestParameterException){
|
||||
return CommonResult.failed(ExceptionCodeEnum.PRAM_NOT_MATCH, Objects.requireNonNull(e.getMessage()));
|
||||
}
|
||||
//未知错误
|
||||
return CommonResult.failed(ExceptionCodeEnum.ERROR,e.getMessage());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user