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 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
22 changes: 16 additions & 6 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!
update_sqs_policy!
redirect_to @sns_subscription, notice: 'SNS subscription was successfully created.'
end
else
render :new
end
Expand All @@ -31,7 +42,6 @@ def create
def update
@sns_subscription = Barbeque::SNSSubscription.find(params[:id])
if @sns_subscription.update(params.require(:sns_subscription).permit(:job_definition_id))
update_sqs_policy!
redirect_to @sns_subscription, notice: 'SNS subscription was successfully updated.'
else
render :edit
Expand Down
22 changes: 22 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,28 @@
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

context 'with AuthorizationError' 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::AuthorizationError.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 authorized'])
end
end
end

describe '#destroy' do
Expand Down