Skip to content

Commit 8d43aaa

Browse files
author
veeck
committed
handle errors from weatherprovider
for now only in openmeteo
1 parent 52afe0a commit 8d43aaa

File tree

6 files changed

+43
-50
lines changed

6 files changed

+43
-50
lines changed

modules/default/weather/current.njk

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,8 @@
9090
{% if config.showHumidity === "below" %}
9191
<span class="medium dimmed">{{ humidity() }}</span>
9292
{% endif %}
93-
{% elseif error %}
94-
<div class="small dimmed">
95-
{{ "MODULE_CONFIG_ERROR" | translate({MODULE_NAME: "Weather", ERROR: error}) | safe }}
96-
</div>
93+
{% elseif error %}
94+
<div class="small dimmed">{{ "MODULE_CONFIG_ERROR" | translate({MODULE_NAME: "Weather", ERROR: error}) | safe }}</div>
9795
{% else %}
9896
<div class="dimmed light small">{{ "LOADING" | translate }}</div>
9997
{% endif %}

modules/default/weather/forecast.njk

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@
3939
{% set currentStep = currentStep + 1 %}
4040
{% endfor %}
4141
</table>
42-
{% elseif error %}
43-
<div class="small dimmed">
44-
{{ "MODULE_CONFIG_ERROR" | translate({MODULE_NAME: "Weather", ERROR: error}) | safe }}
45-
</div>
42+
{% elseif error %}
43+
<div class="small dimmed">{{ "MODULE_CONFIG_ERROR" | translate({MODULE_NAME: "Weather", ERROR: error}) | safe }}</div>
4644
{% else %}
4745
<div class="dimmed light small">{{ "LOADING" | translate }}</div>
4846
{% endif %}

modules/default/weather/hourly.njk

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@
4141
{% set currentStep = currentStep + 1 %}
4242
{% endfor %}
4343
</table>
44-
{% elseif error %}
45-
<div class="small dimmed">
46-
{{ "MODULE_CONFIG_ERROR" | translate({MODULE_NAME: "Weather", ERROR: error}) | safe }}
47-
</div>
44+
{% elseif error %}
45+
<div class="small dimmed">{{ "MODULE_CONFIG_ERROR" | translate({MODULE_NAME: "Weather", ERROR: error}) | safe }}</div>
4846
{% else %}
4947
<div class="dimmed light small">{{ "LOADING" | translate }}</div>
5048
{% endif %}

