Erlang Error Handling when input argument is wrong format

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Erlang Error Handling when input argument is wrong format

WW-2
Hi I have a question about the error handling in erlang when input argument is in wrong format.

example

spec my_fun(Arg:String()) -> term().

In case someone calling my_fun( atom_input), how to handle the error ?

my solution:

my_fun(Arg) when is_string(Arg) -> 
    do_something.

With the guards  the function will only take string(), when input is not a string the interface will generate run_time error   {error, function_fault}


My question: Is my solution good enough ?  Any bettter way to handle the wrong input ?


Best Regards W.W.(KingWang)
Reply | Threaded
Open this post in threaded view
|

Re: Erlang Error Handling when input argument is wrong format

bengt e
Greetings,

Your solution is good enough.

Best Wishes,
bengt


On 4 Mar 2020, at 09:41, WW <[hidden email]> wrote:

Hi I have a question about the error handling in erlang when input argument is in wrong format.

example

spec my_fun(Arg:String()) -> term().

In case someone calling my_fun( atom_input), how to handle the error ?

my solution:

my_fun(Arg) when is_string(Arg) -> 
    do_something.

With the guards  the function will only take string(), when input is not a string the interface will generate run_time error   {error, function_fault}


My question: Is my solution good enough ?  Any bettter way to handle the wrong input ?


Best Regards W.W.(KingWang)

Reply | Threaded
Open this post in threaded view
|

Re: Erlang Error Handling when input argument is wrong format

Yao Bao
In reply to this post by WW-2
Your solution is good enough and beautiful.

Cheers,
Yao

在 2020年3月4日,16:41,WW <[hidden email]> 写道:

Hi I have a question about the error handling in erlang when input argument is in wrong format.

example

spec my_fun(Arg:String()) -> term().

In case someone calling my_fun( atom_input), how to handle the error ?

my solution:

my_fun(Arg) when is_string(Arg) -> 
    do_something.

With the guards  the function will only take string(), when input is not a string the interface will generate run_time error   {error, function_fault}


My question: Is my solution good enough ?  Any bettter way to handle the wrong input ?


Best Regards W.W.(KingWang)

Reply | Threaded
Open this post in threaded view
|

Re: Erlang Error Handling when input argument is wrong format

Jesper Louis Andersen-2
In reply to this post by WW-2
I like this solution as well.

The alternative is something like

my_fun(Arg) when is_string(Arg) ->
  do_something;
my_fun(A) when is_atom(A) ->
  my_fun(atom_to_list(A)).

which coerces the atom to a string. But in my experience, this should be avoided unless the interface really demands that variance. It is better to crash the code and fix it in the caller rather than trying to coerce in the callee. Generally, if you are only accepting a small set of possible erlang terms (i.e., strings), then your code is more robust against sending wrong values. If you coerce/canonicalize terms, you run the risk of converting some value which shouldn't have been and you are now running a program where some data is undesired.

The key is that `my_fun(atom_input)` is a bug in the program. It should be fixed by a programmer, and not saved by the system in an attempt to continue at all cost.

On Wed, Mar 4, 2020 at 9:41 AM WW <[hidden email]> wrote:
Hi I have a question about the error handling in erlang when input argument is in wrong format.

example

spec my_fun(Arg:String()) -> term().

In case someone calling my_fun( atom_input), how to handle the error ?

my solution:

my_fun(Arg) when is_string(Arg) -> 
    do_something.

With the guards  the function will only take string(), when input is not a string the interface will generate run_time error   {error, function_fault}


My question: Is my solution good enough ?  Any bettter way to handle the wrong input ?


Best Regards W.W.(KingWang)


--
J.