Skip to content

Commit

Permalink
Twilio delivery method allows Notifier to handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bibstha committed May 19, 2024
1 parent b775e81 commit f4e8aa9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/noticed/delivery_methods/twilio_messaging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ module DeliveryMethods
class TwilioMessaging < DeliveryMethod
def deliver
post_request url, basic_auth: {user: account_sid, pass: auth_token}, form: json.stringify_keys
rescue Noticed::ResponseUnsuccessful => exception
if exception.response.code.start_with?("4") && config[:error_handler]
notification.instance_exec(exception.response, &config[:error_handler])
else
raise
end
end

def json
Expand Down
26 changes: 24 additions & 2 deletions test/delivery_methods/twilio_messaging_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class TwilioMessagingTest < ActiveSupport::TestCase
setup do
@delivery_method = Noticed::DeliveryMethods::TwilioMessaging.new
set_config(
@config = {
account_sid: "acct_1234",
auth_token: "token",
json: -> {
Expand All @@ -13,7 +13,8 @@ class TwilioMessagingTest < ActiveSupport::TestCase
Body: "Hello world"
}
}
)
}
set_config(@config)
end

test "sends sms" do
Expand All @@ -38,6 +39,27 @@ class TwilioMessagingTest < ActiveSupport::TestCase
end
end

test "passes error to notification instance if error_handler is configured" do
@delivery_method = Noticed::DeliveryMethods::TwilioMessaging.new(
"delivery_method_name",
noticed_notifications(:one)
)

error_handler_called = false
@config[:error_handler] = lambda do |twilio_error_message|
error_handler_called = true
end
set_config(@config)


stub_request(:post, "https://api.twilio.com/2010-04-01/Accounts/acct_1234/Messages.json").to_return(status: 422)
assert_nothing_raised do
@delivery_method.deliver
end

assert(error_handler_called, "Handler is called if status is 4xx")
end

private

def set_config(config)
Expand Down

0 comments on commit f4e8aa9

Please sign in to comment.