Handle badarg in Erlang

萝らか妹 提交于 2020-01-04 01:18:07

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/25199963/handle-badarg-in-erlang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!