From 746eafc57490274b5dce513c323dcbd541b07bf5 Mon Sep 17 00:00:00 2001 From: Gabriel Fosse <67290377+majesticio@users.noreply.github.com> Date: Wed, 10 Apr 2024 13:16:28 -0700 Subject: [PATCH] Refactor fetchData function to handle errors and log them (#1104) * Refactor fetchData function to handle errors and log them * Just added/modified a few log messages --------- Co-authored-by: Gabriel Fosse Co-authored-by: Christian Parker --- src/adapters/mexico.js | 51 +++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/src/adapters/mexico.js b/src/adapters/mexico.js index 77b6a750..b7a92012 100644 --- a/src/adapters/mexico.js +++ b/src/adapters/mexico.js @@ -13,6 +13,8 @@ import { unifyParameters, removeUnwantedParameters, } from '../lib/utils.js'; +import log from '../lib/logger.js'; + import got from 'got'; import { DateTime } from 'luxon'; import async from 'async'; @@ -46,31 +48,40 @@ const gotInstance = got.extend({ export const name = 'mexico'; -export async function fetchData(source, cb) { /** * Fetches the data for a given source and returns an appropriate object * @param {object} source A valid source object * @param {function} cb A callback of the form cb(err, data) */ - const tasks = fetchAllStationSites( - await gotInstance(source.sourceURL).text(), - source.url - ).map((e) => { - return async function () { - const response = await gotInstance(e); - return response.body; - }; - }); - async.parallel(tasks, function (err, results) { - if (err) { - return cb({ message: 'Failure to load data urls.' }); - } - const data = formatData(results); - if (data === undefined) { - return cb({ message: 'Failure to parse data.' }); - } - cb(null, data); - }); +export async function fetchData(source, cb) { + try { + log.debug(`Checking - ${source.sourceURL}`); + const tasks = fetchAllStationSites( + await gotInstance(source.sourceURL).text(), + source.url + ).map((e) => { + return async function () { + const response = await gotInstance(e); + return response.body; + }; + }); + + async.parallel(tasks, function (err, results) { + if (err) { + return cb({ message: 'Failure to load data urls.' }); + } + + const data = formatData(results); + if (data === undefined) { + return cb({ message: 'Failure to parse data.' }); + } + + cb(null, data); + }); + } catch (error) { + log.error(`Error occurred: ${error.message}`); + cb({ message: `An error occurred while fetching the data: ${error.message}` }); + } } /**