I have multiple API calls in the getStaticProps. I want handle errors for each API call individually. How can I do that?
export async function getStaticProps(context) {
const fund = context.params.fund;
const avggReturnsRes = await fetch(
https://localhost:8000/avggaut/?name=${fund}
);
const avggReturnsResult = await avggReturnsRes.json();
const navPlotRes = await fetch(
https://localhost:8000/navplot/?name=${fund}
);
const navPlotResult = await navPlotRes.json();
return {
props: {
fund: fund,
avggReturnsResult: avggReturnsResult,
navPlotResult: navPlotResult,
},
};
}
wrap your fetch calls in a
try-catchblock and, generally if the first request fails then control directly jumps tocatchblock without executing the second fetchIf you really want to separate each request you can put them in separate
try-catchblocks, although there is no reason to do that sincetry-catchcatches only one error at a time, nevertheless, you can do this