Skip to content

Commit c734c05

Browse files
committed
feat: safeguard against invalid JSON
It appears that OpenWeatherMap sometimes sent out bad JSON. Learn from the lesson and do better in this PR. Signed-off-by: André Jaenisch <[email protected]>
1 parent 39d72e4 commit c734c05

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

weather-widget/weather.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ local function worker(user_args)
180180
local timeout = args.timeout or 120
181181

182182
local ICONS_DIR = WIDGET_DIR .. '/icons/' .. icon_pack_name .. '/'
183-
local weather_api =
183+
local weather_api =
184184
('https://api.weatherapi.com/v1/current.json' ..
185185
'?q=' .. coordinates[1] .. ',' .. coordinates[2] .. '&key=' .. api_key ..
186186
'&units=' .. units .. '&lang=' .. LANG)
@@ -322,13 +322,24 @@ local function worker(user_args)
322322
return
323323
end
324324

325+
if string.match(stdout, '<') ~= nil then
326+
if not warning_shown then
327+
warning_shown = true
328+
widget:is_ok(false)
329+
tooltip:add_to_object(widget)
330+
331+
widget:connect_signal('mouse::enter', function() tooltip.text = stdout end)
332+
end
333+
return
334+
end
335+
325336
warning_shown = false
326337
tooltip:remove_from_object(widget)
327338
widget:is_ok(true)
328339

329340
local result = json.decode(stdout)
330341
widget:set_image(ICONS_DIR .. icon_map[result.current.condition.code] .. icons_extension)
331-
-- TODO: if units isn't "metric", read temp_f instead
342+
-- TODO: if units isn't "metric", read temp_f instead
332343
widget:set_text(gen_temperature_str(result.current.temp_c, '%.0f', both_units_widget, units))
333344

334345
current_weather_widget:update(result.current)

0 commit comments

Comments
 (0)