Improved error handling.

This commit is contained in:
Martin Edenhofer 2016-04-21 15:43:13 +02:00
parent fcdf62cd13
commit 30b05bd991
2 changed files with 35 additions and 11 deletions

View file

@ -5,11 +5,11 @@ class Integration::SipgateController < ApplicationController
# notify about inbound call / block inbound call
def in
return if feature_disabled
return if !configured?
config = Setting.get('sipgate_config')
config_inbound = config[:inbound]
block_caller_ids = config_inbound[:block_caller_ids]
config_inbound = config[:inbound] || {}
block_caller_ids = config_inbound[:block_caller_ids] || []
if params['event'] == 'newCall'
@ -49,7 +49,7 @@ class Integration::SipgateController < ApplicationController
# set caller id of outbound call
def out
return if feature_disabled
return if !configured?
config = Setting.get('sipgate_config')
config_outbound = config[:outbound][:routing_table]
@ -91,15 +91,26 @@ class Integration::SipgateController < ApplicationController
private
def feature_disabled
def configured?
if !Setting.get('sipgate_integration')
render(
json: {},
status: :unauthorized
)
return true
xml_error('Feature is disable, please contact your admin to enable it!')
return false
end
false
config = Setting.get('sipgate_config')
if !config || !config[:inbound] || !config[:outbound]
xml_error('Feature not configured, please contact your admin!')
return false
end
true
end
def xml_error(error)
xml = Builder::XmlMarkup.new(indent: 2)
xml.instruct!
content = xml.Response() do
xml.Error(error)
end
send_data content, type: 'application/xml; charset=UTF-8;', status: '500'
end
def base_url

View file

@ -179,6 +179,19 @@ class SipgateControllerTest < ActionDispatch::IntegrationTest
assert_equal('http://zammad.example.com/api/v1/sipgate/in', on_hangup)
assert_equal('http://zammad.example.com/api/v1/sipgate/in', on_answer)
# no config
Setting.set('sipgate_config', {})
params = 'event=newCall&direction=in&from=4912347114711&to=4930600000000&callId=4991155921769858278&user%5B%5D=user+1&user%5B%5D=user+2'
post '/api/v1/sipgate/in', params
assert_response(500)
error = nil
content = @response.body
response = REXML::Document.new(content)
response.elements.each('Response/Error') do |element|
error = element.text
end
assert_equal('Feature not configured, please contact your admin!', error)
end
end