From 5a7084359e9b11fec1547ece08bb52548dc62f42 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 25 Jan 2024 14:49:51 +0100 Subject: [PATCH] [CSAPI]: Preserve exception cause on invalid payload For some exceptions, the cause backtrace provides necessary details for debugging the actual cause of the issue, since the message is not always detailed enough. --- .../org/sensorhub/impl/service/consys/ServiceErrors.java | 8 +++++++- .../impl/service/consys/resource/BaseResourceHandler.java | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/sensorhub-service-consys/src/main/java/org/sensorhub/impl/service/consys/ServiceErrors.java b/sensorhub-service-consys/src/main/java/org/sensorhub/impl/service/consys/ServiceErrors.java index 14b53ef87..3150bba63 100644 --- a/sensorhub-service-consys/src/main/java/org/sensorhub/impl/service/consys/ServiceErrors.java +++ b/sensorhub-service-consys/src/main/java/org/sensorhub/impl/service/consys/ServiceErrors.java @@ -42,7 +42,13 @@ public static InvalidRequestException badRequest(String msg) public static InvalidRequestException invalidPayload(String msg) { - return new InvalidRequestException(ErrorCode.BAD_PAYLOAD, msg); + return invalidPayload(msg, null); + } + + + public static InvalidRequestException invalidPayload(String msg, Throwable cause) + { + return new InvalidRequestException(ErrorCode.BAD_PAYLOAD, msg, cause); } diff --git a/sensorhub-service-consys/src/main/java/org/sensorhub/impl/service/consys/resource/BaseResourceHandler.java b/sensorhub-service-consys/src/main/java/org/sensorhub/impl/service/consys/resource/BaseResourceHandler.java index 93fa8048d..39e61c0aa 100644 --- a/sensorhub-service-consys/src/main/java/org/sensorhub/impl/service/consys/resource/BaseResourceHandler.java +++ b/sensorhub-service-consys/src/main/java/org/sensorhub/impl/service/consys/resource/BaseResourceHandler.java @@ -355,7 +355,7 @@ protected void create(final RequestContext ctx) throws IOException } catch (ResourceParseException e) { - throw ServiceErrors.invalidPayload("Invalid payload: " + e.getMessage()); + throw ServiceErrors.invalidPayload("Invalid payload: " + e.getMessage(), e); } } @@ -399,7 +399,7 @@ protected void update(final RequestContext ctx, final String id) throws IOExcept } catch (ResourceParseException e) { - throw ServiceErrors.invalidPayload("Invalid payload: " + e.getMessage()); + throw ServiceErrors.invalidPayload("Invalid payload: " + e.getMessage(), e); } // update in datastore