Improve error handling

- Use safeJson() and safePromiseAll() for centralized error handling
- Enhance logging with structured debug flags
This commit is contained in:
Eddy G
2025-06-24 23:10:52 -04:00
parent e472b99b44
commit 8f34aa5139
2 changed files with 39 additions and 19 deletions

View File

@@ -3,7 +3,7 @@
import STATUS from './status.mjs'; import STATUS from './status.mjs';
import { distance as calcDistance } from './utils/calc.mjs'; import { distance as calcDistance } from './utils/calc.mjs';
import { json } from './utils/fetch.mjs'; import { safeJson, safePromiseAll } from './utils/fetch.mjs';
import { temperature as temperatureUnit } from './utils/units.mjs'; import { temperature as temperatureUnit } from './utils/units.mjs';
import { getSmallIcon } from './icons.mjs'; import { getSmallIcon } from './icons.mjs';
import { preloadImg } from './utils/image.mjs'; import { preloadImg } from './utils/image.mjs';
@@ -12,6 +12,7 @@ import WeatherDisplay from './weatherdisplay.mjs';
import { registerDisplay } from './navigation.mjs'; import { registerDisplay } from './navigation.mjs';
import * as utils from './regionalforecast-utils.mjs'; import * as utils from './regionalforecast-utils.mjs';
import { getPoint } from './utils/weather.mjs'; import { getPoint } from './utils/weather.mjs';
import { debugFlag } from './utils/debug.mjs';
// map offset // map offset
const mapOffsetXY = { const mapOffsetXY = {
@@ -80,16 +81,27 @@ class RegionalForecast extends WeatherDisplay {
// get now as DateTime for calculations below // get now as DateTime for calculations below
const now = DateTime.now(); const now = DateTime.now();
// get regional forecasts and observations (the two are intertwined due to the design of api.weather.gov) // get regional forecasts and observations using centralized safe Promise handling
const regionalDataAll = await Promise.all(regionalCities.map(async (city) => { const regionalDataAll = await safePromiseAll(regionalCities.map(async (city) => {
try { try {
const point = city?.point ?? (await getAndFormatPoint(city.lat, city.lon)); const point = city?.point ?? (await getAndFormatPoint(city.lat, city.lon));
if (!point) throw new Error('No pre-loaded point'); if (!point) {
if (debugFlag('verbose-failures')) {
console.warn(`Unable to get Points for '${city.Name ?? city.city}'`);
}
return false;
}
// start off the observation task // start off the observation task
const observationPromise = utils.getRegionalObservation(point, city); const observationPromise = utils.getRegionalObservation(point, city);
const forecast = await json(`https://api.weather.gov/gridpoints/${point.wfo}/${point.x},${point.y}/forecast`); const forecast = await safeJson(`https://api.weather.gov/gridpoints/${point.wfo}/${point.x},${point.y}/forecast`);
if (!forecast) {
if (debugFlag('verbose-failures')) {
console.warn(`Regional Forecast request for ${city.Name ?? city.city} failed`);
}
return false;
}
// get XY on map for city // get XY on map for city
const cityXY = utils.getXYForCity(city, minMaxLatLon.maxLat, minMaxLatLon.minLon, this.weatherParameters.state); const cityXY = utils.getXYForCity(city, minMaxLatLon.maxLat, minMaxLatLon.minLon, this.weatherParameters.state);
@@ -133,8 +145,7 @@ class RegionalForecast extends WeatherDisplay {
utils.buildForecast(forecast.properties.periods[currentPeriod + 2], city, cityXY), utils.buildForecast(forecast.properties.periods[currentPeriod + 2], city, cityXY),
]; ];
} catch (error) { } catch (error) {
console.log(`No regional forecast data for '${city.name ?? city.city}'`); console.error(`Unexpected error getting Regional Forecast data for '${city.name ?? city.city}': ${error.message}`);
console.log(error);
return false; return false;
} }
})); }));
@@ -215,12 +226,19 @@ class RegionalForecast extends WeatherDisplay {
} }
const getAndFormatPoint = async (lat, lon) => { const getAndFormatPoint = async (lat, lon) => {
const point = await getPoint(lat, lon); try {
return { const point = await getPoint(lat, lon);
x: point.properties.gridX, if (!point) {
y: point.properties.gridY, return null;
wfo: point.properties.gridId, }
}; return {
x: point.properties.gridX,
y: point.properties.gridY,
wfo: point.properties.gridId,
};
} catch (error) {
throw new Error(`Unexpected error getting point for ${lat},${lon}: ${error.message}`);
}
}; };
// register display // register display

View File

@@ -1,13 +1,15 @@
import { json } from './fetch.mjs'; import { safeJson } from './fetch.mjs';
import { debugFlag } from './debug.mjs';
const getPoint = async (lat, lon) => { const getPoint = async (lat, lon) => {
try { const point = await safeJson(`https://api.weather.gov/points/${lat.toFixed(4)},${lon.toFixed(4)}`);
return await json(`https://api.weather.gov/points/${lat.toFixed(4)},${lon.toFixed(4)}`); if (!point) {
} catch (error) { if (debugFlag('verbose-failures')) {
console.log(`Unable to get point ${lat}, ${lon}`); console.warn(`Unable to get points for ${lat},${lon}`);
console.error(error); }
return false; return false;
} }
return point;
}; };
export { export {