DEV Community

Cover image for How do you handle improper function arguments?

Posted on

How do you handle improper function arguments?

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?

Discussion (3)

avalander profile image

I find that Either and Maybe (also known as Optional) 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 and Maybe, you might find it interesting.

haidv profile image

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