Badarg handle in Erlang
I am very new to Erlang and I am getting a badarg error when I try to convert binary to string like below.
Prefix = binary:bin_to_list(wh_json:get_ne_value(<<"prefix">>, Patterns)),
where Patterns
:
Pattern1--> {[{<<"prefix">>,<<>>},{<<"callerId">>,<<"1001">>},{<<"cid_regex">>,<<"^\\+?1001">>}]}
Pattern2--> {[{<<"prefix">>,<<"12">>},{<<"callerId">>,<<"1001">>},{<<"cid_regex">>,<<"^\\+?1001">>}]}
for Pattern2
it works fine, but for Pattern1
i am getting this error because the prefix doesn't matter in Pattern1
.
So, can anyone tell me how I can handle this situation where the prefix value can be null
or any value, it should work for both conditions.
Make sure to wh_json:get_ne_value
return undefined
before calling binary:bin_to_list
:
Prefix =
case wh_json:get_ne_value(<<"prefix">>, Patterns) of
undefined ->
prefix_not_found;
BinaryPrefix when is_binary(BinaryPrefix) ->
binary:bin_to_list(BinaryPrefix)
end