Skip to content

Commit e6847e5

Browse files
authored
Added basic error handling to data fetch in emissions script
2 parents 668d8e4 + 7891414 commit e6847e5

1 file changed

Lines changed: 98 additions & 88 deletions

File tree

‎data/functions/generate_average_co2.js‎

Lines changed: 98 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -51,109 +51,119 @@ const type = "average";
5151
// Save a minified version of the JS file to the src/data folder
5252

5353
(async () => {
54-
const response = await fetch(sourceURL);
55-
const data = await response.json();
56-
57-
/**
58-
* Groups the API data by country code.
59-
* @type {Object.<string, Array>}
60-
*/
61-
const groupedData = await data.reduce((acc, item) => {
62-
const key =
63-
item.country_code === "" ? item.country_or_region : item.country_code;
64-
if (!acc[key]) {
65-
acc[key] = [];
54+
try {
55+
const response = await fetch(sourceURL);
56+
if (!response.ok) {
57+
throw new Error(`Network response was not ok: ${response.statusText}`);
6658
}
67-
acc[key].push(item);
68-
return acc;
69-
}, {});
70-
71-
/**
72-
* Extracts the latest year's data for each country.
73-
* @type {Object.<string, Object>}
74-
*/
75-
const latestData = await Object.keys(groupedData).reduce((acc, key) => {
76-
// Find the last year in the array with emissions intensity data
77-
const latestYear = groupedData[key].reduce((acc, item, index) => {
59+
const data = await response.json();
60+
61+
/**
62+
* Groups the API data by country code.
63+
* @type {Object.<string, Array>}
64+
*/
65+
const groupedData = await data.reduce((acc, item) => {
66+
const key =
67+
item.country_code === "" ? item.country_or_region : item.country_code;
68+
if (!acc[key]) {
69+
acc[key] = [];
70+
}
71+
acc[key].push(item);
72+
return acc;
73+
}, {});
74+
75+
/**
76+
* Extracts the latest year's data for each country.
77+
* @type {Object.<string, Object>}
78+
*/
79+
const latestData = await Object.keys(groupedData).reduce((acc, key) => {
80+
// Find the last year in the array with emissions intensity data
81+
const latestYear = groupedData[key].reduce((acc, item, index) => {
82+
if (
83+
item.emissions_intensity_gco2_per_kwh === null ||
84+
item.emissions_intensity_gco2_per_kwh === ""
85+
) {
86+
return acc;
87+
}
88+
return index;
89+
}, 0);
90+
91+
acc[key] = groupedData[key][latestYear];
92+
return acc;
93+
}, {});
94+
95+
// Loop through the data and extract the emissions intensity data
96+
// Save it to the gridIntensityResults object with the country code as the key
97+
Object.values(latestData).forEach((row) => {
7898
if (
79-
item.emissions_intensity_gco2_per_kwh === null ||
80-
item.emissions_intensity_gco2_per_kwh === ""
99+
row.emissions_intensity_gco2_per_kwh === null ||
100+
row.emissions_intensity_gco2_per_kwh === ""
81101
) {
82-
return acc;
102+
return;
83103
}
84-
return index;
85-
}, 0);
86-
87-
acc[key] = groupedData[key][latestYear];
88-
return acc;
89-
}, {});
90-
91-
// Loop through the data and extract the emissions intensity data
92-
// Save it to the gridIntensityResults object with the country code as the key
93-
Object.values(latestData).forEach((row) => {
94-
if (
95-
row.emissions_intensity_gco2_per_kwh === null ||
96-
row.emissions_intensity_gco2_per_kwh === ""
97-
) {
98-
return;
99-
}
100104

101-
const country =
102-
row.country_code === "" ? row.country_or_region : row.country_code;
105+
const country =
106+
row.country_code === "" ? row.country_or_region : row.country_code;
103107

104-
gridIntensityResults[country.toUpperCase()] =
105-
row.emissions_intensity_gco2_per_kwh;
108+
gridIntensityResults[country.toUpperCase()] =
109+
row.emissions_intensity_gco2_per_kwh;
106110

107-
generalResults[country] = {
108-
country_code: row.country_code,
109-
country_or_region: row.country_or_region,
110-
year: row.year,
111-
emissions_intensity_gco2_per_kwh: row.emissions_intensity_gco2_per_kwh,
112-
};
113-
});
111+
generalResults[country] = {
112+
country_code: row.country_code,
113+
country_or_region: row.country_or_region,
114+
year: row.year,
115+
emissions_intensity_gco2_per_kwh: row.emissions_intensity_gco2_per_kwh,
116+
};
117+
});
114118

115-
const jsDocComments = `/**
119+
// Ensure directories exist
120+
fs.mkdirSync("data/output", { recursive: true });
121+
fs.mkdirSync("src/data", { recursive: true });
122+
123+
const jsDocComments = `/**
116124
* @fileoverview Minified average CO2 emissions intensity data for countries.
117125
* @generated Generated by generate_average_co2.js
118126
* @version 1.0.0
119127
*/
120-
121128
/**
122129
* @constant {Object.<string, number>} data - Average CO2 emissions intensity data for various countries.
123130
* @constant {string} type - Type of data being represented.
124131
*/`;
125132

126-
/**
127-
* Saves the country code and emissions data for use in the CO2.js library.
128-
* @type {void}
129-
*/
130-
fs.writeFileSync(
131-
"data/output/average-intensities.js",
132-
`
133-
const data = ${JSON.stringify(gridIntensityResults, null, " ")};
134-
const type = "${type}";
135-
export { data, type };
136-
export default { data, type };
137-
`
138-
);
139-
/**
140-
* Saves a minified version of the data for easy import into the library.
141-
* @type {void}
142-
*/
143-
fs.writeFileSync(
144-
"src/data/average-intensities.min.js",
145-
`${jsDocComments}
146-
const data = ${JSON.stringify(
147-
gridIntensityResults
148-
)}; const type = "${type}"; export { data, type }; export default { data, type };`
149-
);
150-
151-
/**
152-
* Saves the full data set as a JSON file for reference.
153-
* @type {void}
154-
*/
155-
fs.writeFileSync(
156-
"data/output/average-intensities.json",
157-
JSON.stringify(generalResults, null, " ")
158-
);
133+
/**
134+
* Saves the country code and emissions data for use in the CO2.js library.
135+
* @type {void}
136+
*/
137+
fs.writeFileSync(
138+
"data/output/average-intensities.js",
139+
`const data = ${JSON.stringify(gridIntensityResults, null, " ")};
140+
const type = "${type}";
141+
export { data, type };
142+
export default { data, type };
143+
`
144+
);
145+
146+
/**
147+
* Saves a minified version of the data for easy import into the library.
148+
* @type {void}
149+
*/
150+
fs.writeFileSync(
151+
"src/data/average-intensities.min.js",
152+
`${jsDocComments}
153+
const data = ${JSON.stringify(
154+
gridIntensityResults
155+
)}; const type = "${type}"; export { data, type }; export default { data, type };`
156+
);
157+
158+
/**
159+
* Saves the full data set as a JSON file for reference.
160+
* @type {void}
161+
*/
162+
fs.writeFileSync(
163+
"data/output/average-intensities.json",
164+
JSON.stringify(generalResults, null, " ")
165+
);
166+
} catch (error) {
167+
console.error("Error fetching or processing data:", error);
168+
}
159169
})();

0 commit comments

Comments
 (0)