[openweathermap] Improved exception handling (#11949)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2022-01-03 14:00:25 +01:00 committed by GitHub
parent 41d775c02b
commit dee9ab891f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 75 additions and 208 deletions

View File

@ -1,63 +0,0 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.openweathermap.internal.connection;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/**
* The {@link OpenWeatherMapCommunicationException} is a communication exception for the connections to OpenWeatherMap
* API.
*
* @author Christoph Weitkamp - Initial contribution
*/
@NonNullByDefault
public class OpenWeatherMapCommunicationException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* Constructs a new exception with null as its detail message.
*/
public OpenWeatherMapCommunicationException() {
super();
}
/**
* Constructs a new exception with the specified detail message.
*
* @param message Detail message
*/
public OpenWeatherMapCommunicationException(@Nullable String message) {
super(message);
}
/**
* Constructs a new exception with the specified cause.
*
* @param cause The cause
*/
public OpenWeatherMapCommunicationException(@Nullable Throwable cause) {
super(cause);
}
/**
* Constructs a new exception with the specified detail message and cause.
*
* @param message Detail message
* @param cause The cause
*/
public OpenWeatherMapCommunicationException(@Nullable String message, @Nullable Throwable cause) {
super(message, cause);
}
}

View File

@ -1,63 +0,0 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.openweathermap.internal.connection;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/**
* The {@link OpenWeatherMapConfigurationException} is a configuration exception for the connections to OpenWeatherMap
* API.
*
* @author Christoph Weitkamp - Initial contribution
*/
@NonNullByDefault
public class OpenWeatherMapConfigurationException extends IllegalArgumentException {
private static final long serialVersionUID = 1L;
/**
* Constructs a new exception with null as its detail message.
*/
public OpenWeatherMapConfigurationException() {
super();
}
/**
* Constructs a new exception with the specified detail message.
*
* @param message Detail message
*/
public OpenWeatherMapConfigurationException(@Nullable String message) {
super(message);
}
/**
* Constructs a new exception with the specified cause.
*
* @param cause The cause
*/
public OpenWeatherMapConfigurationException(@Nullable Throwable cause) {
super(cause);
}
/**
* Constructs a new exception with the specified detail message and cause.
*
* @param message Detail message
* @param cause The cause
*/
public OpenWeatherMapConfigurationException(@Nullable String message, @Nullable Throwable cause) {
super(message, cause);
}
}

View File

