Skip to content

Commit cddc898

Browse files
authored
Merge pull request #359 from agrobbin/500-html-response
Handle non-JSON 500 errors
2 parents afe4909 + 541c421 commit cddc898

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### 0.16.1 (Next)
22

33
* Your contribution here.
4+
* [#359](https://github.com/slack-ruby/slack-ruby-client/pull/359): Handle non-JSON 500 errors - [@agrobbin](https://github.com/agrobbin).
45

56
### 0.16.0 (2021/01/24)
67

lib/slack/web/faraday/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ def connection
2424
::Faraday::Connection.new(endpoint, options) do |connection|
2525
connection.use ::Faraday::Request::Multipart
2626
connection.use ::Faraday::Request::UrlEncoded
27-
connection.use ::Slack::Web::Faraday::Response::WrapError
2827
connection.use ::Slack::Web::Faraday::Response::RaiseError
2928
connection.use ::FaradayMiddleware::Mashify, mash_class: Slack::Messages::Message
3029
connection.use ::FaradayMiddleware::ParseJson
30+
connection.use ::Slack::Web::Faraday::Response::WrapError
3131
connection.response :logger, logger if logger
3232
connection.adapter adapter
3333
end

lib/slack/web/faraday/response/raise_error.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@ def on_complete(env)
2323

2424
def call(env)
2525
super
26-
rescue Slack::Web::Api::Errors::SlackError, Slack::Web::Api::Errors::TooManyRequestsError
27-
raise
2826
rescue ::Faraday::ParsingError
2927
raise Slack::Web::Api::Errors::ParsingError.new('parsing_error', env.response)
30-
rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed
31-
raise Slack::Web::Api::Errors::TimeoutError.new('timeout_error', env.response)
3228
end
3329
end
3430
end

lib/slack/web/faraday/response/wrap_error.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ module Slack
33
module Web
44
module Faraday
55
module Response
6-
class WrapError < ::Faraday::Response::RaiseError
6+
class WrapError < ::Faraday::Response::Middleware
7+
UNAVAILABLE_ERROR_STATUSES = (500..599).freeze
8+
79
def on_complete(env)
8-
super
9-
rescue Slack::Web::Api::Errors::SlackError
10-
raise
11-
rescue ::Faraday::ServerError
10+
return unless UNAVAILABLE_ERROR_STATUSES.cover?(env.status)
11+
1212
raise Slack::Web::Api::Errors::UnavailableError.new('unavailable_error', env.response)
1313
end
14+
15+
def call(env)
16+
super
17+
rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed
18+
raise Slack::Web::Api::Errors::TimeoutError.new('timeout_error', env.response)
19+
end
1420
end
1521
end
1622
end

spec/slack/web/client_spec.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,22 @@
322322
end
323323

324324
context '5xx response' do
325-
before { stub_slack_request.to_return(status: 500, body: '{}') }
325+
context 'with a JSON body' do
326+
before { stub_slack_request.to_return(status: 500, body: '{}') }
326327

327-
it 'raises UnavailableError' do
328-
expect { request }.to raise_error(Slack::Web::Api::Errors::UnavailableError).with_message('unavailable_error')
329-
expect(exception.cause).to be_a(Faraday::ServerError)
330-
expect(exception.response.status).to eq(500)
328+
it 'raises UnavailableError' do
329+
expect { request }.to raise_error(Slack::Web::Api::Errors::UnavailableError).with_message('unavailable_error')
330+
expect(exception.response.status).to eq(500)
331+
end
332+
end
333+
334+
context 'with a HTML response' do
335+
before { stub_slack_request.to_return(status: 500, body: '<html></html>') }
336+
337+
it 'raises UnavailableError' do
338+
expect { request }.to raise_error(Slack::Web::Api::Errors::UnavailableError).with_message('unavailable_error')
339+
expect(exception.response.status).to eq(500)
340+
end
331341
end
332342
end
333343
end

0 commit comments

Comments
 (0)