Skip to content

Commit

Permalink
Handle server errors such as timouts & non-json responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ojab committed Oct 12, 2020
1 parent 7f5c40a commit ae25088
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
8 changes: 8 additions & 0 deletions lib/slack/web/faraday/response/raise_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ def on_complete(env)
error_class ||= Slack::Web::Api::Errors::SlackError
raise error_class.new(error_message, env.response)
end

def call(env)
super
rescue Slack::Web::Api::Errors::SlackError, Slack::Web::Api::Errors::TooManyRequestsError => e
raise e
rescue ::Faraday::ServerError, ::Faraday::ParsingError
raise Slack::Web::Api::Errors::InternalError.new('internal_error', env.response)
end
end
end
end
Expand Down
36 changes: 30 additions & 6 deletions spec/slack/web/faraday/response/raise_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,38 @@
require 'spec_helper'

RSpec.describe Slack::Web::Faraday::Response::RaiseError do
subject(:raise_error_obj) { described_class.new }
subject(:raise_error_obj) { described_class.new(app) }

describe '#on_complete' do
let(:status) { 200 }
let(:response) { nil }
let(:body) { {} }
let(:env) { double status: status, response: response, body: body }
let(:app) { proc {} }
let(:status) { 200 }
let(:response) { nil }
let(:body) { {} }
let(:env) { double status: status, response: response, body: body }

describe '#call' do
context 'with non-json reply' do
let(:app) { proc { raise ::Faraday::ParsingError, 'oops' } }
let(:env) { double status: status, response: response, body: body }

it 'raises Slack::Web::Api::Errors::InternalError' do
expect { raise_error_obj.call(env) }.to (
raise_error(Slack::Web::Api::Errors::InternalError, 'internal_error')
)
end
end

context 'with no reply (for example, on timeout)' do
let(:app) { proc { raise ::Faraday::TimeoutError, 'oops' } }
let(:env) { double status: status, response: nil, body: nil }
it 'raises Slack::Web::Api::Errors::InternalError' do
expect { raise_error_obj.call(env) }.to (
raise_error(Slack::Web::Api::Errors::InternalError, 'internal_error')
)
end
end
end

describe '#on_complete' do
context 'with status of 429' do
let(:status) { 429 }

Expand Down

0 comments on commit ae25088

Please sign in to comment.