From 39e095c6bd7644fdee45b80ef2b0bd45e61ca3b5 Mon Sep 17 00:00:00 2001 From: FarukBraimo Date: Wed, 19 Jun 2024 21:05:27 +0200 Subject: [PATCH] Fix: Adding exception handler --- .../exception/ExceptionHandlerManager.java | 86 +++++++++++++++++++ .../falcon/controller/InsightController.java | 2 + .../falcon/model/response/ErrorMessage.java | 6 ++ 3 files changed, 94 insertions(+) create mode 100644 src/main/java/com/vodacom/falcon/config/exception/ExceptionHandlerManager.java create mode 100644 src/main/java/com/vodacom/falcon/model/response/ErrorMessage.java diff --git a/src/main/java/com/vodacom/falcon/config/exception/ExceptionHandlerManager.java b/src/main/java/com/vodacom/falcon/config/exception/ExceptionHandlerManager.java new file mode 100644 index 0000000..da7e828 --- /dev/null +++ b/src/main/java/com/vodacom/falcon/config/exception/ExceptionHandlerManager.java @@ -0,0 +1,86 @@ +package com.vodacom.falcon.config.exception; + +import com.vodacom.falcon.model.response.ErrorMessage; +import jakarta.servlet.http.HttpServletRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +import java.io.IOException; +import java.sql.SQLException; +import java.util.Objects; + +@Order(Ordered.HIGHEST_PRECEDENCE) +@ControllerAdvice +@RestControllerAdvice +public class ExceptionHandlerManager extends ResponseEntityExceptionHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionHandlerManager.class); + HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; + String message = "Error. %s - %s"; + @ExceptionHandler(HttpMessageNotReadableException.class) + @ResponseBody + protected ResponseEntity handleException(HttpMessageNotReadableException ex, HttpServletRequest request) { + final String DEFAULT_MESSAGE = "Malformed JSON request"; + final String INVALID_CREDENTIALS = "Invalid credentials"; + String error = ex.getMessage(); + + if (ex.getMessage().contains(INVALID_CREDENTIALS)) { + error = INVALID_CREDENTIALS; + status = HttpStatus.UNAUTHORIZED; + } else { + status = HttpStatus.BAD_REQUEST; + error = Objects.nonNull(error) ? error.contains("body is missing") ? "Required request body is missing" : error : DEFAULT_MESSAGE; + } + ErrorMessage response = returnMessage(error, ex); + return new ResponseEntity<>(response, response.status()); + } + + @ExceptionHandler(NullPointerException.class) + @ResponseBody + protected ResponseEntity handleNullPointerException(NullPointerException ex) { + status = HttpStatus.NO_CONTENT; + ErrorMessage response = returnMessage("Null Pointer Exception", ex); + return new ResponseEntity<>(response, response.status()); + } + + @ExceptionHandler(Exception.class) + @ResponseBody + protected ResponseEntity handleException(Exception ex) { + if (ex instanceof SQLException && (ex.getMessage().contains("SQLGrammarException") || ex.getMessage().contains("ConstraintViolationException"))) { + ErrorMessage response = returnMessage("SQLGrammarException", ex); + return new ResponseEntity<>(response, response.status()); + } + ErrorMessage response = returnMessage("Exception", ex); + return new ResponseEntity<>(response, response.status()); + } + + + @ExceptionHandler(IOException.class) + @ResponseBody + protected ResponseEntity handleIOException(IOException ex) { + ErrorMessage response = returnMessage("IO Exception", ex); + return new ResponseEntity<>(response, response.status()); + } + + @ExceptionHandler(RuntimeException.class) + @ResponseBody + protected ResponseEntity handleRuntimeException(RuntimeException ex){ + ErrorMessage response = returnMessage("RuntimeException", ex); + return new ResponseEntity<>(response, response.status()); + } + + private ErrorMessage returnMessage(String error, Exception ex){ + message = String.format(message, error, ex.getMessage()); + LOGGER.error(message); + return new ErrorMessage(message, status); + } +} diff --git a/src/main/java/com/vodacom/falcon/controller/InsightController.java b/src/main/java/com/vodacom/falcon/controller/InsightController.java index 5a6eb0a..a2a2ecd 100644 --- a/src/main/java/com/vodacom/falcon/controller/InsightController.java +++ b/src/main/java/com/vodacom/falcon/controller/InsightController.java @@ -37,6 +37,8 @@ public class InsightController { @ApiResponse(responseCode = "500", content = {@Content(schema = @Schema(implementation = String.class), mediaType = "application/json")}) }) public ResponseEntity getInsight(@RequestParam("city") @Valid String city) { + + InsightResponse response = falconInsightService.getInsight(city); return new ResponseEntity<>(response, HttpStatus.OK); } diff --git a/src/main/java/com/vodacom/falcon/model/response/ErrorMessage.java b/src/main/java/com/vodacom/falcon/model/response/ErrorMessage.java new file mode 100644 index 0000000..2ef6297 --- /dev/null +++ b/src/main/java/com/vodacom/falcon/model/response/ErrorMessage.java @@ -0,0 +1,6 @@ +package com.vodacom.falcon.model.response; + +import org.springframework.http.HttpStatus; + +public record ErrorMessage(String message, HttpStatus status) { +}