✅ How to handle errors in services/controllers
Hi! I do most of work in services and then call the method in the controller. What's the proper way to handle errors that occur in service? I need return error to the frontend (the error model must be consistent). Is it fine to throw error in the services or is there a better way?
Here is example of
AccountService
method
6 Replies
Just make sure the exceptions make sense (so no generic
Exception
) and handled them in the exception-handling middleware
Basically, throw a custom NotFoundException
or a ConflictException
Then, in the exception-handling middleware do something like
Otherwise, any exception you throw will result in 500 Internal Server Error
okay, thank you
Alternatively, use some sort of a result pattern
I'll check it and decide which one is more suitable
$close if you have no further questions :)
Use the /close command to mark a forum thread as answered