Next.js Discord

Discord Forum

Try Catch

Answered
(-.-) posted this in #help-forum
Open in Discord
Original message was deleted.
Answered by Pearls
It is not necessary, but it is recommended to add some sort of error control. either using a catch statement after the fetch (fetch().catch) or just a full on try/catch.
View full answer

10 Replies

It is not necessary, but it is recommended to add some sort of error control. either using a catch statement after the fetch (fetch().catch) or just a full on try/catch.
Answer
When I use try catch it doesn't catch 400 errors, should I throw an exception manually?
I want to be able to activate error.js in catch and also send this error to a log service
@(-.-) Click to see attachment
this code would appear to be correct, let me explain it a little for your own understanding.

The !res.ok checks if the response status code is not a success status code (200-299), if it isnt it will throw an exception. The thing with this is that it doesnt check for network errors or parsing errors, thats where your try/catch statement comes in and catches those errors and logs them to console.
Yes thats correct
Thanks a lot for the help.
Masai Lion
Hi