我们发布啦

This commit is contained in:
张乐
2020-08-13 16:12:57 +08:00
parent a3e1c38d27
commit c0cec49f41
1885 changed files with 376936 additions and 2 deletions

View 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;
}
}

View 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);
}
}

View 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;
}
}

View File

@@ -0,0 +1,8 @@
package com.exception;
public interface ExceptionHandler {
long getCode();
String getMessage();
}

View File

@@ -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());
}
}