From c9cdee4d24a635764cb21ddc8c44dcfde18cd39e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Wed, 13 Jan 2021 15:46:09 +0800 Subject: [PATCH] perf($ApiGateway): capture FeignException [skip ci] --- .../universal/handler/GlobalExceptionHandler.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/api-gateway/src/main/java/com/jmsoftware/maf/apigateway/universal/handler/GlobalExceptionHandler.java b/api-gateway/src/main/java/com/jmsoftware/maf/apigateway/universal/handler/GlobalExceptionHandler.java index f6008760..f38f8bf4 100644 --- a/api-gateway/src/main/java/com/jmsoftware/maf/apigateway/universal/handler/GlobalExceptionHandler.java +++ b/api-gateway/src/main/java/com/jmsoftware/maf/apigateway/universal/handler/GlobalExceptionHandler.java @@ -1,11 +1,13 @@ package com.jmsoftware.maf.apigateway.universal.handler; +import cn.hutool.core.util.ObjectUtil; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.jmsoftware.maf.common.bean.ResponseBodyBean; import com.jmsoftware.maf.common.exception.SecurityException; import com.jmsoftware.maf.reactivespringbootstarter.util.RequestUtil; import com.netflix.hystrix.exception.HystrixRuntimeException; +import feign.FeignException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import lombok.val; @@ -73,6 +75,13 @@ private ResponseBodyBean setResponseBody(ServerHttpResponse response, Throwab response.setStatusCode(((ResponseStatusException) ex).getStatus()); return ResponseBodyBean.ofStatus(((ResponseStatusException) ex).getStatus()); } else if (ex instanceof HystrixRuntimeException) { + if (ObjectUtil.isNotNull(ex.getCause()) && ex.getCause() instanceof FeignException) { + val cause = (FeignException) ex.getCause(); + val httpStatus = HttpStatus.valueOf(cause.status()); + response.setStatusCode(httpStatus); + return ResponseBodyBean.ofStatus(httpStatus, + String.format("%s %s", ex.getMessage(), ex.getCause().getMessage())); + } response.setStatusCode(HttpStatus.SERVICE_UNAVAILABLE); return ResponseBodyBean.ofStatus(HttpStatus.SERVICE_UNAVAILABLE, String.format("%s %s", ex.getMessage(), ex.getCause().getMessage()));