When sending a request to another module and expecting a result, it seems to me there are two ways of dealing with the 'non-happy paths'.
- Throw an exception
- Return a result object that wraps different results (such as value and error)
I would say the first one seems better in general. It keeps the code clean and readable. If you expect the result to be correct, just throw an exception to handle happy path divergence.
But what when you have no clue what the result will be?
For example calling a module that validates a lottery ticket. The happy path would be that you won, but it probably won't be. (As pointed out by @Ben Cottrell in the comments, "not winning" is also the happy path, maybe not for the end user, though)
Would it be better to consider that the happy path is getting a result from the LotteryTicketValidator
and just handle exceptions for when the ticket could not be processed?
Another one could be user authentication when logging in. Can we assume that the user entered the correct credentials and throw an exception when the credentials are invalid, or should we expect to get some sort of LoginResult
object?