From f9c2aa3581c123d6c549bafc5372ef60370ef960 Mon Sep 17 00:00:00 2001 From: Volodymyr Babak Date: Fri, 30 Aug 2024 10:18:40 +0300 Subject: [PATCH] TbHttpClient: return cause instead of original exception in case 2xx status code --- .../rule/engine/rest/TbHttpClient.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java index 104e477346..839a7633fc 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java @@ -209,13 +209,23 @@ public class TbHttpClient { semaphore.release(); } - onFailure.accept(processException(msg, throwable), throwable); + onFailure.accept(processException(msg, throwable), processThrowable(throwable)); }); } catch (InterruptedException e) { log.warn("Timeout during waiting for reply!", e); } } + private Throwable processThrowable(Throwable origin) { + if (origin instanceof WebClientResponseException restClientResponseException + && restClientResponseException.getStatusCode().is2xxSuccessful()) { + // return cause instead of original exception in case 2xx status code + // this will provide meaningful error message to the user + return new RuntimeException(restClientResponseException.getCause()); + } + return origin; + } + public URI buildEncodedUri(String endpointUrl) { if (endpointUrl == null) { throw new RuntimeException("Url string cannot be null!"); @@ -287,9 +297,6 @@ public class TbHttpClient { metaData.putValue(STATUS, restClientResponseException.getStatusText()); metaData.putValue(STATUS_CODE, restClientResponseException.getStatusCode().value() + ""); metaData.putValue(ERROR_BODY, restClientResponseException.getResponseBodyAsString()); - if (restClientResponseException.getStatusCode().is2xxSuccessful()) { - metaData.putValue(ERROR, metaData.getValue(ERROR) + ". Cause: " +restClientResponseException.getCause().getMessage()); - } } return TbMsg.transformMsgMetadata(origMsg, metaData); }