Added DataNotFoundException.

This commit is contained in:
Prominence 2018-07-05 23:41:02 +03:00
parent 3bb0eb043d
commit a1276abcea
3 changed files with 52 additions and 11 deletions

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2018 Alexey Zinchenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package by.prominence.openweather.api.exception;
public class DataNotFoundException extends Exception {
public DataNotFoundException() {
super("Data for provided parameters wasn't found. Please, check your request.");
}
public DataNotFoundException(String message) {
super(message);
}
}

View File

@ -22,6 +22,7 @@
package by.prominence.openweather.api.provider;
import by.prominence.openweather.api.exception.DataNotFoundException;
import by.prominence.openweather.api.exception.InvalidAuthTokenException;
import by.prominence.openweather.api.model.Coordinates;
import by.prominence.openweather.api.model.WeatherResponse;
@ -63,27 +64,27 @@ public class DefaultWeatherProvider implements WeatherProvider {
return this;
}
public WeatherResponse getByCityId(String id) throws InvalidAuthTokenException {
public WeatherResponse getByCityId(String id) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?id=" + id);
}
public WeatherResponse getByCityName(String name) throws InvalidAuthTokenException {
public WeatherResponse getByCityName(String name) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?q=" + name);
}
public WeatherResponse getByCoordinates(double latitude, double longitude) throws InvalidAuthTokenException {
public WeatherResponse getByCoordinates(double latitude, double longitude) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?lat=" + latitude + "&lon=" + longitude);
}
public WeatherResponse getByCoordinates(Coordinates coordinates) throws InvalidAuthTokenException {
public WeatherResponse getByCoordinates(Coordinates coordinates) throws InvalidAuthTokenException, DataNotFoundException {
return getByCoordinates(coordinates.getLatitude(), coordinates.getLongitude());
}
public WeatherResponse getByZIPCode(String zipCode, String countryCode) throws InvalidAuthTokenException {
public WeatherResponse getByZIPCode(String zipCode, String countryCode) throws InvalidAuthTokenException, DataNotFoundException {
return executeRequest("?zip=" + zipCode + "," + countryCode);
}
private WeatherResponse executeRequest(String parameterString) throws InvalidAuthTokenException {
private WeatherResponse executeRequest(String parameterString) throws InvalidAuthTokenException, DataNotFoundException {
String url = OPEN_WEATHER_API_URL + parameterString + "&appid=" + authToken;
@ -117,6 +118,11 @@ public class DefaultWeatherProvider implements WeatherProvider {
if (weatherResponse.getResponseCode() == 401) {
throw new InvalidAuthTokenException();
}
if (weatherResponse.getResponseCode() == 404) {
throw new DataNotFoundException();
}
} catch (IOException e) {
e.printStackTrace();
}

View File

@ -22,17 +22,18 @@
package by.prominence.openweather.api.provider;
import by.prominence.openweather.api.exception.DataNotFoundException;
import by.prominence.openweather.api.exception.InvalidAuthTokenException;
import by.prominence.openweather.api.model.Coordinates;
import by.prominence.openweather.api.model.WeatherResponse;
public interface WeatherProvider {
public WeatherResponse getByCityId(String id) throws InvalidAuthTokenException;
public WeatherResponse getByCityName(String name) throws InvalidAuthTokenException;
public WeatherResponse getByCoordinates(double latitude, double longitude) throws InvalidAuthTokenException;
public WeatherResponse getByCoordinates(Coordinates coordinates) throws InvalidAuthTokenException;
public WeatherResponse getByZIPCode(String zipCode, String countryCode) throws InvalidAuthTokenException;
public WeatherResponse getByCityId(String id) throws InvalidAuthTokenException, DataNotFoundException;
public WeatherResponse getByCityName(String name) throws InvalidAuthTokenException, DataNotFoundException;
public WeatherResponse getByCoordinates(double latitude, double longitude) throws InvalidAuthTokenException, DataNotFoundException;
public WeatherResponse getByCoordinates(Coordinates coordinates) throws InvalidAuthTokenException, DataNotFoundException;
public WeatherResponse getByZIPCode(String zipCode, String countryCode) throws InvalidAuthTokenException, DataNotFoundException;
public WeatherProvider setLanguage(String language);
public WeatherProvider setUnit(String unit);