Hi alchemists!
I have a question about best practices for returning values from a function that just forwards what another inner function does.
I wonder what are the best practices for handling cases like this. Should I just forward the returned value from the wrapped function or should I treat the returning cases even when by contract they are the same as the wrapping function?
@type on_work_fun :: {:ok, result} | {:error, term()}
@spec work_fun(any()) :: on_work_fun
def work_fun(arg) do
...
end
Then in another module…
@type on_wrapper_func :: {:ok, result} | {:error, term()}
@spec wrapper_fun(any()) :: on_wrapper_fun
# V1, just forward the wrapped function result (relies on the contract to be respected in the future)
def wrapper_fun(arg) do
work_fun(arg)
end
# Keeps this function contract in any case
def wrapper_fun(arg) do
case work_fun(arg) do
{:ok, result} -> {:ok, result}
{:error, reason} -> {:error, reason}
error -> {:error, error}
end
end