package jnpf.handler; import jnpf.util.JsonUtil; import jnpf.base.ActionResult; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; import org.springframework.cloud.gateway.support.NotFoundException; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.http.HttpStatus; 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 reactor.core.publisher.Mono; /** * 网关统一异常处理 * * @author JNPF开发平台组 * @version V3.1.0 * @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com) * @date 2021-03-24 */ @Slf4j @Order(-1) @Configuration public class GatewayHandler implements ErrorWebExceptionHandler { @Override public Mono handle(ServerWebExchange exchange, Throwable ex) { ServerHttpResponse response = exchange.getResponse(); if (exchange.getResponse().isCommitted()) { return Mono.error(ex); } String msg; if (ex instanceof NotFoundException) { msg = "服务未找到"; } else if (ex instanceof ResponseStatusException) { ResponseStatusException responseStatusException = (ResponseStatusException) ex; msg = responseStatusException.getMessage(); } else { msg = "内部服务器错误"; log.error(ex.getMessage(), ex); } response.getHeaders().setContentType(MediaType.APPLICATION_JSON); response.setStatusCode(HttpStatus.OK); return response.writeWith(Mono.fromSupplier(() -> { DataBufferFactory bufferFactory = response.bufferFactory(); ActionResult result = ActionResult.fail(msg); return bufferFactory.wrap(JsonUtil.getObjectToString(result).getBytes()); })); } }