I've recently been thinking about how to improve my code patterns to give my work a more consistent and easy to understand feel.
Working functionally means that every function must return a response. If a function was to receive incorrect arguments, then how should it respond?
Returning false
for any anomalous behaviour (invalid input/error catch) and handling that in the parent is the best solution I have come up with.
How do you handle this scenario?
Top comments (2)
I find that
Either
andMaybe
(also known asOptional
) are good ways of handling functions that can return a successful result or a failure. I tend to avoid throwing errors in Javascript because then both programmatic and application errors fall in the same bag and if I want to handle some errors in different places than others I need to catch and rethrow, polluting the error stack.A while ago I wrote an article about handling errors with
Either
andMaybe
, you might find it interesting.Handling errors with Either
Avalander ・ Apr 8 '18 ・ 6 min read
There should be a validate function to validate your initial input and if the input is invalid, the system should throw a meaningful error for end-user, like BadRequestError. That's my idea