All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.spring.boxes.gateway.exception.GatewayExceptionHandler Maven / Gradle / Ivy

The newest version!
package com.spring.boxes.gateway.exception;

import com.spring.boxes.dollar.JSONUtils;
import com.spring.boxes.dollar.StringUtils;
import com.spring.boxes.dollar.support.ApiException;
import com.spring.boxes.dollar.support.ApiResult;
import com.spring.boxes.dollar.support.scope.ScopeField;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.support.WebExchangeBindException;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GatewayExceptionHandler implements ErrorWebExceptionHandler {

    @NonNull
    @Override
    public Mono handle(ServerWebExchange exchange, @NonNull Throwable ex) {
        ServerHttpResponse response = exchange.getResponse();
        HttpStatus status = response.getStatusCode();
        if (response.isCommitted()) {
            return Mono.error(ex);
        }
        String language = exchange.getRequest().getHeaders().getFirst(ScopeField.LANGUAGE);
        String originPath = exchange.getRequest().getPath().value();
        log.info("[异常机制] originPath:{} status:{} message:{}", originPath, status, ex.getLocalizedMessage());
        if (ex instanceof WebExchangeBindException) {
            WebExchangeBindException ge = (WebExchangeBindException) ex;
            ApiResult result = ApiResult.failure(ge.getLocalizedMessage());
            response.setStatusCode(HttpStatus.OK);
            response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
            String value = StringUtils.trimToEmpty(JSONUtils.toJSON(result));
            log.info("[异常机制] 应用异常! 结果:{}", value);
            DataBuffer buffer = response.bufferFactory().wrap(value.getBytes(StandardCharsets.UTF_8));
            return response.writeWith(Flux.just(buffer));
        }
        if (ex instanceof ApiException) {
            log.info("[异常机制] 应用异常! originPath:{}", originPath);
            ApiResult result = new ApiResult<>(((ApiException) ex).getStatus().getStatus(), ex.getLocalizedMessage());
            response.setStatusCode(HttpStatus.OK);
            response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
            String value = StringUtils.trimToEmpty(JSONUtils.toJSON(result));
            log.info("[异常机制] 应用异常! 结果:{}", value);
            DataBuffer buffer = response.bufferFactory().wrap(value.getBytes(StandardCharsets.UTF_8));
            return response.writeWith(Flux.just(buffer));
        }
        if (Objects.isNull(status) || status.is5xxServerError()) {
            return Mono.error(ex);
        }
        log.info("[异常机制] [其他异常] originPath:{} status:{} language:{} message:{}", originPath, status, language, ex.getLocalizedMessage());
        return Mono.error(ex);
    }

}