package com.webflux.launchadmin.global; import com.alibaba.fastjson.JSON; import com.plumelog.core.util.LogExceptionStackTrace; import com.webflux.launchcommon.returnObj.RW; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataAccessException; import org.springframework.http.HttpStatus; import org.springframework.util.MultiValueMap; import org.springframework.validation.BindException; import org.springframework.web.bind.MethodArgumentNotValidException; 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 org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.server.MissingRequestValueException; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import javax.security.auth.login.AccountExpiredException; import java.nio.file.AccessDeniedException; import java.util.HashMap; import java.util.Map; import java.util.Objects; /** * @author moka */ @RestControllerAdvice @Slf4j public class GlobalExceptionHandler { @ExceptionHandler(BaseException.class) public Mono> handleException(ServerWebExchange exchange, BaseException e) { log.warn("接口异常 uri={} e={}", exchange.getRequest().getPath().value(), LogExceptionStackTrace.erroStackTrace(e)); return RW.fail("500", e.getMessage(), Mono.just(e.getMessage())); } /** * 文件上传异常 */ @ExceptionHandler(MaxUploadSizeExceededException.class) public Mono> baseException(MaxUploadSizeExceededException e) { return RW.fail("文件大小超出限制,请修改后重新上传。", Mono.just(e.getMessage())); } @ExceptionHandler(AccessDeniedException.class) public Mono> handleAuthorizationException(ServerWebExchange exchange, AccessDeniedException e) { log.warn("接口异常 uri = {} e = {}", exchange.getRequest().getPath().value(), LogExceptionStackTrace.erroStackTrace(e)); return RW.fail(HttpStatus.FORBIDDEN.toString(), "没有权限,请联系管理员授权", Mono.just(e.getMessage())); } //@ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MissingRequestValueException.class) public Mono> handleMissingRequestValueException(ServerWebExchange exchange, MissingRequestValueException e) { log.warn("接口异常 uri = {} e = {}", exchange.getRequest().getPath().value(), LogExceptionStackTrace.erroStackTrace(e)); return RW.fail(HttpStatus.FORBIDDEN.toString(), "请求参数异常", Mono.just(e.getMessage())); } @ExceptionHandler(AccountExpiredException.class) public Mono> handleAccountExpiredException(ServerWebExchange exchange, AccountExpiredException e) { log.warn("接口异常 uri = {} e = {}", exchange.getRequest().getPath().value(), LogExceptionStackTrace.erroStackTrace(e)); return RW.fail(e.getMessage(), Mono.just(e.getMessage())); } @ExceptionHandler(DataAccessException.class) public Mono> handleDataAccessException(ServerWebExchange exchange, DataAccessException e) { log.error("接口异常 uri = {} e = {}", exchange.getRequest().getPath().value(), LogExceptionStackTrace.erroStackTrace(e)); return RW.fail(e.getMessage(), Mono.just(e.getMessage())); } @ExceptionHandler(Exception.class) public Mono> handleException(ServerWebExchange exchange, Exception e) { if (e instanceof BaseException) { try { Map logger = new HashMap<>(); MultiValueMap queryParams = exchange.getRequest().getQueryParams(); logger.put("param", queryParams); String paramStr = JSON.toJSONString(logger); log.info("裂变项目api请求记录:{}:{}", exchange.getRequest().getPath().value(), paramStr); } catch (Exception e1) { log.warn("裂变项目api请求记录异常:{}", LogExceptionStackTrace.erroStackTrace(e1)); } log.warn("接口异常 uri= {} e= {}", exchange.getRequest().getPath().value(), LogExceptionStackTrace.erroStackTrace(e)); } else { log.error("接口异常 uri = {} e = {}", exchange.getRequest().getPath().value(), LogExceptionStackTrace.erroStackTrace(e)); } return RW.fail(e.getMessage(), Mono.just(e.getMessage())); } /** * 自定义验证异常 */ @ExceptionHandler(BindException.class) public Mono> validatedBindException(ServerWebExchange exchange, BindException e) { log.warn("接口异常 uri={} e={}", exchange.getRequest().getPath().value(), LogExceptionStackTrace.erroStackTrace(e)); String message = e.getAllErrors().getFirst().getDefaultMessage(); assert message != null; return RW.fail(message, Mono.just(message)); } /** * 自定义验证异常 */ @ExceptionHandler(MethodArgumentNotValidException.class) public Mono> validExceptionHandler(ServerWebExchange exchange, MethodArgumentNotValidException e) { log.warn("接口异常 uri={} e={}", exchange.getRequest().getPath().value(), LogExceptionStackTrace.erroStackTrace(e)); String message = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage(); assert message != null; return RW.fail(message, Mono.just(message)); } }