| | |
| | | package com.panzhihua.applets.exception; |
| | | |
| | | import com.panzhihua.common.constants.HttpStatus; |
| | | import com.panzhihua.common.exceptions.TokenException; |
| | | import com.panzhihua.common.exceptions.UnAuthenticationException; |
| | | import com.panzhihua.common.exceptions.UnAuthorizationException; |
| | | import com.panzhihua.common.model.vos.R; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.validation.BindingResult; |
| | | import org.springframework.validation.FieldError; |
| | | import org.springframework.validation.ObjectError; |
| | | import org.springframework.web.bind.MethodArgumentNotValidException; |
| | | import org.springframework.web.bind.annotation.ExceptionHandler; |
| | | import org.springframework.web.bind.annotation.RestControllerAdvice; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @program: springcloud_k8s_panzhihuazhihuishequ |
| | |
| | | * @author: huang.hongfa weixin hhf9596 qq 959656820 |
| | | * @create: 2020-11-25 14:36 |
| | | **/ |
| | | @Slf4j |
| | | @RestControllerAdvice |
| | | public class GlobalExceptionCapture { |
| | | /** |
| | |
| | | */ |
| | | @ExceptionHandler(value = Exception.class) |
| | | public R errorHandler(Exception ex) { |
| | | log.error("捕捉到全局异常【{}】",ex.getMessage()); |
| | | return R.fail(); |
| | | } |
| | | |
| | | /** |
| | | * 校验异常 |
| | | * @param ex valid |
| | | * @return 返回json |
| | | */ |
| | | @ExceptionHandler(value = MethodArgumentNotValidException.class) |
| | | public R methodArgumentNotValidException(MethodArgumentNotValidException ex) { |
| | | BindingResult result = ex.getBindingResult(); |
| | | StringBuilder errorMessage = new StringBuilder(); |
| | | if (result.hasErrors()) { |
| | | List<ObjectError> errors = result.getAllErrors(); |
| | | errors.forEach(p ->{ |
| | | FieldError fieldError = (FieldError) p; |
| | | errorMessage.append(fieldError.getDefaultMessage()); |
| | | errorMessage.append(" "); |
| | | }); |
| | | } |
| | | return R.fail(HttpStatus.BAD_REQUEST,errorMessage.toString()); |
| | | } |
| | | } |