Skip to content

Commit

Permalink
Added better handling of c8y SDK exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
cpoder committed Nov 10, 2024
1 parent cebd359 commit 447c218
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,7 +32,7 @@ private ResponseEntity<LoraError> 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)
Expand All @@ -41,7 +42,7 @@ ResponseEntity<LoraError> 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)
Expand All @@ -51,6 +52,16 @@ ResponseEntity<LoraError> 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<LoraError> 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()));
}
}

0 comments on commit 447c218

Please sign in to comment.