| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 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<RW<Object>> 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<RW<Object>> baseException(MaxUploadSizeExceededException e) {
- return RW.fail("文件大小超出限制,请修改后重新上传。", Mono.just(e.getMessage()));
- }
- @ExceptionHandler(AccessDeniedException.class)
- public Mono<RW<Object>> 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<RW<Object>> 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<RW<Object>> 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<RW<Object>> 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<RW<Object>> handleException(ServerWebExchange exchange, Exception e) {
- if (e instanceof BaseException) {
- try {
- Map<String, Object> logger = new HashMap<>();
- MultiValueMap<String, String> 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<RW<Object>> 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<RW<Object>> 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));
- }
- }
|