modules/default/weather/providers/openmeteo.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -143,56 +143,41 @@ WeatherProvider.register("openmeteo", {
143143
},
144144

145145
fetchCurrentWeather () {
146-
this.fetchData(this.getUrl())
146+
return this.fetchData(this.getUrl())
147147
.then((data) => this.parseWeatherApiResponse(data))
148148
.then((parsedData) => {
149149
if (!parsedData) {
150-
// No usable data?
151-
return;
150+
throw new Error("No usable data ...");
152151
}
153-
154152
const currentWeather = this.generateWeatherDayFromCurrentWeather(parsedData);
155153
this.setCurrentWeather(currentWeather);
156154
})
157-
.catch(function (request) {
158-
Log.error("Could not load data ... ", request);
159-
})
160155
.finally(() => this.updateAvailable());
161156
},
162157

163158
fetchWeatherForecast () {
164-
this.fetchData(this.getUrl())
159+
return this.fetchData(this.getUrl())
165160
.then((data) => this.parseWeatherApiResponse(data))
166161
.then((parsedData) => {
167162
if (!parsedData) {
168-
// No usable data?
169-
return;
163+
throw new Error("No usable data ...");
170164
}
171-
172165
const dailyForecast = this.generateWeatherObjectsFromForecast(parsedData);
173166
this.setWeatherForecast(dailyForecast);
174167
})
175-
.catch(function (request) {
176-
Log.error("Could not load data ... ", request);
177-
})
178168
.finally(() => this.updateAvailable());
179169
},
180170

181171
fetchWeatherHourly () {
182-
this.fetchData(this.getUrl())
172+
return this.fetchData(this.getUrl())
183173
.then((data) => this.parseWeatherApiResponse(data))
184174
.then((parsedData) => {
185175
if (!parsedData) {
186-
// No usable data?
187-
return;
176+
throw new Error("No usable data ...");
188177
}
189-
190178
const hourlyForecast = this.generateWeatherObjectsFromHourly(parsedData);
191179
this.setWeatherHourly(hourlyForecast);
192180
})
193-
.catch(function (request) {
194-
Log.error("Could not load data ... ", request);
195-
})
196181
.finally(() => this.updateAvailable());
197182
},
198183

modules/default/weather/weather.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Module.register("weather", {
4747
// Module properties.
4848
weatherProvider: null,
4949

50+
error: null,
51+
5052
// Can be used by the provider to display location of event if nothing else is specified
5153
firstEvent: null,
5254

@@ -148,6 +150,12 @@ Module.register("weather", {
148150
// Skip some hourly forecast entries if configured
149151
const hourlyData = this.weatherProvider.weatherHourly()?.filter((e, i) => (i + 1) % this.config.hourlyForecastIncrements === this.config.hourlyForecastIncrements - 1);
150152

153+
if (this.error) {
154+
return {
155+
error: this.error
156+
};
157+
}
158+
151159
return {
152160
config: this.config,
153161
current: currentData,
@@ -194,20 +202,26 @@ Module.register("weather", {
194202
nextLoad = delay;
195203
}
196204

197-
setTimeout(() => {
198-
switch (this.config.type.toLowerCase()) {
199-
case "current":
200-
this.weatherProvider.fetchCurrentWeather();
201-
break;
202-
case "hourly":
203-
this.weatherProvider.fetchWeatherHourly();
204-
break;
205-
case "daily":
206-
case "forecast":
207-
this.weatherProvider.fetchWeatherForecast();
208-
break;
209-
default:
210-
Log.error(`Invalid type ${this.config.type} configured (must be one of 'current', 'hourly', 'daily' or 'forecast')`);
205+
setTimeout(async () => {
206+
try {
207+
switch (this.config.type.toLowerCase()) {
208+
case "current":
209+
await this.weatherProvider.fetchCurrentWeather();
210+
break;
211+
case "hourly":
212+
this.weatherProvider.fetchWeatherHourly();
213+
break;
214+
case "daily":
215+
case "forecast":
216+
this.weatherProvider.fetchWeatherForecast();
217+
break;
218+
default:
219+
Log.error(`Invalid type ${this.config.type} configured (must be one of 'current', 'hourly', 'daily' or 'forecast')`);
220+
}
221+
this.error = null;
222+
} catch (error) {
223+
this.error = error;
224+
this.updateAvailable();
211225
}
212226
}, nextLoad);
213227
},

modules/default/weather/weatherprovider.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ const WeatherProvider = Class.extend({
4141

4242
// This method should start the API request to fetch the current weather.
4343
// This method should definitely be overwritten in the provider.
44-
fetchCurrentWeather () {
44+
async fetchCurrentWeather () {
4545
Log.warn(`Weather provider: ${this.providerName} does not subclass the fetchCurrentWeather method.`);
4646
},
4747

4848
// This method should start the API request to fetch the weather forecast.
4949
// This method should definitely be overwritten in the provider.
50-
fetchWeatherForecast () {
50+
async fetchWeatherForecast () {
5151
Log.warn(`Weather provider: ${this.providerName} does not subclass the fetchWeatherForecast method.`);
5252
},
5353

5454
// This method should start the API request to fetch the weather hourly.
5555
// This method should definitely be overwritten in the provider.
56-
fetchWeatherHourly () {
56+
async fetchWeatherHourly () {
5757
Log.warn(`Weather provider: ${this.providerName} does not subclass the fetchWeatherHourly method.`);
5858
},
5959

0 commit comments

Comments
 (0)