Next.js Discord

Discord Forum

Catch AggregateError ECONNREFUSED

Answered
Japanese Bobtail posted this in #help-forum
Open in Discord
Japanese BobtailOP
I'm using this async function to fetch my api data. How can i catch the error if the api is not available? "[TypeError: fetch failed] [cause]: [AggregateError: ] { code: 'ECONNREFUSED' }" its always returning "OK" from the try block.

try { const res = await fetch(URL, { next: { revalidate: 3600 }, ...headers, }); console.log('OK'); } catch (err) { console.log('ERROR'); }
Answered by Japanese Bobtail
I got it. Its a problem with chromes cache. If i disable the browser cache, it works and nextjs fires the correct server error [ Server ] Error: fetch failed
View full answer

13 Replies

@B33fb0n3 The request will be always available (as you create it) and the result can be something. So check the „res.ok“ to see if the request was ok or not. You can check the „res.status“ as well
Japanese BobtailOP
I've already checked the "res.ok" - its true. So the error still exists.

export const getPage = async (slug) => { const res = await fetch(URL, { next: { revalidate: 3600 }, ...headers, }); if (!res.ok) return false; const json = await parseJson(res); if (!json.length || !json[0].id) return false; return json[0]; };
If i check the json[0] object, i got my data. Maybe its a caching problem?
@Japanese Bobtail If i check the json[0] object, i got my data. Maybe its a caching problem?
it looks more like the external API is shit. Yea, then check the json data if there is an error
Japanese BobtailOP
Its not an api problem because the api is not online.
@Japanese Bobtail Its not an api problem because the api is not online.
maybe that's the issue. But when the api is offline, how do you get an response from the api?
Japanese BobtailOP
Yes thats the issue. But my question is, how can i catch this? If i console the res i got the correct json object. So i very confused. On one side the res return data on the other side i got the aggregate error.
I am so confused right now... how do you get data from your API, when your API can't return data, because it's offline?
@B33fb0n3 I am so confused right now... how do you get data from your API, when your API can't return data, because it's offline?
Japanese BobtailOP
Yes i am confused too. Here is the code ... the cosole logs > true, 200 and a complete object with the correct data... and the editor console logs are at the end of my text.

export const getPage = async (slug) => { const res = await fetch(${process.env.API_URL}/page/${slug}, { next: { revalidate: 3600 }, ...headers, }); if (!res.ok) return false; const json = await parseJson(res); if (!json.length || !json[0].id) return false; console.log(res.ok, res.status, json[0]); return json[0]; };

[TypeError: fetch failed] {
[cause]: [AggregateError: ] { code: 'ECONNREFUSED' }
}
[TypeError: fetch failed] {
[cause]: [AggregateError: ] { code: 'ECONNREFUSED' }
}
⨯ unhandledRejection: [TypeError: fetch failed] {
[cause]: [AggregateError: ] { code: 'ECONNREFUSED' }
}
⨯ unhandledRejection: [TypeError: fetch failed] {
[cause]: [AggregateError: ] { code: 'ECONNREFUSED' }
}
⨯ [Error: failed to pipe response] {
[cause]: [TypeError: fetch failed] {
[cause]: [AggregateError: ] { code: 'ECONNREFUSED' }
}
}
Japanese BobtailOP
I got it. Its a problem with chromes cache. If i disable the browser cache, it works and nextjs fires the correct server error [ Server ] Error: fetch failed
Answer