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

Add error handling around SNS API calls #21

Merged
merged 5 commits into from
Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 18 additions & 5 deletions app/controllers/barbeque/sns_subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@ def edit

def create
@sns_subscription = Barbeque::SNSSubscription.new(params.require(:sns_subscription).permit(:topic_arn, :job_queue_id, :job_definition_id))

if @sns_subscription.save
update_sqs_policy!
subscribe_topic!
redirect_to @sns_subscription, notice: 'SNS subscription was successfully created.'
if @sns_subscription.valid?
begin
subscribe_topic!
rescue Aws::SNS::Errors::AuthorizationError
@sns_subscription.errors[:topic_arn] << 'is not authorized'
@sns_topic_arns = fetch_sns_topic_arns
render :new
rescue Aws::SNS::Errors::NotFound
@sns_subscription.errors[:topic_arn] << 'is not found'
@sns_topic_arns = fetch_sns_topic_arns
render :new
else
@sns_subscription.save
Copy link
Member

Choose a reason for hiding this comment

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

Use save! if you don't check the return value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

update_sqs_policy!
redirect_to @sns_subscription, notice: 'SNS subscription was successfully created.'
end
else
render :new
end
Expand Down Expand Up @@ -50,6 +61,8 @@ def destroy

def fetch_sns_topic_arns
sns_client.list_topics.topics.map(&:topic_arn)
rescue Aws::Errors::MissingCredentialsError, Aws::SNS::Errors::AuthorizationError
Copy link
Member

Choose a reason for hiding this comment

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

I think these exceptions should be reported as 5xx error because sns:ListTopics permission is required in Barbeque

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so, too. fixed.

[]
end

def update_sqs_policy!
Expand Down
11 changes: 11 additions & 0 deletions spec/controllers/barbeque/sns_subscriptions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@
expect { post :create , params: { sns_subscription: attributes } }.
to change { Barbeque::SNSSubscription.count }.by(1)
end

context 'with NotFound error' do
it 'does not create a record and shows error message' do
expect(sqs_client).not_to receive(:get_queue_attributes)
expect(controller).to receive(:subscribe_topic!).and_raise(Aws::SNS::Errors::NotFound.new(self, 'not found'))
allow(controller).to receive(:fetch_sns_topic_arns).and_return([])
post :create , params: { sns_subscription: attributes }
expect(response).to render_template(:new)
expect(assigns(:sns_subscription).errors[:topic_arn]).to eq(['is not found'])
end
end
end

describe '#destroy' do
Expand Down