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

Twilio delivery method allows Notifier to handle errors #444

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions docs/delivery_methods/twilio_messaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ deliver_by :twilio_messaging do |config|
end
```

## Error Handling

Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
Twilio provides a full list of error codes that can be handled as needed. See https://www.twilio.com/docs/api/errors

```ruby
deliver_by :twilio_messaging do |config|
config.error_handler = lambda do |twilio_error_response|
error_hash = JSON.parse(twilio_error_response.body)
case error_hash["code"]
when 21211
# The 'To' number is not a valid phone number.
# Write your error handling code
else
raise "Unhandled Twilio error: #{error_hash}"
end
end
end
```

## Options

* `json` - *Optional*
Expand Down
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
25 changes: 23 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,26 @@ 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