Skip to content

Commit a50de84

Browse files
authored
add specific error classes for Web API (#312)
1 parent 067dc06 commit a50de84

File tree

10 files changed

+647
-7
lines changed

10 files changed

+647
-7
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ AllCops:
1111
- vendor/**/*
1212
- lib/slack/web/api/slack-api-ref/**/*
1313
- lib/slack/web/api/endpoints/* # Auto-generated
14+
- lib/slack/web/api/errors.rb # Auto-generated
1415
- spec/slack/web/api/endpoints/* # Auto-generated
1516
Layout/EmptyLineAfterMagicComment:
1617
Enabled: false

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* [#305](https://github.com/slack-ruby/slack-ruby-client/pull/305): Added `admin.inviteRequests.approve`, `admin.inviteRequests.deny`, `admin.inviteRequests.list`, `admin.inviteRequests.approved.list`, `admin.inviteRequests.denied.list`, `admin.teams.create`, `admin.teams.list`, `admin.teams.admins.list`, `admin.teams.owners.list`, `admin.teams.settings`, `admin.teams.settings.setIcon`, `admin.teams.settings.setName`, `admin.teams.settings.setDescription`, `admin.users.assign`, `admin.users.invite`, `admin.users.remove`, `admin.users.setAdmin`, `admin.users.setOwner` and `admin.users.setRegular` endpoints - [@manuelmeurer](https://github.com/manuelmeurer).
44
* [#311](https://github.com/slack-ruby/slack-ruby-client/pull/311): Made Web API `response_metadata` more accessible in errors - [@jmanian](https://github.com/jmanian).
5+
* [#312](https://github.com/slack-ruby/slack-ruby-client/pull/312): Added specific error classes for Web API - [@jmanian](https://github.com/jmanian).
56
* Your contribution here.
67

78
### 0.14.5 (2019/12/23)

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ A Ruby client for the Slack [Web](https://api.slack.com/web), [RealTime Messagin
3232
- [Web Client Options](#web-client-options)
3333
- [Pagination Support](#pagination-support)
3434
- [Error Handling](#error-handling)
35+
- [Slack Errors](#slack-errors)
36+
- [Rate Limiting](#rate-limiting)
37+
- [Other Errors](#other-errors)
3538
- [RealTime Client](#realtime-client)
3639
- [Slack::RealTime::Stores::Store](#slackrealtimestoresstore)
3740
- [Slack::RealTime::Stores::Starter](#slackrealtimestoresstarter)
@@ -275,9 +278,33 @@ all_members # many thousands of team members retrieved 10 at a time
275278

276279
#### Error Handling
277280

278-
If a request fails, a `Slack::Web::Api::Errors::SlackError` will be raised. The error message contains the error code, which is also accessible with `slack_error.error`. In case of multiple errors, the error message contains the error codes separated by commas, or they are accessible as an array with `slack_error.errors`. The original response is also accessible using the `response` attribute. The `response_metadata` is accessible with `slack_error.response_metadata`.
281+
##### Slack Errors
279282

280-
If you exceed [Slack’s rate limits](https://api.slack.com/docs/rate-limits), a `Slack::Web::Api::Errors::TooManyRequestsError` will be raised instead.
283+
If Slack returns an error for the request, then an error will be raised. The error class is specific to the type of error that Slack returns. For instance if Slack returns `account_inactive` then the error will be `Slack::Web::Api::Errors::AccountInactive`. This allows you to handle certain types of errors as needed:
284+
285+
```ruby
286+
rescue Slack::Web::Api::Errors::AccountInactive => e
287+
# deal with inactive account
288+
end
289+
```
290+
291+
All of these errors inherit from `Slack::Web::Api::Errors::SlackError`, so you can handle or silence all errors if necessary:
292+
293+
```ruby
294+
rescue Slack::Web::Api::Errors::SlackError => e
295+
# capture all Slack errors
296+
end
297+
```
298+
299+
If there's a new error type that is not yet known by this library, then it will raise `Slack::Web::Api::Errors::SlackError`. (Update the Web API if you find that errors are missing — see [CONTRIBUTING](CONTRIBUTING.md).)
300+
301+
In all of these cases the error message contains the error code, which is also accessible with `slack_error.error`. In case of multiple errors, the error message contains the error codes separated by commas, or they are accessible as an array with `slack_error.errors`. The original response is also accessible using the `response` attribute. The `response_metadata` is accessible with `slack_error.response_metadata`.
302+
303+
##### Rate Limiting
304+
305+
If you exceed [Slack’s rate limits](https://api.slack.com/docs/rate-limits), a `Slack::Web::Api::Errors::TooManyRequestsError` will be raised instead. (This does not inherit from `Slack::Web::Api::Errors::SlackError`.)
306+
307+
##### Other Errors
281308

282309
In any other case, a `Faraday::ClientError` will be raised. This may be the case if Slack is temporarily unavailable, for example.
283310

lib/slack-ruby-client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
require_relative 'slack/web/api/errors/slack_error'
2828
require_relative 'slack/web/api/errors/too_many_requests_error'
2929
require_relative 'slack/web/api/error'
30+
require_relative 'slack/web/api/errors'
3031
require_relative 'slack/web/faraday/response/raise_error'
3132
require_relative 'slack/web/faraday/connection'
3233
require_relative 'slack/web/faraday/request'

0 commit comments

Comments
 (0)