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

Avatar
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
Avatar
When I use try catch it doesn't catch 400 errors, should I throw an exception manually?
Image
I want to be able to activate error.js in catch and also send this error to a log service
Avatar
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.
Avatar
I understand, so within the catch block I could send this error to a log service?
In other words, I don't need to return errors within catch? Can I just return them in the console?
Avatar
Yes thats correct
Avatar
Thanks a lot for the help.
Avatar
Masai Lion
Hi