Skip to content

Commit

Permalink
SRUBY-8 update error handling (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
bodrovis authored Sep 21, 2021
1 parent ba763bf commit 6cc5430
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
2. [Create a topic branch.][branch]
3. Implement your feature or bug fix.
4. Don't forget to add specs and make sure they pass by running `rspec .`.
5. Make sure your code complies with the style guide by running `rubocop`. `rubocop -a` can automatically fix most issues for you.
5. Make sure your code complies with the style guide by running `rubocop`. `rubocop -a` can automatically fix most issues for you. Run `rubocop -A` to make it more aggressive.
6. If necessary, add documentation for your feature or bug fix.
7. Commit and push your changes.
8. [Submit a pull request.][pr]
Expand Down
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: ruby

rvm:
- 2.6.7
- 2.7.3
- 3.0.1
- 2.6.8
- 2.7.4
- 3.0.2

install: bundle install --retry=3

Expand Down
2 changes: 1 addition & 1 deletion docs/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ gem 'minima', '~> 2.0'

# If you want to use GitHub Pages, remove the "gem "jekyll"" above and
# uncomment the line below. To upgrade, run `bundle update github-pages`.
gem 'github-pages', '~> 218', group: :jekyll_plugins
gem 'github-pages', '~> 219', group: :jekyll_plugins

# If you have any plugins, put them here!
group :jekyll_plugins do
Expand Down
5 changes: 5 additions & 0 deletions docs/additional_info/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 4.3.1 (21-Sep-21)

* Make exception handling more solid
* Test with more recent Rubies

## 4.3.0 (15-Jul-21)

* Added support for GZip compression. It is off by default but you can enable it by setting the `enable_compression` option to `true`:
Expand Down
3 changes: 2 additions & 1 deletion lib/ruby-lokalise-api/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class Error < StandardError
class << self
# Create a new error from an HTTP response
def from_response(body)
new body['error']['message'].to_s
msg = body.key?('error') ? body['error']['message'] : body['message']
new msg.to_s
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/ruby-lokalise-api/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def prepare(path)
def respond_with(response, client)
body = custom_load response.body
uri = Addressable::URI.parse response.env.url
respond_with_error(response.status, body) if body.respond_to?(:has_key?) && body.key?('error')
respond_with_error response.status, body if response.status.between?(400, 599) || (body.respond_to?(:has_key?) && body.key?('error'))
extract_headers_from(response).
merge('content' => body,
'client' => client,
Expand All @@ -74,7 +74,7 @@ def extract_headers_from(response)
end

def respond_with_error(code, body)
raise(Lokalise::Error, body['error']) unless Lokalise::Error::ERRORS.key? code
raise(Lokalise::Error, body['error'] || body) unless Lokalise::Error::ERRORS.key? code

raise Lokalise::Error::ERRORS[code].from_response(body)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby-lokalise-api/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Lokalise
VERSION = '4.3.0'
VERSION = '4.3.1'
end
51 changes: 51 additions & 0 deletions spec/fixtures/vcr_cassettes/error_no_error_key.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions spec/lib/ruby-lokalise-api/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
end.to raise_error(described_class)
end

it 'handles an exception when the response does not contain an error key' do
expect do
VCR.use_cassette('error_no_error_key') do
get 'projects', Lokalise.client('invalid')
end
end.to raise_error(Lokalise::Error::BadRequest)
end

it 'raises BadRequest when API token is invalid' do
expect do
VCR.use_cassette('error_invalid_token') do
Expand Down

0 comments on commit 6cc5430

Please sign in to comment.