|
| 1 | +package com.github.cadecode.uniboot.gateway.handler; |
| 2 | + |
| 3 | +import cn.hutool.core.util.ObjectUtil; |
| 4 | +import com.github.cadecode.uniboot.common.core.enums.ApiErrorCode; |
| 5 | +import com.github.cadecode.uniboot.common.core.util.JacksonUtil; |
| 6 | +import com.github.cadecode.uniboot.common.core.web.response.ApiResult; |
| 7 | +import com.github.cadecode.uniboot.framework.api.enums.FrameErrorEnum; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; |
| 10 | +import org.springframework.cloud.gateway.support.NotFoundException; |
| 11 | +import org.springframework.core.annotation.Order; |
| 12 | +import org.springframework.core.io.buffer.DataBuffer; |
| 13 | +import org.springframework.http.HttpHeaders; |
| 14 | +import org.springframework.http.HttpStatus; |
| 15 | +import org.springframework.http.MediaType; |
| 16 | +import org.springframework.http.server.reactive.ServerHttpResponse; |
| 17 | +import org.springframework.stereotype.Component; |
| 18 | +import org.springframework.web.server.ResponseStatusException; |
| 19 | +import org.springframework.web.server.ServerWebExchange; |
| 20 | +import reactor.core.publisher.Mono; |
| 21 | + |
| 22 | +/** |
| 23 | + * 网关全局异常处理 |
| 24 | + * |
| 25 | + * @author Cade Li |
| 26 | + * @since 2023/8/11 |
| 27 | + */ |
| 28 | +@Slf4j |
| 29 | +@Order(-1) |
| 30 | +@Component |
| 31 | +public class GlobalExceptionHandler implements ErrorWebExceptionHandler { |
| 32 | + |
| 33 | + @Override |
| 34 | + public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) { |
| 35 | + ServerHttpResponse response = exchange.getResponse(); |
| 36 | + if (exchange.getResponse().isCommitted()) { |
| 37 | + return Mono.error(ex); |
| 38 | + } |
| 39 | + ApiResult<Object> result = getApiResultFromEx(exchange, ex); |
| 40 | + log.error("[GW]Exception handler=> status:{}, path:{}, msg:{},", result.getStatus(), result.getError().getPath(), result.getError().getMessage(), ex); |
| 41 | + response.setStatusCode(HttpStatus.resolve(result.getStatus())); |
| 42 | + response.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); |
| 43 | + DataBuffer dataBuffer = response.bufferFactory().wrap(JacksonUtil.toJson(result).getBytes()); |
| 44 | + return response.writeWith(Mono.just(dataBuffer)); |
| 45 | + } |
| 46 | + |
| 47 | + private ApiResult<Object> getApiResultFromEx(ServerWebExchange exchange, Throwable ex) { |
| 48 | + String moreInfo = ex.getMessage(); |
| 49 | + String path = exchange.getRequest().getPath().toString(); |
| 50 | + Integer status = null; |
| 51 | + ApiErrorCode errorCode; |
| 52 | + // 服务未找到 503 |
| 53 | + if (ex instanceof NotFoundException) { |
| 54 | + errorCode = FrameErrorEnum.GW_SVC_NOT_FOUND; |
| 55 | + } else if (ex instanceof ResponseStatusException) { |
| 56 | + // 响应状态异常 |
| 57 | + errorCode = FrameErrorEnum.GW_RES_STATUS_ERROR; |
| 58 | + ResponseStatusException e = (ResponseStatusException) ex; |
| 59 | + status = e.getStatus().value(); |
| 60 | + } else { |
| 61 | + // 其他异常 |
| 62 | + errorCode = FrameErrorEnum.GW_SVC_INTERNAL_ERROR; |
| 63 | + } |
| 64 | + ApiResult<Object> result = ApiResult.error(errorCode).moreInfo(moreInfo).path(path); |
| 65 | + if (ObjectUtil.isNotNull(status)) { |
| 66 | + result.status(status); |
| 67 | + } |
| 68 | + return result; |
| 69 | + } |
| 70 | +} |
0 commit comments