Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save Twilio errors information #758

Merged
merged 5 commits into from
Nov 4, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions app/helpers/call_log_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ def call_log_fail_info(call_log, link=true)
return nil if call_log.fail_reason.nil?
info = content_tag(:span, "#{call_log.fail_reason.capitalize}. ")
if call_log.fail_code
fail_details = call_log.fail_details ? "#{call_log.fail_details} (#{call_log.fail_code})" : "Code #{call_log.fail_code}."
info << link_to_if(link, fail_details, error_code_url(call_log.fail_code), class: 'call-log-fail-info')
fail_details = call_log.fail_details ? "#{call_log.fail_details} (#{call_log.fail_code.slice(/\w+:(.+)/, 1)})" : "Code #{call_log.fail_code}."
url = error_code_url(call_log.fail_code)
info << link_to_if(link && url, fail_details, url, class: 'call-log-fail-info')
elsif call_log.fail_details
info << content_tag(:span, "#{call_log.fail_details}.")
end
info
end

def error_code_url(code)
url = "https://github.com/instedd/verboice/wiki/Error-codes"
url << "##{code.gsub(':', '').downcase}" if code
url
if code.downcase.starts_with?("twilio:")
"https://www.twilio.com/docs/errors/#{code.slice(/twilio:(.+)/, 1)}"
elsif code.downcase.starts_with?("isdn:")
"https://github.com/instedd/verboice/wiki/Error-codes##{code.gsub(':', '').downcase}"
end
end
end

