Skip to content

Commit

Permalink
Refactor fetchData function to handle errors and log them
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Fosse committed Apr 10, 2024
1 parent a9a6c40 commit 17e36ef
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 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,39 @@ 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 {
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);
cb({ message: 'An error occurred while fetching the data.' });
}
}

/**
Expand Down

0 comments on commit 17e36ef

Please sign in to comment.