Let's say I have to develop an API which will talk to the database and will have some methods to do CRUD operations. Let's say I have a method that fetches a List of something based on some criteria:
def fetchUsers(criteria: Criteria): List[User] = ???
If I cannot find any users for the given Criteria, should I return an empty List or it is a good practice to return a Try[List[User]] and if I do not find any users, I return a Failure?
What is considered a good practice?
Try[NonEmptyList[User]].List[User]and let the caller decide how to handle the empty case.Try[_]only if there's value in having the calling code handle the error case. i.e., the calling code wants to be given a chance to do something in case of error (log it, present a message to the user, retry, etc). For a lot of cases though (db connection error, out of space, etc) there's not much the calling code can do (true exceptional case), and I think it's better to use an exception that is then caught at the top.