diff --git a/src/main/java/com/vodacom/falcon/config/exception/ExceptionHandlerManager.java b/src/main/java/com/vodacom/falcon/config/exception/ExceptionHandlerManager.java deleted file mode 100644 index da7e828..0000000 --- a/src/main/java/com/vodacom/falcon/config/exception/ExceptionHandlerManager.java +++ /dev/null @@ -1,86 +0,0 @@ -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/service/ExchangeRateService.java b/src/main/java/com/vodacom/falcon/service/ExchangeRateService.java index 817c015..2ba99b4 100644 --- a/src/main/java/com/vodacom/falcon/service/ExchangeRateService.java +++ b/src/main/java/com/vodacom/falcon/service/ExchangeRateService.java @@ -33,11 +33,9 @@ public ExchangeRateResponse getExchangeRates(String countryCode) { ExchangeRateResponse ratesFromMainSource = buildExchangeRates(mainExchangeRateUrl); - -// FixME: Enable this -// if (ratesFromMainSource != null) { -// return ratesFromMainSource; -// } + if (ratesFromMainSource != null) { + return ratesFromMainSource; + } return buildExchangeRates(optionalExchangeRateUrl); } diff --git a/src/main/java/com/vodacom/falcon/service/WeatherForecastService.java b/src/main/java/com/vodacom/falcon/service/WeatherForecastService.java index 62e822f..c10b56d 100644 --- a/src/main/java/com/vodacom/falcon/service/WeatherForecastService.java +++ b/src/main/java/com/vodacom/falcon/service/WeatherForecastService.java @@ -25,9 +25,8 @@ public class WeatherForecastService { @Value("${open-weather-map.apiKeyV3}") private String openWeatherApiKeyV3; - private final String OPEN_WEATHER_API_VERSION = "2.5"; // TODO: Add feature flag Or row controller, to determine the version to use. (2.5 0r 3.0) + private final String OPEN_WEATHER_API_VERSION = "3.0"; - // TODO: CACHE THE RESPONSE public WeatherForecastResponse getWeatherForecast(String city) { return WeatherForecastResponse .builder() @@ -43,7 +42,7 @@ private OpenWeatherForecastResponse buildWeatherForecast(String city) { return null; } - String url = String.format("%s/data/%s/onecall?units=metric&cnt=4&exclude=hourly,minutely,alerts&lat=%s&lon=%s&appid=%s", FalconDefaults.OPEN_WEATHER_API_BASE_URL, OPEN_WEATHER_API_VERSION, location.getLat(), location.getLon(), openWeatherApiKeyV2); + String url = String.format("%s/data/%s/onecall?units=metric&cnt=4&exclude=hourly,minutely,alerts&lat=%s&lon=%s&appid=%s", FalconDefaults.OPEN_WEATHER_API_BASE_URL, OPEN_WEATHER_API_VERSION, location.getLat(), location.getLon(), openWeatherApiKeyV3); HttpResponse response = APICaller.getData(url); if (response != null) { OpenWeatherForecastResponse openWeatherForecast = deserialize(response.body(), OpenWeatherForecastResponse.class);