Given the method in UserService: update , what's the best way to handle errors / exceptions here?
Option A:
def update(...): Try[User]
Thus, I need to define my custom exceptions and throw them into the function body when necessary. Most of these exceptions are business errors (for example, user_id cannot be changed, etc.). It doesnβt matter what kind of exception (business error, network exception, database database exception, etc.), process them the same way and simply return Failure(err) - let the upper level handle them.
Option B:
def update(...): Either[Error, User]
This is an exception. In the body of the function, it catches all possible exceptions and turns them into Error, and for business errors, it simply returns Left[Error] .
Using Try seems more natural to me as I want to handle errors. Either is a more general thing - Either[Error, T] is just one special case, and I think Try invented for this special case. But I also read that we should avoid using exceptions to handle errors ...
So which solution is better and why?
source share