Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling in API routes #85

Open
jamieparkinson opened this issue Mar 31, 2023 · 0 comments
Open

Error handling in API routes #85

jamieparkinson opened this issue Mar 31, 2023 · 0 comments

Comments

@jamieparkinson
Copy link

I noticed that in your API routes you're explicitly returning 500 errors - like here, for example

} catch (error) {
console.error(error.message)
res.status(500).json({ error: 'Something went wrong' })
}

A piece of advice that has stuck with me is never to return a 500 on purpose, or at the very least only to return a 500 if you really have no idea what to do with it, as it's for the case that "the server has encountered a situation it does not know how to handle".

You're already using 405 nicely for if the user is doing something other than a PUT - some other descriptive errors might be a 400 if the date isn't in the right format, a 404 if the task with the given ID can't be found, or a 418 if the server is a teapot. Then if you catch an error and you still can't work out what it's about - it's time for a 500! I think the server will return a 500 for you if there's an uncaught error, so you still don't necessarily need to handle it yourselves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant