which approach is better for handling errors in my app
I am working on a chat app the user can add friends i am checking whether the recipent of the friend request is already in the user's friends list or not and i'm checking if the user attempts to send a friend request to himself i am currently handling these cases by return an object with success property of false and a message indicating the error , is that the right approach ? or should i throw an Error with the same messages i already have and handle it with try and catch statements .
2 Replies
depending on the context but i think you got it right
In general, reserve throwing errors for cases that are truly exceptional or need to be handled by the application as a whole, like server issues or unhandled conditions, while for validation or business logic checks (like friend requests), returning an object with a success flag and message is typically more appropriate.