Skip to content

Commit

Permalink
Merge pull request #342 from CodeForAfrica/fix/error_handling_and_rev…
Browse files Browse the repository at this point in the history
…alidation

Improve error handling and revalidation
  • Loading branch information
kilemensi authored Jul 18, 2024
2 parents 3847cc8 + 3abed70 commit 1ab6694
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion contrib/dokku/Dockerfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
FROM codeforafrica/sensors-africa-ui:0.4.21
FROM codeforafrica/sensors-africa-ui:0.4.22
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sensors.africa",
"description": "sensors.AFRICA",
"version": "0.4.21",
"version": "0.4.22",
"private": true,
"main": "index.js",
"homepage": "https://sensors.africa/",
Expand Down Expand Up @@ -83,5 +83,10 @@
"*.yml": [
"yarn format-staged"
]
}
},
"engines": {
"node": "20.x",
"yarn": "1"
},
"packageManager": "[email protected]"
}
3 changes: 2 additions & 1 deletion src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const P2_READING = "P2";
const formatAirStats = (data, isPm2 = false) => {
const formatted = {};
["average", "maximum", "minimum"].forEach((stat) => {
const parsed = Number.parseFloat(data[stat]);
// Handle cases we have not data
const parsed = Number.parseFloat(data?.[stat]);
if (isPm2 && stat === "average") {
formatted.averageDescription = `measurements not recorded`;
if (!Number.isNaN(parsed)) {
Expand Down
5 changes: 5 additions & 0 deletions src/components/Hambuger/HambugerMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ function HambugerMenu({ handleToggle, menuOpen, ...props }) {
</Grid>
<Grid item xs={12}>
<Modal
BackdropProps={{
style: {
backgroundColor: "rgba(0,0,0,0.8)",
},
}}
className={classes.modalContent}
open={menuOpen}
onClose={handleToggle}
Expand Down
13 changes: 9 additions & 4 deletions src/pages/air/city/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,19 @@ export async function getStaticProps({ params: { id: city } }) {
const airRes = await API.getAirData(city);
const weeklyP2Res = await API.getWeeklyP2Data(city);
if (!(airRes.ok || weeklyP2Res.ok)) {
return {
notFound: true,
};
// Throw an error instead of returning so that the cache is not updated
// until the next successful request.
const status = `air: ${airRes.status}, weeklyP2: ${weeklyP2Res.status}`;
throw new Error(`Failed to fetch data, received status ${status}`);
}
const air = (await airRes.json()) || {};
const weeklyP2 = (await weeklyP2Res.json()) || {};
const data = { air, weeklyP2 };
return { props: { city, data }, revalidate: 5 * 60 * 60 };
return {
props: { city, data },
// API update data every 5 mins
revalidate: 5 * 60, // in seconds
};
}

export default City;

0 comments on commit 1ab6694

Please sign in to comment.