Skip to content

Commit

Permalink
[Fix] error from v1 is not handled
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarusyk committed Jun 27, 2024
1 parent b2e38fa commit e6dff80
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## [Unreleased]

## [v0.1.3](https://github.com/mmarusyk/easyship/tree/v0.1.3) - 2024-06-27

### Fixed
- Error from v1 is not handled by @mmarusyk

## [v0.1.2](https://github.com/mmarusyk/easyship/tree/v0.1.2) - 2024-06-24

### Features
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
easyship (0.1.2)
easyship (0.1.3)
faraday (~> 2.9)

GEM
Expand Down
1 change: 1 addition & 0 deletions lib/easyship/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Easyship
class Error
# rubocop:disable Style::MutableConstant Style::MissingElse
ERRORS = {
400 => Easyship::Errors::BadRequestError,
401 => Easyship::Errors::InvalidTokenError,
402 => Easyship::Errors::PaymentRequiredError,
404 => Easyship::Errors::ResourceNotFoundError,
Expand Down
10 changes: 10 additions & 0 deletions lib/easyship/errors/bad_request_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require_relative 'client_error'

module Easyship
module Errors
# Raised when Easyship returns the HTTP status code 400
class BadRequestError < ClientError; end
end
end
10 changes: 3 additions & 7 deletions lib/easyship/middleware/error_handler_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ def body_error(body)

if body.key?(:error) && body[:error].is_a?(Hash)
format_body_error(body)
elsif body.key?(:errors) && body[:errors].is_a?(Array)
format_body_errors_array(body)
elsif body.key?(:errors)
format_body_errors(body)
else
Expand All @@ -46,12 +44,10 @@ def format_body_error(body)
body[:error]
end

def format_body_errors_array(body)
{ details: body[:errors], message: body[:errors].map { |error| error[:message] }.join(', ') }
end

def format_body_errors(body)
{ details: body[:errors], message: body[:errors] }
errors = Array(body[:errors])

{ details: errors, message: errors.join(', ') }
end

def format_by_default(body)
Expand Down
2 changes: 1 addition & 1 deletion lib/easyship/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Easyship
VERSION = '0.1.2'
VERSION = '0.1.3'
end
69 changes: 69 additions & 0 deletions spec/cassettes/rate/v1/rates/invalid-body.yml

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

12 changes: 12 additions & 0 deletions spec/easyship/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,16 @@
end
end
end

describe '#post' do
let(:path) { '/rate/v1/rates' }

context 'when invalid request body' do
it 'raises a ClientError' do
VCR.use_cassette('rate/v1/rates/invalid-body') do
expect { client.post(path, {}) }.to raise_error(Easyship::Errors::BadRequestError)
end
end
end
end
end

0 comments on commit e6dff80

Please sign in to comment.