| | |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.http.HttpStatus; |
| | | import jakarta.servlet.ServletException; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import jakarta.validation.ConstraintViolation; |
| | | import jakarta.validation.ConstraintViolationException; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.dromara.common.core.domain.R; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.exception.SseException; |
| | | import org.dromara.common.core.exception.base.BaseException; |
| | | import org.dromara.common.core.utils.StreamUtils; |
| | | import org.dromara.common.json.utils.JsonUtils; |
| | | import org.springframework.context.support.DefaultMessageSourceResolvable; |
| | | import org.springframework.validation.BindException; |
| | | import org.springframework.web.HttpRequestMethodNotSupportedException; |
| | |
| | | log.error(e.getMessage()); |
| | | Integer code = e.getCode(); |
| | | return ObjectUtil.isNotNull(code) ? R.fail(code, e.getMessage()) : R.fail(e.getMessage()); |
| | | } |
| | | |
| | | /** |
| | | * 认证失败 |
| | | */ |
| | | @ResponseStatus(org.springframework.http.HttpStatus.UNAUTHORIZED) |
| | | @ExceptionHandler(SseException.class) |
| | | public String handleNotLoginException(SseException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.debug("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage()); |
| | | return JsonUtils.toJsonString(R.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源")); |
| | | } |
| | | |
| | | /** |
| | | * servlet异常 |
| | | */ |
| | | @ExceptionHandler(ServletException.class) |
| | | public R<Void> handleServletException(ServletException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',发生未知异常.", requestURI, e); |
| | | return R.fail(e.getMessage()); |
| | | } |
| | | |
| | | /** |
| | |
| | | @ExceptionHandler(MethodArgumentNotValidException.class) |
| | | public R<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { |
| | | log.error(e.getMessage()); |
| | | String message = e.getBindingResult().getFieldError().getDefaultMessage(); |
| | | String message = StreamUtils.join(e.getBindingResult().getAllErrors(), DefaultMessageSourceResolvable::getDefaultMessage, ", "); |
| | | return R.fail(message); |
| | | } |
| | | |