Skip to content

Commit c320e8f

Browse files
authored
Merge pull request #350 from ojab/master
Handle server errors such as timouts & non-json responses
2 parents 7f5c40a + a4c4373 commit c320e8f

File tree

13 files changed

+119
-31
lines changed

13 files changed

+119
-31
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Layout/EmptyLineAfterMagicComment:
1818
Enabled: false
1919

2020
Metrics/BlockLength:
21-
Max: 250
21+
Enabled: false
2222

2323
Metrics/ClassLength:
2424
Max: 250

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
### 0.15.2 (Next)
1+
### 0.16.0 (Next)
22

33
* [#348](https://github.com/slack-ruby/slack-ruby-client/pull/348): Added `admin_conversations_archive`, `admin_conversations_convertToPrivate`, `admin_conversations_create`, `admin_conversations_delete`, `admin_conversations_disconnectShared`, `admin_conversations_getConversationPrefs`, `admin_conversations_getTeams`, `admin_conversations_invite`, `admin_conversations_rename`, `admin_conversations_search`, `admin_conversations_setConversationPrefs`, `admin_conversations_unarchive`, `admin_conversations_ekm_listOriginalConnectedChannelInfo`, `admin_users_session_invalidate`, `apps_event_authorizations_list`, `conversations_mark`, `workflows_stepCompleted`, `workflows_stepFailed`, `workflows_updateStep` endpoints - [@wasabigeek](https://github.com/wasabigeek).
4+
* [#350](https://github.com/slack-ruby/slack-ruby-client/pull/350): Handle server errors such as timouts & non-json responses (see [Upgrading to 0.16.0](UPGRADING.md#upgrading-to--0160)) - [@ojab](https://github.com/ojab).
45
* Your contribution here.
56

67
### 0.15.1 (2020/9/3)

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,11 @@ If you exceed [Slack’s rate limits](https://api.slack.com/docs/rate-limits), a
326326

327327
##### Other Errors
328328

329-
In any other case, a `Faraday::ClientError` will be raised. This may be the case if Slack is temporarily unavailable, for example.
329+
In case of Slack temporarily unavailability a `Slack::Web::Api::Errors::ServerError` (`Slack::Web::Api::Errors::SlackError` subclass) subclasses will be raised and original `Faraday::Error` will be accesible via `exception.cause`.
330+
331+
Specifically `Slack::Web::Api::Errors::ParsingError` will be raised on non-json response (i. e. 200 OK with `Slack unavailable` HTML page) and `Slack::Web::Api::Errors::HttpRequestError` subclasses for connection failures (`Slack::Web::Api::Errors::TimeoutError` for read/open timeouts & `Slack::Web::Api::Errors::UnavailableError` for 5xx HTTP responses).
332+
333+
In any other case, a `Faraday::ClientError` will be raised.
330334

331335
### RealTime Client
332336

UPGRADING.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Upgrading Slack-Ruby-Client
22
===========================
33

4+
### Upgrading to >= 0.16.0
5+
6+
As of 0.16.0 `Faraday::Error` exceptions sans `Faraday::ClientError` are wrapped into `Slack::Web::Api::Errors::ServerError`, so if you're rescuing `Faraday::Error` — you should adjust your code to use `Slack::Web::Api::Errors::ServerError` and use `exception.cause` if underlying `Faraday::Error` is needed.
7+
8+
See [README#other-errors](README.md#other-errors) and [#350](https://github.com/slack-ruby/slack-ruby-client/pull/350) for more information.
9+
410
### Upgrading to >= 0.15.0
511

612
As of 0.15.0, `activesupport` is no longer required. Add `gem 'activesupport'` to your Gemfile if you required ActiveSupport via this library.
@@ -130,5 +136,3 @@ gem 'celluloid-io'
130136
When in doubt, use `faye-websocket`.
131137

132138
See [#5](https://github.com/slack-ruby/slack-ruby-client/issues/5) for more information.
133-
134-

lib/slack-ruby-client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
require_relative 'slack/web/api/error'
3030
require_relative 'slack/web/api/errors'
3131
require_relative 'slack/web/faraday/response/raise_error'
32+
require_relative 'slack/web/faraday/response/wrap_error'
3233
require_relative 'slack/web/faraday/connection'
3334
require_relative 'slack/web/faraday/request'
3435
require_relative 'slack/web/api/mixins'

lib/slack/web/api/errors.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ class HandleAlreadyExists < SlackError; end
161161
class HashConflict < SlackError; end
162162
class InactiveCall < SlackError; end
163163
class InternalError < SlackError; end
164+
class ServerError < InternalError; end
165+
class ParsingError < ServerError; end
166+
class HttpRequestError < ServerError; end
167+
class TimeoutError < HttpRequestError; end
168+
class UnavailableError < HttpRequestError; end
164169
class InvalidActor < SlackError; end
165170
class InvalidAppId < SlackError; end
166171
class InvalidArgName < SlackError; end

lib/slack/web/faraday/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ 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 ::Faraday::Response::RaiseError
27+
connection.use ::Slack::Web::Faraday::Response::WrapError
2828
connection.use ::Slack::Web::Faraday::Response::RaiseError
2929
connection.use ::FaradayMiddleware::Mashify, mash_class: Slack::Messages::Message
3030
connection.use ::FaradayMiddleware::ParseJson

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ module Response
66
class RaiseError < ::Faraday::Response::Middleware
77
def on_complete(env)
88
raise Slack::Web::Api::Errors::TooManyRequestsError, env.response if env.status == 429
9-
return unless (body = env.body) && !body['ok']
9+
10+
return unless env.success?
11+
12+
body = env.body
13+
return unless body
14+
return if body['ok']
1015

1116
error_message =
1217
body['error'] || body['errors'].map { |message| message['error'] }.join(',')
@@ -15,6 +20,16 @@ def on_complete(env)
1520
error_class ||= Slack::Web::Api::Errors::SlackError
1621
raise error_class.new(error_message, env.response)
1722
end
23+
24+
def call(env)
25+
super
26+
rescue Slack::Web::Api::Errors::SlackError, Slack::Web::Api::Errors::TooManyRequestsError
27+
raise
28+
rescue ::Faraday::ParsingError
29+
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)
32+
end
1833
end
1934
end
2035
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
module Slack
3+
module Web
4+
module Faraday
5+
module Response
6+
class WrapError < ::Faraday::Response::RaiseError
7+
def on_complete(env)
8+
super
9+
rescue Slack::Web::Api::Errors::SlackError
10+
raise
11+
rescue ::Faraday::ServerError
12+
raise Slack::Web::Api::Errors::UnavailableError.new('unavailable_error', env.response)
13+
end
14+
end
15+
end
16+
end
17+
end
18+
end

spec/slack/real_time/client_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22
require 'spec_helper'
33

4-
RSpec.describe Slack::RealTime::Client do # rubocop:disable Metrics/BlockLength
4+
RSpec.describe Slack::RealTime::Client do
55
let(:ws) { double(Slack::RealTime::Concurrency::Mock::WebSocket, on: true) }
66

77
before do

0 commit comments

Comments
 (0)