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

templates.gateway.GlobalExceptionHandler.ftl Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package ${packageName}.gateway.handler;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.handler.ResponseStatusExceptionHandler;
import org.yes.tools.core.exception.ExceptionCodeEnum;
import org.yes.tools.core.exception.YesBaseException;
import org.yes.tools.core.result.CommonResult;
import reactor.core.publisher.Mono;

/***
 * 网关异常通用处理器,只作用在webflux 环境下 , 优先级低于 {@link ResponseStatusExceptionHandler} 执行
 *
 * @author Co.
 * @date: 2022/10/15 11:19
 * @return
 */
@Slf4j
@Order(-1)
@RequiredArgsConstructor
public class GlobalExceptionHandler implements ErrorWebExceptionHandler {

    private final ObjectMapper objectMapper;

    @Override
    public Mono handle(ServerWebExchange exchange, Throwable ex) {
        ServerHttpResponse response = exchange.getResponse();

        if (response.isCommitted()) {
            return Mono.error(ex);
        }

        // header set
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        if (ex instanceof ResponseStatusException) {
            response.setStatusCode(((ResponseStatusException) ex).getStatusCode());
        }

        return response.writeWith(Mono.fromSupplier(() -> {
            DataBufferFactory bufferFactory = response.bufferFactory();
            try {
                if (ex instanceof NotFoundException) {
                    return bufferFactory.wrap(objectMapper.writeValueAsBytes(JSON.toJSON(CommonResult.failed(ExceptionCodeEnum.NOT_FOUND))));
                }
                if (ex instanceof YesBaseException) {
                    return bufferFactory.wrap(objectMapper.writeValueAsBytes(JSON.toJSON(CommonResult.failed(Integer.parseInt(((YesBaseException) ex).getCode()), ex.getMessage()))));
                }
                return bufferFactory.wrap(objectMapper.writeValueAsBytes(JSON.toJSON(CommonResult.failed(ex.getMessage()))));
            } catch (JsonProcessingException e) {
                log.error("Error writing response", ex);
                return bufferFactory.wrap(new byte[0]);
            }
        }));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy