在Erlang处理badarg [英] Handle badarg in Erlang
本文介绍了在Erlang处理badarg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Prefix = binary:bin_to_list(wh_json:get_ne_value(<prefix>>> ;,模式)),
其中模式
是:
Pattern1- - > {[{<< 前缀 >>,<<>>},{<< CALLERID >>,<< 1001 >>} ,{<cid_regex>>,<^ \\ +?1001>>>}]}
/ pre>
Pattern2 - > {[{<< 前缀 >>,<< 12 >>},{<< CALLERID >>,<< 1001 > >},{<cid_regex>>,<^ \\ +?1001>>}}}
for
Pattern2
它可以正常工作,但对于Pattern1
我收到此错误,因为前缀在Pattern1
中没有任何值。
所以,任何人都可以告诉我如何处理这种情况,其中前缀值可以是
null
或任何值,它应该适用于这两个条件。解决方案检查
wh_json:get_ne_value
返回未定义
之前调用binary:bin_to_list
:前缀=
case wh_json:get_ne_value(<前缀>> ;,模式)
undefined - >
prefix_not_found;
当is_binary(BinaryPrefix)时,BinaryPrefix - >
binary:bin_to_list(BinaryPrefix)
end
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 forPattern1
I am getting this error because prefix does not have any value inPattern1
.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.解决方案Check whether
wh_json:get_ne_value
returnsundefined
before callingbinary: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
这篇关于在Erlang处理badarg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文