Skip to content

Commit

Permalink
Refactor fetchData function to handle errors and log them (#1104)
Browse files Browse the repository at this point in the history
* Refactor fetchData function to handle errors and log them

* Just added/modified a few log messages

---------

Co-authored-by: Gabriel Fosse <[email protected]>
Co-authored-by: Christian Parker <[email protected]>
  • Loading branch information
3 people authored Apr 10, 2024
1 parent a9a6c40 commit 746eafc
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions src/adapters/mexico.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}` });
}
}

/**
Expand Down

0 comments on commit 746eafc

Please sign in to comment.