6 changes: 5 additions & 1 deletion broker/src/asterisk/asterisk_pbx_log_srv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ handle_cast({varset, Event}, State=#state{guid=Guid}) ->

handle_cast({hangup, Event}, State=#state{guid=Guid,session_id=SessionId}) ->
Reason = proplists:get_value('cause-txt', Event),
Code = proplists:get_value(cause, Event),
Code = case proplists:get_value(cause, Event) of
undefined -> undefined;
<<"0">> -> undefined;
C -> "ISDN:" ++ binary_to_list(C)
end,
create_log(Guid, ["Channel hangup. Reason: ", Reason]),
call_log_srv:hangup(SessionId, {Code, Reason}),
{stop, normal, State};
Expand Down
7 changes: 1 addition & 6 deletions broker/src/call_log_srv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,7 @@ handle_cast({hangup, {_Code, <<"Unknown">>}}, State = #state{timeout = Timeout})
{noreply, State, Timeout};

handle_cast({hangup, {Code, Reason}}, State = #state{call_log = CallLog, timeout = Timeout}) ->
FullCode = case Code of
undefined -> undefined;
<<"0">> -> undefined;
_ -> "ISDN:" ++ binary_to_list(Code)
end,
NewCallLog = call_log:update(CallLog#call_log{fail_code = FullCode, fail_details = Reason}),
NewCallLog = call_log:update(CallLog#call_log{fail_code = Code, fail_details = Reason}),
{noreply, State#state{call_log = NewCallLog}, Timeout}.

%% @private
Expand Down
15 changes: 9 additions & 6 deletions broker/src/session/session.erl
Original file line number Diff line number Diff line change
Expand Up @@ -431,16 +431,19 @@ finalize({failed, Reason}, State = #state{session = Session = #session{call_log
"queued"
end
end,
lager:info("Call failed with reason ~p", [Reason]),
FailInfo = case Reason of
hangup -> [{fail_reason, "hangup"}];
busy -> [{fail_reason, "busy"}];
no_answer -> [{fail_reason, "no-answer"}];
{error, Reason} -> [{fail_reason, "fatal error"}, {fail_details, Reason}];
_ -> [{fail_reason, "error"}]
hangup -> [{fail_reason, "hangup"}];
busy -> [{fail_reason, "busy"}];
no_answer -> [{fail_reason, "no-answer"}];
{error, ErrDetails} -> [{fail_reason, "fatal error"}, {fail_details, ErrDetails}];
{error, ErrDetails, Code} -> [{fail_reason, "fatal error"}, {fail_details, ErrDetails}, {fail_code, Code}];
_ -> [{fail_reason, "error"}]
end,
CallLog:update([{state, NewState}, {finished_at, calendar:universal_time()}] ++ FailInfo),
StopReason = case Reason of
{error, Error} -> Error;
{error, ErrDetails2} -> ErrDetails2;
{error, ErrDetails2, _} -> ErrDetails2;
_ -> normal
end,
{stop, StopReason, State}.
Expand Down
35 changes: 32 additions & 3 deletions broker/src/twilio/twilio_broker.erl
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
-module(twilio_broker).
-export([start_link/0, init/0, dispatch/1, create_channel/1, destroy_channel/1, get_channel_status/1]).
-export([start_link/0, init/0, dispatch/1, create_channel/1, destroy_channel/1, get_channel_status/1, parse_exception/2]).

-behaviour(broker).
-include("session.hrl").
-include("uri.hrl").

-include_lib("xmerl/include/xmerl.hrl").

-compile([{parse_transform, lager_transform}]).

start_link() ->
broker:start_link(?MODULE).

Expand Down Expand Up @@ -32,11 +36,36 @@ dispatch(_Session = #session{session_id = SessionId, channel = Channel, address
],

Response = uri:post_form(RequestBody, [{basic_auth, {AccountSid, AuthToken}}], RequestUrl),
io:format("Response: ~p~n", [Response]),
lager:info("Twilio response: ~p~n", [Response]),
case Response of
{ok, {{_, 201, _}, _, _}} -> ok;
{ok, {{_, _, Reason}, _, _}} -> {error, Reason};
{ok, {{_, _, Reason}, _, Msg}} -> {error, parse_exception(Reason, Msg)};
_ ->
timer:apply_after(timer:minutes(1), broker, notify_ready, [?MODULE]),
{error, unavailable}
end.

parse_exception(Reason, Message) ->
try
{#xmlElement{content = [RestException]}, _} = xmerl_scan:string(Message),
case RestException#xmlElement.name of
'RestException' ->
Content = RestException#xmlElement.content,
FullCode = case extract_exception_item('Code', Content) of
undefined -> undefined;
Code -> "twilio:" ++ Code
end,
{error, extract_exception_item('Message', Content), FullCode};
_ -> Reason
end
catch
_ -> Reason
end.
Copy link
Member

@ggiraldez ggiraldez Nov 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's probably better to use xmerl_xs here. See http://erlang.org/doc/man/xmerl_xs.html

Something like:

parse_exception(Reason, Body) ->
  try
    {Doc, []} = xmerl_scan:string(Body),
    [Exception] = xmerl_xs:select("/TwilioResponse/RestException", Doc),
    FullCode = case xmerl_xs:value_of(xmerl_xs:select("./Code", Exception)) of
      [Code] -> "twilio" ++ Code;
      _ -> undefined
    end,
    [Message] = xmerl_xs:value_of(xmerl_xs:select("./Message", Exception)),
    {error, Message, FullCode}
  catch
    _ -> Reason
  end.


extract_exception_item(TargetName, [#xmlElement{name = Name, content = [#xmlText{value = Text}]} | Rest]) ->
case Name of
TargetName -> Text;
_ -> extract_exception_item(TargetName, Rest)
end;
extract_exception_item(_, []) ->
undefined.
6 changes: 6 additions & 0 deletions broker/test/twilio_broker_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-module(twilio_broker_test).
-include_lib("eunit/include/eunit.hrl").

parse_exception_test() ->
XmlString = "<?xml version='1.0' encoding='UTF-8'?>\n<TwilioResponse><RestException><Code>13223</Code><Message>The phone number you are attempting to call, 12345, is not valid.</Message><MoreInfo>https://www.twilio.com/docs/errors/13223</MoreInfo><Status>400</Status></RestException></TwilioResponse>",
?assertEqual({error, "The phone number you are attempting to call, 12345, is not valid.", "twilio:13223"}, twilio_broker:parse_exception("", XmlString)).