From 1832f209361c897c5a32b3afc7d49da5e232577e Mon Sep 17 00:00:00 2001 From: gsuarezc <72304747+gsuarezc@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:50:28 +0100 Subject: [PATCH] owmweather: Handle possible exceptions and make conditional shorter. Rewrite the comment in the ChangeLog --- apps/owmweather/ChangeLog | 2 +- apps/owmweather/lib.js | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/apps/owmweather/ChangeLog b/apps/owmweather/ChangeLog index 13b037671..ac8270fe9 100644 --- a/apps/owmweather/ChangeLog +++ b/apps/owmweather/ChangeLog @@ -3,4 +3,4 @@ 0.03: Fix updating weather too often 0.04: Minor code improvements 0.05: Upgrade OWM to One Call API 3.0. Add pressure to weather.json -0.06: Fix One Call API 3.0 does not return city name but it is required by weather app +0.06: Fix One Call API 3.0 not returning city names, which are required by the weather app diff --git a/apps/owmweather/lib.js b/apps/owmweather/lib.js index 6201304f3..3c01b79d4 100644 --- a/apps/owmweather/lib.js +++ b/apps/owmweather/lib.js @@ -1,7 +1,10 @@ function parseWeather(response) { let owmData = JSON.parse(response); - let isOwmData = (owmData.lat && owmData.lon) && owmData.current.weather && owmData.current; + let isOwmData = false; + try { + isOwmData = (owmData.lat && owmData.lon) && owmData.current.weather && owmData.current; + } catch (_e) {} if (isOwmData) { let json = require("Storage").readJSON('weather.json') || {}; @@ -12,9 +15,9 @@ function parseWeather(response) { weather.code = owmData.current.weather[0].id; weather.wdir = owmData.current.wind_deg; weather.wind = owmData.current.wind_speed; - weather.loc = owmData.name ? owmData.name : ""; + weather.loc = owmData.name || ""; weather.txt = owmData.current.weather[0].main; - weather.hpa = owmData.current.pressure ? owmData.current.pressure : 0; + weather.hpa = owmData.current.pressure || 0; if (weather.wdir != null) { let deg = weather.wdir;