Handle badarg in Erlang
Asked Answered
R

1

3

I am very new to the Erlang and I am getting badarg error when I try to convert binary to string as shown below.

Prefix = binary:bin_to_list(wh_json:get_ne_value(<<"prefix">>, Patterns)),

where Patterns are:

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 prefix does not have any value in Pattern1.

So, can any one tell me how I can handle this situation where prefix value can be null or any value, it should work for both the conditions.

Reservist answered 8/8, 2014 at 9:18 Comment(0)
R
5

Check whether wh_json:get_ne_value returns 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
Raglan answered 8/8, 2014 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.