diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index fbda584..e8e6d99 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -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] diff --git a/.travis.yml b/.travis.yml index d4a0cfb..17f168d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/docs/Gemfile b/docs/Gemfile index e96e8ce..0805e4f 100644 --- a/docs/Gemfile +++ b/docs/Gemfile @@ -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 diff --git a/docs/additional_info/changelog.md b/docs/additional_info/changelog.md index 44f9198..141f04a 100644 --- a/docs/additional_info/changelog.md +++ b/docs/additional_info/changelog.md @@ -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`: diff --git a/lib/ruby-lokalise-api/error.rb b/lib/ruby-lokalise-api/error.rb index b7fa596..92c2578 100644 --- a/lib/ruby-lokalise-api/error.rb +++ b/lib/ruby-lokalise-api/error.rb @@ -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 diff --git a/lib/ruby-lokalise-api/request.rb b/lib/ruby-lokalise-api/request.rb index d22040b..6088e54 100644 --- a/lib/ruby-lokalise-api/request.rb +++ b/lib/ruby-lokalise-api/request.rb @@ -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, @@ -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 diff --git a/lib/ruby-lokalise-api/version.rb b/lib/ruby-lokalise-api/version.rb index 61caf41..2de8e7e 100644 --- a/lib/ruby-lokalise-api/version.rb +++ b/lib/ruby-lokalise-api/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Lokalise - VERSION = '4.3.0' + VERSION = '4.3.1' end diff --git a/spec/fixtures/vcr_cassettes/error_no_error_key.yml b/spec/fixtures/vcr_cassettes/error_no_error_key.yml new file mode 100644 index 0000000..c633a46 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/error_no_error_key.yml @@ -0,0 +1,51 @@ +--- +http_interactions: +- request: + method: get + uri: https://api.lokalise.com/api2/projects + body: + encoding: US-ASCII + string: '' + headers: + Accept: + - application/json + User-Agent: + - ruby-lokalise-api gem/4.3.0 + X-api-token: + - invalid + response: + status: + code: 400 + message: Bad Request + headers: + server: + - nginx + date: + - Tue, 21 Sep 2021 12:26:16 GMT + content-type: + - application/json + transfer-encoding: + - chunked + connection: + - keep-alive + cache-control: + - max-age=0, must-revalidate, no-cache, no-store, private + pragma: + - no-cache + x-content-type-options: + - nosniff + x-frame-options: + - deny + x-xss-protection: + - 1; mode=block + strict-transport-security: + - max-age=31536000 + referrer-policy: + - origin + expires: + - Tue, 21 Sep 2021 12:26:16 GMT + body: + encoding: UTF-8 + string: '{"message":"Invalid `X-Api-Token` header","code":400}' + recorded_at: Tue, 21 Sep 2021 12:26:16 GMT +recorded_with: VCR 6.0.0 diff --git a/spec/lib/ruby-lokalise-api/error_spec.rb b/spec/lib/ruby-lokalise-api/error_spec.rb index ba4958d..db0e3d3 100644 --- a/spec/lib/ruby-lokalise-api/error_spec.rb +++ b/spec/lib/ruby-lokalise-api/error_spec.rb @@ -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