Issue #59: Show content on weather API error

This commit is contained in:
Dale Davies
2023-03-17 14:04:57 +00:00
parent 5afe3cc636
commit da9d357f1d
6 changed files with 31 additions and 23 deletions

View File

@@ -1 +1 @@
v1.3.1 (1678896002)
v1.3.1 (1679061647)

View File

@@ -132,6 +132,10 @@ export default class Main {
this.eventemitter.emit('show-content');
});
this.eventemitter.on('weather-error', error => {
this.eventemitter.emit('show-content');
});
this.eventemitter.on('clock-updated', clockdata => {
if (this.timeelm != null) {
this.timeelm.innerHTML = clockdata.formatted_time;

View File

@@ -34,28 +34,32 @@ export default class Weather {
}
// Get some data from the weather api...
fetch(apiurl)
.then(response => response.json())
.then(data => {
if (data.error) {
console.error('JUMP ERROR: There was an issue with the OWM API... ' + data.error);
return;
}
if (data.cod === 401) {
.then(response => {
if (response.status === 401) {
console.error('JUMP ERROR: The OWM API key is invalid, check config.php');
this.eventemitter.emit('weather-error');
return;
}
// Determine if we should use the day or night variant of our weather icon.
var daynightvariant = 'night';
if (data.dt > data.sys.sunrise && data.dt < data.sys.sunset) {
daynightvariant = 'day'
}
this.eventemitter.emit('weather-loaded', {
locationcode: data.id,
locationname: data.name,
temp: Math.ceil(data.main.temp) + '&deg;' + (JUMP.metrictemp ? 'C' : 'F'),
description: data.weather[0].main,
iconclass: 'wi-owm-' + daynightvariant + '-' + data.weather[0].id,
timezoneshift: data.timezone*1000,
response.json().then(data => {
console.log(data);
if (data.error) {
console.error('JUMP ERROR: There was an issue with the OWM API... ' + data.error);
this.eventemitter.emit('weather-error');
return;
}
// Determine if we should use the day or night variant of our weather icon.
var daynightvariant = 'night';
if (data.dt > data.sys.sunrise && data.dt < data.sys.sunset) {
daynightvariant = 'day'
}
this.eventemitter.emit('weather-loaded', {
locationcode: data.id,
locationname: data.name,
temp: Math.ceil(data.main.temp) + '&deg;' + (JUMP.metrictemp ? 'C' : 'F'),
description: data.weather[0].main,
iconclass: 'wi-owm-' + daynightvariant + '-' + data.weather[0].id,
timezoneshift: data.timezone*1000,
});
});
})
}

View File

@@ -61,7 +61,7 @@ class Weather extends AbstractAPI {
curl_close($ch);
// If we had an error then return the error message and exit, otherwise return the API response.
if (isset($curlerror)) {
http_response_code(400);
http_response_code(curl_getinfo($ch)['http_code']);
die(json_encode(['error' => $curlerror]));
}
return $response;

View File

@@ -1 +1 @@
<script defer src="{{{wwwurl}}}/assets/js/index.b57ed030906eab7ad01e.min.js"></script>
<script defer src="{{{wwwurl}}}/assets/js/index.4769124eb98e9f4445fe.min.js"></script>