Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor fetchData function to handle errors and log them #1104

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading