From 447c21877439223db01fa6c16241995fc1a59ee5 Mon Sep 17 00:00:00 2001 From: cpoder Date: Sun, 10 Nov 2024 14:45:28 +0100 Subject: [PATCH] Added better handling of c8y SDK exceptions --- .../lora/ns/exception/LoraExceptionHandler.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/java/lora-ns-ms/src/main/java/lora/ns/exception/LoraExceptionHandler.java b/java/lora-ns-ms/src/main/java/lora/ns/exception/LoraExceptionHandler.java index 362eeefc2..be1344b14 100644 --- a/java/lora-ns-ms/src/main/java/lora/ns/exception/LoraExceptionHandler.java +++ b/java/lora-ns-ms/src/main/java/lora/ns/exception/LoraExceptionHandler.java @@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; import com.cumulocity.model.event.CumulocitySeverities; +import com.cumulocity.sdk.client.SDKException; import feign.FeignException; import feign.FeignException.FeignClientException; @@ -31,7 +32,7 @@ private ResponseEntity processLoraException(LoraException e) { StringWriter detailedMessage = new StringWriter(); e.printStackTrace(new PrintWriter(detailedMessage)); return new ResponseEntity<>(new LoraError(e.getMessage(), detailedMessage.toString()), - HttpStatus.INTERNAL_SERVER_ERROR); + HttpStatus.INTERNAL_SERVER_ERROR); } @ExceptionHandler(FeignClientException.class) @@ -41,7 +42,7 @@ ResponseEntity processFeignClientException(FeignClientException e) { StringWriter detailedMessage = new StringWriter(); e.printStackTrace(new PrintWriter(detailedMessage)); return new ResponseEntity<>(new LoraError(e.getMessage(), detailedMessage.toString()), - HttpStatus.resolve(e.status())); + HttpStatus.resolve(e.status())); } @ExceptionHandler(FeignException.class) @@ -51,6 +52,16 @@ ResponseEntity processFeignException(FeignException e) { StringWriter detailedMessage = new StringWriter(); e.printStackTrace(new PrintWriter(detailedMessage)); return new ResponseEntity<>(new LoraError(e.getMessage(), detailedMessage.toString()), - HttpStatus.resolve(e.status())); + HttpStatus.resolve(e.status())); + } + + @ExceptionHandler(SDKException.class) + ResponseEntity processC8YSDKException(SDKException e) { + loraContextService.error(e.getMessage(), e); + loraContextService.sendAlarm(e.getClass().getSimpleName(), e.getMessage(), CumulocitySeverities.CRITICAL); + StringWriter detailedMessage = new StringWriter(); + e.printStackTrace(new PrintWriter(detailedMessage)); + return new ResponseEntity<>(new LoraError(e.getMessage(), detailedMessage.toString()), + HttpStatus.resolve(e.getHttpStatus())); } }