|
|
|
@ -7,14 +7,17 @@ import com.huoran.iasf.common.utils.R; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.apache.shiro.authc.AuthenticationException; |
|
|
|
|
import org.apache.shiro.authz.AuthorizationException; |
|
|
|
|
import org.springframework.http.HttpStatus; |
|
|
|
|
import org.springframework.validation.BindingResult; |
|
|
|
|
import org.springframework.web.bind.MethodArgumentNotValidException; |
|
|
|
|
import org.springframework.web.bind.annotation.ControllerAdvice; |
|
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler; |
|
|
|
|
import org.springframework.web.bind.annotation.ResponseStatus; |
|
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice; |
|
|
|
|
|
|
|
|
|
import javax.validation.ConstraintViolation; |
|
|
|
|
import javax.validation.ConstraintViolationException; |
|
|
|
|
import javax.validation.ValidationException; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.Set; |
|
|
|
|
import java.util.concurrent.atomic.AtomicReference; |
|
|
|
@ -34,12 +37,21 @@ public class RestExceptionHandler { |
|
|
|
|
* 系统繁忙,请稍候再试" |
|
|
|
|
*/ |
|
|
|
|
@ExceptionHandler(Exception.class) |
|
|
|
|
@ResponseStatus(HttpStatus.NOT_FOUND) |
|
|
|
|
public R handleException(Exception e) { |
|
|
|
|
log.error("Exception,exception:{}", e, e); |
|
|
|
|
return R.getResult(BaseResponseCode.SYSTEM_BUSY); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ExceptionHandler(IllegalArgumentException.class) |
|
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST) |
|
|
|
|
public R illegalArgumentException(IllegalArgumentException e) { |
|
|
|
|
log.error("Exception,exception:{}", e, e); |
|
|
|
|
return R.getResult(BaseResponseCode.METHOD_ARGUMENT_NOT_VALID_EXCEPTION); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ExceptionHandler(AuthenticationException.class) |
|
|
|
|
@ResponseStatus(HttpStatus.UNAUTHORIZED) |
|
|
|
|
public R authenticationException(AuthenticationException e) { |
|
|
|
|
log.error("Exception,exception:{}", e, e); |
|
|
|
|
// throw new BusinessException(BaseResponseCode.TOKEN_ERROR);
|
|
|
|
@ -50,6 +62,7 @@ public class RestExceptionHandler { |
|
|
|
|
* 自定义全局异常处理 |
|
|
|
|
*/ |
|
|
|
|
@ExceptionHandler(value = BusinessException.class) |
|
|
|
|
@ResponseStatus(HttpStatus.UNAUTHORIZED) |
|
|
|
|
public R businessExceptionHandler(BusinessException e) { |
|
|
|
|
log.error("Exception,exception:{}", e, e); |
|
|
|
|
BaseResponseCode em = e.getBaseResponseCode(); |
|
|
|
@ -60,6 +73,7 @@ public class RestExceptionHandler { |
|
|
|
|
* 没有权限 返回403视图 |
|
|
|
|
*/ |
|
|
|
|
@ExceptionHandler(value = AuthorizationException.class) |
|
|
|
|
@ResponseStatus(HttpStatus.UNAUTHORIZED) |
|
|
|
|
public R errorPermission(AuthorizationException e) { |
|
|
|
|
log.error("Exception,exception:{}", e, e); |
|
|
|
|
return new R(BaseResponseCode.UNAUTHORIZED_ERROR); |
|
|
|
@ -70,6 +84,7 @@ public class RestExceptionHandler { |
|
|
|
|
* 处理参数格式校验异常 |
|
|
|
|
*/ |
|
|
|
|
@ExceptionHandler(value = MethodArgumentNotValidException.class) |
|
|
|
|
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) |
|
|
|
|
public R handleValidException(MethodArgumentNotValidException e){ |
|
|
|
|
log.error("参数格式校验异常"); |
|
|
|
|
BindingResult bindingResult = e.getBindingResult(); |
|
|
|
@ -87,6 +102,7 @@ public class RestExceptionHandler { |
|
|
|
|
* 处理Validated List<entity> 异常 |
|
|
|
|
*/ |
|
|
|
|
@ExceptionHandler |
|
|
|
|
@ResponseStatus(HttpStatus.BAD_REQUEST) |
|
|
|
|
public R handle(ConstraintViolationException exception) { |
|
|
|
|
log.error("methodArgumentNotValidExceptionHandler bindingResult.allErrors():{},exception:{}", exception, exception); |
|
|
|
|
Set<ConstraintViolation<?>> violations = exception.getConstraintViolations(); |
|
|
|
@ -98,4 +114,11 @@ public class RestExceptionHandler { |
|
|
|
|
return R.getResult(BaseResponseCode.METHOD_ARGUMENT_NOT_VALID_EXCEPTION.getCode(), builder.toString()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ExceptionHandler |
|
|
|
|
@ResponseStatus(HttpStatus.NOT_FOUND) |
|
|
|
|
public R handle(ValidationException e) { |
|
|
|
|
log.error("Exception,exception:{}", e, e); |
|
|
|
|
return new R(404, e.getMessage()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|