package com.zy.common.config;
|
|
import com.core.common.R;
|
import com.core.exception.CoolException;
|
import lombok.Data;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.FieldError;
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
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 org.springframework.web.method.HandlerMethod;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
/**
|
* Created by vincent on 2019-06-09
|
*/
|
@RestControllerAdvice
|
public class CoolExceptionHandler {
|
|
@ExceptionHandler(Exception.class)
|
public R handlerException(HandlerMethod handler, Exception e) {
|
e.printStackTrace();
|
return R.error();
|
}
|
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
public R handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
|
return R.error();
|
}
|
|
@ExceptionHandler(CoolException.class)
|
public R handleRRException(CoolException e) {
|
String[] split = e.getMessage().split("-");
|
if (split.length == 2) {
|
if (String.valueOf(split[0]).length() < 3) {
|
return R.parse(e.getMessage());
|
}
|
}
|
return R.error(e.getMessage());
|
}
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
public R handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
|
// 返回错误列表
|
BindingResult bindingResult = ex.getBindingResult();
|
final String[] field = {""};
|
|
bindingResult.getFieldErrors().forEach(fieldError -> {
|
String message = fieldError.getDefaultMessage();
|
field[0] = field[0] + message+";";
|
});
|
|
// 参数为空
|
return new R(401, field[0]);
|
}
|
|
}
|