@ -15,7 +15,6 @@ package org.openhab.binding.openweathermap.internal.connection;
import static org.eclipse.jetty.http.HttpMethod.GET; import static org.eclipse.jetty.http.HttpMethod.GET;
import static org.eclipse.jetty.http.HttpStatus.*; import static org.eclipse.jetty.http.HttpStatus.*;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.ZoneId; import java.time.ZoneId;
@ -46,6 +45,8 @@ import org.openhab.binding.openweathermap.internal.dto.onecallhist.OpenWeatherMa
import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapAPIHandler; import org.openhab.binding.openweathermap.internal.handler.OpenWeatherMapAPIHandler;
import org.openhab.core.cache.ByteArrayFileCache; import org.openhab.core.cache.ByteArrayFileCache;
import org.openhab.core.cache.ExpiringCacheMap; import org.openhab.core.cache.ExpiringCacheMap;
import org.openhab.core.i18n.CommunicationException;
import org.openhab.core.i18n.ConfigurationException;
import org.openhab.core.io.net.http.HttpUtil; import org.openhab.core.io.net.http.HttpUtil;
import org.openhab.core.library.types.PointType; import org.openhab.core.library.types.PointType;
import org.openhab.core.library.types.RawType; import org.openhab.core.library.types.RawType;
@ -121,11 +122,11 @@ public class OpenWeatherMapConnection {
* @param location location represented as {@link PointType} * @param location location represented as {@link PointType}
* @return the current weather data * @return the current weather data
* @throws JsonSyntaxException * @throws JsonSyntaxException
* @throws OpenWeatherMapCommunicationException * @throws CommunicationException
* @throws OpenWeatherMapConfigurationException * @throws ConfigurationException
*/ */
public synchronized @Nullable OpenWeatherMapJsonWeatherData getWeatherData(@Nullable PointType location) public synchronized @Nullable OpenWeatherMapJsonWeatherData getWeatherData(@Nullable PointType location)
throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException { throws JsonSyntaxException, CommunicationException, ConfigurationException {
return gson.fromJson( return gson.fromJson(
getResponseFromCache( getResponseFromCache(
buildURL(WEATHER_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))), buildURL(WEATHER_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
@ -139,14 +140,14 @@ public class OpenWeatherMapConnection {
* @param count number of hours * @param count number of hours
* @return the hourly forecast data * @return the hourly forecast data
* @throws JsonSyntaxException * @throws JsonSyntaxException
* @throws OpenWeatherMapCommunicationException * @throws CommunicationException
* @throws OpenWeatherMapConfigurationException * @throws ConfigurationException
*/ */
public synchronized @Nullable OpenWeatherMapJsonHourlyForecastData getHourlyForecastData( public synchronized @Nullable OpenWeatherMapJsonHourlyForecastData getHourlyForecastData(
@Nullable PointType location, int count) @Nullable PointType location, int count)
throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException { throws JsonSyntaxException, CommunicationException, ConfigurationException {
if (count <= 0) { if (count <= 0) {
throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-not-supported-number-of-hours"); throw new ConfigurationException("@text/offline.conf-error-not-supported-number-of-hours");
} }
Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location); Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
@ -163,14 +164,13 @@ public class OpenWeatherMapConnection {
* @param count number of days * @param count number of days
* @return the daily forecast data * @return the daily forecast data
* @throws JsonSyntaxException * @throws JsonSyntaxException
* @throws OpenWeatherMapCommunicationException * @throws CommunicationException
* @throws OpenWeatherMapConfigurationException * @throws ConfigurationException
*/ */
public synchronized @Nullable OpenWeatherMapJsonDailyForecastData getDailyForecastData(@Nullable PointType location, public synchronized @Nullable OpenWeatherMapJsonDailyForecastData getDailyForecastData(@Nullable PointType location,
int count) int count) throws JsonSyntaxException, CommunicationException, ConfigurationException {
throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
if (count <= 0) { if (count <= 0) {
throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-not-supported-number-of-days"); throw new ConfigurationException("@text/offline.conf-error-not-supported-number-of-days");
} }
Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location); Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
@ -186,11 +186,11 @@ public class OpenWeatherMapConnection {
* @param location location represented as {@link PointType} * @param location location represented as {@link PointType}
* @return the UV Index data * @return the UV Index data
* @throws JsonSyntaxException * @throws JsonSyntaxException
* @throws OpenWeatherMapCommunicationException * @throws CommunicationException
* @throws OpenWeatherMapConfigurationException * @throws ConfigurationException
*/ */
public synchronized @Nullable OpenWeatherMapJsonUVIndexData getUVIndexData(@Nullable PointType location) public synchronized @Nullable OpenWeatherMapJsonUVIndexData getUVIndexData(@Nullable PointType location)
throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException { throws JsonSyntaxException, CommunicationException, ConfigurationException {
return gson.fromJson( return gson.fromJson(
getResponseFromCache( getResponseFromCache(
buildURL(UVINDEX_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))), buildURL(UVINDEX_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
@ -203,15 +203,14 @@ public class OpenWeatherMapConnection {
* @param location location represented as {@link PointType} * @param location location represented as {@link PointType}
* @return the UV Index forecast data * @return the UV Index forecast data
* @throws JsonSyntaxException * @throws JsonSyntaxException
* @throws OpenWeatherMapCommunicationException * @throws CommunicationException
* @throws OpenWeatherMapConfigurationException * @throws ConfigurationException
*/ */
public synchronized @Nullable List<OpenWeatherMapJsonUVIndexData> getUVIndexForecastData( public synchronized @Nullable List<OpenWeatherMapJsonUVIndexData> getUVIndexForecastData(
@Nullable PointType location, int count) @Nullable PointType location, int count)
throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException { throws JsonSyntaxException, CommunicationException, ConfigurationException {
if (count <= 0) { if (count <= 0) {
throw new OpenWeatherMapConfigurationException( throw new ConfigurationException("@text/offline.conf-error-not-supported-uvindex-number-of-days");
"@text/offline.conf-error-not-supported-uvindex-number-of-days");
} }
Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location); Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
@ -227,11 +226,11 @@ public class OpenWeatherMapConnection {
* @param location location represented as {@link PointType} * @param location location represented as {@link PointType}
* @return the Air Pollution data * @return the Air Pollution data
* @throws JsonSyntaxException * @throws JsonSyntaxException
* @throws OpenWeatherMapCommunicationException * @throws CommunicationException
* @throws OpenWeatherMapConfigurationException * @throws ConfigurationException
*/ */
public synchronized @Nullable OpenWeatherMapJsonAirPollutionData getAirPollutionData(@Nullable PointType location) public synchronized @Nullable OpenWeatherMapJsonAirPollutionData getAirPollutionData(@Nullable PointType location)
throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException { throws JsonSyntaxException, CommunicationException, ConfigurationException {
return gson.fromJson( return gson.fromJson(
getResponseFromCache( getResponseFromCache(
buildURL(AIR_POLLUTION_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))), buildURL(AIR_POLLUTION_URL, getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
@ -245,12 +244,11 @@ public class OpenWeatherMapConnection {
* @param location location represented as {@link PointType} * @param location location represented as {@link PointType}
* @return the Air Pollution forecast data * @return the Air Pollution forecast data
* @throws JsonSyntaxException * @throws JsonSyntaxException
* @throws OpenWeatherMapCommunicationException * @throws CommunicationException
* @throws OpenWeatherMapConfigurationException * @throws ConfigurationException
*/ */
public synchronized @Nullable OpenWeatherMapJsonAirPollutionData getAirPollutionForecastData( public synchronized @Nullable OpenWeatherMapJsonAirPollutionData getAirPollutionForecastData(
@Nullable PointType location) @Nullable PointType location) throws JsonSyntaxException, CommunicationException, ConfigurationException {
throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
return gson.fromJson( return gson.fromJson(
getResponseFromCache(buildURL(AIR_POLLUTION_FORECAST_URL, getResponseFromCache(buildURL(AIR_POLLUTION_FORECAST_URL,
getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))), getRequestParams(handler.getOpenWeatherMapAPIConfig(), location))),
@ -303,12 +301,12 @@ public class OpenWeatherMapConnection {
* @param excludeDaily if true, will not fetch hourly forecast data from the server * @param excludeDaily if true, will not fetch hourly forecast data from the server
* @return * @return
* @throws JsonSyntaxException * @throws JsonSyntaxException
* @throws OpenWeatherMapCommunicationException * @throws CommunicationException
* @throws OpenWeatherMapConfigurationException * @throws ConfigurationException
*/ */
public synchronized @Nullable OpenWeatherMapOneCallAPIData getOneCallAPIData(@Nullable PointType location, public synchronized @Nullable OpenWeatherMapOneCallAPIData getOneCallAPIData(@Nullable PointType location,
boolean excludeMinutely, boolean excludeHourly, boolean excludeDaily, boolean excludeAlerts) boolean excludeMinutely, boolean excludeHourly, boolean excludeDaily, boolean excludeAlerts)
throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException { throws JsonSyntaxException, CommunicationException, ConfigurationException {
Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location); Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
List<String> exclude = new ArrayList<>(); List<String> exclude = new ArrayList<>();
if (excludeMinutely) { if (excludeMinutely) {
@ -339,12 +337,11 @@ public class OpenWeatherMapConnection {
* @param days number of days in the past, relative to the current time. * @param days number of days in the past, relative to the current time.
* @return * @return
* @throws JsonSyntaxException * @throws JsonSyntaxException
* @throws OpenWeatherMapCommunicationException * @throws CommunicationException
* @throws OpenWeatherMapConfigurationException * @throws ConfigurationException
*/ */
public synchronized @Nullable OpenWeatherMapOneCallHistAPIData getOneCallHistAPIData(@Nullable PointType location, public synchronized @Nullable OpenWeatherMapOneCallHistAPIData getOneCallHistAPIData(@Nullable PointType location,
int days) int days) throws JsonSyntaxException, CommunicationException, ConfigurationException {
throws JsonSyntaxException, OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location); Map<String, String> params = getRequestParams(handler.getOpenWeatherMapAPIConfig(), location);
// the API requests the history as timestamp in Unix time format. // the API requests the history as timestamp in Unix time format.
params.put(PARAM_HISTORY_DATE, params.put(PARAM_HISTORY_DATE,
@ -355,14 +352,14 @@ public class OpenWeatherMapConnection {
private Map<String, String> getRequestParams(OpenWeatherMapAPIConfiguration config, @Nullable PointType location) { private Map<String, String> getRequestParams(OpenWeatherMapAPIConfiguration config, @Nullable PointType location) {
if (location == null) { if (location == null) {
throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-missing-location"); throw new ConfigurationException("@text/offline.conf-error-missing-location");
} }
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
// API key (see https://openweathermap.org/appid) // API key (see https://openweathermap.org/appid)
String apikey = config.apikey; String apikey = config.apikey;
if (apikey == null || (apikey = apikey.trim()).isEmpty()) { if (apikey == null || (apikey = apikey.trim()).isEmpty()) {
throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-missing-apikey"); throw new ConfigurationException("@text/offline.conf-error-missing-apikey");
} }
params.put(PARAM_APPID, apikey); params.put(PARAM_APPID, apikey);
@ -387,15 +384,7 @@ public class OpenWeatherMapConnection {
} }
private String encodeParam(@Nullable String value) { private String encodeParam(@Nullable String value) {
if (value == null) { return value == null ? "" : URLEncoder.encode(value, StandardCharsets.UTF_8);
return "";
}
try {
return URLEncoder.encode(value, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
logger.debug("UnsupportedEncodingException occurred during execution: {}", e.getLocalizedMessage(), e);
return "";
}
} }
private @Nullable String getResponseFromCache(String url) { private @Nullable String getResponseFromCache(String url) {
@ -421,26 +410,29 @@ public class OpenWeatherMapConnection {
case NOT_FOUND_404: case NOT_FOUND_404:
errorMessage = getErrorMessage(content); errorMessage = getErrorMessage(content);
logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage); logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage);
throw new OpenWeatherMapConfigurationException(errorMessage); throw new ConfigurationException(errorMessage);
case TOO_MANY_REQUESTS_429: case TOO_MANY_REQUESTS_429:
// TODO disable refresh job temporarily (see https://openweathermap.org/appid#Accesslimitation) // TODO disable refresh job temporarily (see https://openweathermap.org/appid#Accesslimitation)
default: default:
errorMessage = getErrorMessage(content); errorMessage = getErrorMessage(content);
logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage); logger.debug("OpenWeatherMap server responded with status code {}: {}", httpStatus, errorMessage);
throw new OpenWeatherMapCommunicationException(errorMessage); throw new CommunicationException(errorMessage);
} }
} catch (ExecutionException e) { } catch (ExecutionException e) {
String errorMessage = e.getLocalizedMessage(); String errorMessage = e.getMessage();
logger.trace("Exception occurred during execution: {}", errorMessage, e); logger.debug("ExecutionException occurred during execution: {}", errorMessage, e);
if (e.getCause() instanceof HttpResponseException) { if (e.getCause() instanceof HttpResponseException) {
logger.debug("OpenWeatherMap server responded with status code {}: Invalid API key.", UNAUTHORIZED_401); logger.debug("OpenWeatherMap server responded with status code {}: Invalid API key.", UNAUTHORIZED_401);
throw new OpenWeatherMapConfigurationException("@text/offline.conf-error-invalid-apikey", e.getCause()); throw new ConfigurationException("@text/offline.conf-error-invalid-apikey", e.getCause());
} else { } else {
throw new OpenWeatherMapCommunicationException(errorMessage, e.getCause()); throw new CommunicationException(
errorMessage == null ? "@text/offline.communication-error" : errorMessage, e.getCause());
} }
} catch (InterruptedException | TimeoutException e) { } catch (InterruptedException | TimeoutException e) {
logger.debug("Exception occurred during execution: {}", e.getLocalizedMessage(), e); String errorMessage = e.getMessage();
throw new OpenWeatherMapCommunicationException(e.getLocalizedMessage(), e.getCause()); logger.debug("InterruptedException or TimeoutException occurred during execution: {}", errorMessage, e);
throw new CommunicationException(errorMessage == null ? "@text/offline.communication-error" : errorMessage,
e.getCause());
} }
} }

View File

@ -25,9 +25,9 @@ import javax.measure.Unit;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapLocationConfiguration; import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapLocationConfiguration;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapCommunicationException;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConfigurationException;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection; import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection;
import org.openhab.core.i18n.CommunicationException;
import org.openhab.core.i18n.ConfigurationException;
import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.DecimalType;
@ -137,9 +137,9 @@ public abstract class AbstractOpenWeatherMapHandler extends BaseThingHandler {
updateChannels(); updateChannels();
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
} }
} catch (OpenWeatherMapCommunicationException e) { } catch (CommunicationException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getLocalizedMessage()); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getLocalizedMessage());
} catch (OpenWeatherMapConfigurationException e) { } catch (ConfigurationException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getLocalizedMessage()); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getLocalizedMessage());
} }
} }
@ -149,11 +149,11 @@ public abstract class AbstractOpenWeatherMapHandler extends BaseThingHandler {
* *
* @param connection {@link OpenWeatherMapConnection} instance * @param connection {@link OpenWeatherMapConnection} instance
* @return true, if the request for the OpenWeatherMap data was successful * @return true, if the request for the OpenWeatherMap data was successful
* @throws OpenWeatherMapCommunicationException if there is a problem retrieving the data * @throws CommunicationException if there is a problem retrieving the data
* @throws OpenWeatherMapConfigurationException if there is a configuration error * @throws ConfigurationException if there is a configuration error
*/ */
protected abstract boolean requestData(OpenWeatherMapConnection connection) protected abstract boolean requestData(OpenWeatherMapConnection connection)
throws OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException; throws CommunicationException, ConfigurationException;
/** /**
* Updates all channels of this handler from the latest OpenWeatherMap data retrieved. * Updates all channels of this handler from the latest OpenWeatherMap data retrieved.

View File

@ -22,10 +22,10 @@ import java.util.regex.Pattern;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapAirPollutionConfiguration; import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapAirPollutionConfiguration;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapCommunicationException;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConfigurationException;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection; import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection;
import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonAirPollutionData; import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonAirPollutionData;
import org.openhab.core.i18n.CommunicationException;
import org.openhab.core.i18n.ConfigurationException;
import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.unit.Units; import org.openhab.core.library.unit.Units;
@ -111,7 +111,7 @@ public class OpenWeatherMapAirPollutionHandler extends AbstractOpenWeatherMapHan
@Override @Override
protected boolean requestData(OpenWeatherMapConnection connection) protected boolean requestData(OpenWeatherMapConnection connection)
throws OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException { throws CommunicationException, ConfigurationException {
logger.debug("Update air pollution data of thing '{}'.", getThing().getUID()); logger.debug("Update air pollution data of thing '{}'.", getThing().getUID());
try { try {
airPollutionData = connection.getAirPollutionData(location); airPollutionData = connection.getAirPollutionData(location);
@ -120,7 +120,7 @@ public class OpenWeatherMapAirPollutionHandler extends AbstractOpenWeatherMapHan
} }
return true; return true;
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {
logger.debug("JsonSyntaxException occurred during execution: {}", e.getLocalizedMessage(), e); logger.debug("JsonSyntaxException occurred during execution: {}", e.getMessage(), e);
return false; return false;
} }
} }

View File

@ -25,8 +25,6 @@ import java.util.regex.Pattern;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapOneCallConfiguration; import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapOneCallConfiguration;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapCommunicationException;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConfigurationException;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection; import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection;
import org.openhab.binding.openweathermap.internal.dto.onecall.Alert; import org.openhab.binding.openweathermap.internal.dto.onecall.Alert;
import org.openhab.binding.openweathermap.internal.dto.onecall.FeelsLike; import org.openhab.binding.openweathermap.internal.dto.onecall.FeelsLike;
@ -34,6 +32,8 @@ import org.openhab.binding.openweathermap.internal.dto.onecall.OpenWeatherMapOne
import org.openhab.binding.openweathermap.internal.dto.onecall.Rain; import org.openhab.binding.openweathermap.internal.dto.onecall.Rain;
import org.openhab.binding.openweathermap.internal.dto.onecall.Snow; import org.openhab.binding.openweathermap.internal.dto.onecall.Snow;
import org.openhab.binding.openweathermap.internal.dto.onecall.Temp; import org.openhab.binding.openweathermap.internal.dto.onecall.Temp;
import org.openhab.core.i18n.CommunicationException;
import org.openhab.core.i18n.ConfigurationException;
import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.QuantityType;
import org.openhab.core.thing.Channel; import org.openhab.core.thing.Channel;
@ -215,14 +215,14 @@ public class OpenWeatherMapOneCallHandler extends AbstractOpenWeatherMapHandler
@Override @Override
protected boolean requestData(OpenWeatherMapConnection connection) protected boolean requestData(OpenWeatherMapConnection connection)
throws OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException { throws CommunicationException, ConfigurationException {
logger.debug("Update weather and forecast data of thing '{}'.", getThing().getUID()); logger.debug("Update weather and forecast data of thing '{}'.", getThing().getUID());
try { try {
weatherData = connection.getOneCallAPIData(location, forecastMinutes == 0, forecastHours == 0, weatherData = connection.getOneCallAPIData(location, forecastMinutes == 0, forecastHours == 0,
forecastDays == 0, numberOfAlerts == 0); forecastDays == 0, numberOfAlerts == 0);
return true; return true;
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {
logger.debug("JsonSyntaxException occurred during execution: {}", e.getLocalizedMessage(), e); logger.debug("JsonSyntaxException occurred during execution: {}", e.getMessage(), e);
return false; return false;
} }
} }

View File

@ -23,13 +23,13 @@ import java.util.regex.Pattern;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapOneCallHistoryConfiguration; import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapOneCallHistoryConfiguration;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapCommunicationException;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConfigurationException;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection; import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection;
import org.openhab.binding.openweathermap.internal.dto.onecallhist.Hourly; import org.openhab.binding.openweathermap.internal.dto.onecallhist.Hourly;
import org.openhab.binding.openweathermap.internal.dto.onecallhist.OpenWeatherMapOneCallHistAPIData; import org.openhab.binding.openweathermap.internal.dto.onecallhist.OpenWeatherMapOneCallHistAPIData;
import org.openhab.binding.openweathermap.internal.dto.onecallhist.Rain; import org.openhab.binding.openweathermap.internal.dto.onecallhist.Rain;
import org.openhab.binding.openweathermap.internal.dto.onecallhist.Snow; import org.openhab.binding.openweathermap.internal.dto.onecallhist.Snow;
import org.openhab.core.i18n.CommunicationException;
import org.openhab.core.i18n.ConfigurationException;
import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.QuantityType;
import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.ChannelUID;
@ -90,13 +90,13 @@ public class OpenWeatherMapOneCallHistoryHandler extends AbstractOpenWeatherMapH
@Override @Override
protected boolean requestData(OpenWeatherMapConnection connection) protected boolean requestData(OpenWeatherMapConnection connection)
throws OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException { throws CommunicationException, ConfigurationException {
logger.debug("Update weather and forecast data of thing '{}'.", getThing().getUID()); logger.debug("Update weather and forecast data of thing '{}'.", getThing().getUID());
try { try {
weatherData = connection.getOneCallHistAPIData(location, day); weatherData = connection.getOneCallHistAPIData(location, day);
return true; return true;
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {
logger.debug("JsonSyntaxException occurred during execution: {}", e.getLocalizedMessage(), e); logger.debug("JsonSyntaxException occurred during execution: {}", e.getMessage(), e);
return false; return false;
} }
} }

View File

@ -22,10 +22,10 @@ import java.util.regex.Pattern;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapUVIndexConfiguration; import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapUVIndexConfiguration;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapCommunicationException;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConfigurationException;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection; import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection;
import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonUVIndexData; import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonUVIndexData;
import org.openhab.core.i18n.CommunicationException;
import org.openhab.core.i18n.ConfigurationException;
import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.thing.Channel; import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.ChannelUID;
@ -116,7 +116,7 @@ public class OpenWeatherMapUVIndexHandler extends AbstractOpenWeatherMapHandler
@Override @Override
protected boolean requestData(OpenWeatherMapConnection connection) protected boolean requestData(OpenWeatherMapConnection connection)
throws OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException { throws CommunicationException, ConfigurationException {
logger.debug("Update UV Index data of thing '{}'.", getThing().getUID()); logger.debug("Update UV Index data of thing '{}'.", getThing().getUID());
try { try {
uvindexData = connection.getUVIndexData(location); uvindexData = connection.getUVIndexData(location);
@ -125,7 +125,7 @@ public class OpenWeatherMapUVIndexHandler extends AbstractOpenWeatherMapHandler
} }
return true; return true;
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {
logger.debug("JsonSyntaxException occurred during execution: {}", e.getLocalizedMessage(), e); logger.debug("JsonSyntaxException occurred during execution: {}", e.getMessage(), e);
return false; return false;
} }
} }

View File

@ -27,8 +27,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpResponseException; import org.eclipse.jetty.client.HttpResponseException;
import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapWeatherAndForecastConfiguration; import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapWeatherAndForecastConfiguration;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapCommunicationException;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConfigurationException;
import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection; import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection;
import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonDailyForecastData; import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonDailyForecastData;
import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonHourlyForecastData; import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonHourlyForecastData;
@ -37,6 +35,8 @@ import org.openhab.binding.openweathermap.internal.dto.base.Rain;
import org.openhab.binding.openweathermap.internal.dto.base.Snow; import org.openhab.binding.openweathermap.internal.dto.base.Snow;
import org.openhab.binding.openweathermap.internal.dto.forecast.daily.FeelsLikeTemp; import org.openhab.binding.openweathermap.internal.dto.forecast.daily.FeelsLikeTemp;
import org.openhab.core.config.core.Configuration; import org.openhab.core.config.core.Configuration;
import org.openhab.core.i18n.CommunicationException;
import org.openhab.core.i18n.ConfigurationException;
import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.QuantityType;
import org.openhab.core.thing.Channel; import org.openhab.core.thing.Channel;
@ -163,7 +163,7 @@ public class OpenWeatherMapWeatherAndForecastHandler extends AbstractOpenWeather
@Override @Override
protected boolean requestData(OpenWeatherMapConnection connection) protected boolean requestData(OpenWeatherMapConnection connection)
throws OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException { throws CommunicationException, ConfigurationException {
logger.debug("Update weather and forecast data of thing '{}'.", getThing().getUID()); logger.debug("Update weather and forecast data of thing '{}'.", getThing().getUID());
try { try {
weatherData = connection.getWeatherData(location); weatherData = connection.getWeatherData(location);
@ -173,7 +173,7 @@ public class OpenWeatherMapWeatherAndForecastHandler extends AbstractOpenWeather
if (forecastDays > 0) { if (forecastDays > 0) {
try { try {
dailyForecastData = connection.getDailyForecastData(location, forecastDays); dailyForecastData = connection.getDailyForecastData(location, forecastDays);
} catch (OpenWeatherMapConfigurationException e) { } catch (ConfigurationException e) {
if (e.getCause() instanceof HttpResponseException) { if (e.getCause() instanceof HttpResponseException) {
forecastDays = 0; forecastDays = 0;
Configuration editConfig = editConfiguration(); Configuration editConfig = editConfiguration();
@ -193,7 +193,7 @@ public class OpenWeatherMapWeatherAndForecastHandler extends AbstractOpenWeather
} }
return true; return true;
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {
logger.debug("JsonSyntaxException occurred during execution: {}", e.getLocalizedMessage(), e); logger.debug("JsonSyntaxException occurred during execution: {}", e.getMessage(), e);
return false; return false;
} }
} }

View File

@ -357,6 +357,7 @@ offline.conf-error-not-supported-onecall-number-of-minutes = The 'forecastMinute
offline.conf-error-not-supported-onecall-number-of-hours = The 'forecastHours' parameter must be between 0 and 48. offline.conf-error-not-supported-onecall-number-of-hours = The 'forecastHours' parameter must be between 0 and 48.
offline.conf-error-not-supported-onecall-number-of-days = The 'forecastDays' parameter must be between 0 and 7. offline.conf-error-not-supported-onecall-number-of-days = The 'forecastDays' parameter must be between 0 and 7.
offline.conf-error-not-supported-onecall-number-of-alerts = The 'numberOfAlerts' parameter must be greater than or equals to 0. offline.conf-error-not-supported-onecall-number-of-alerts = The 'numberOfAlerts' parameter must be greater than or equals to 0.
offline.communication-error = An unexpected exception occurred during execution.
# discovery result # discovery result