Skip to content

Commit 16a72ae

Browse files
authored
Merge pull request #320 from EasyPost/fix_error_handling
fix: error parsing
2 parents 8c1e964 + 325813d commit 16a72ae

13 files changed

Lines changed: 161 additions & 340 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
## Next Release (minor)
44

55
- Adds Ruby 3.4 support
6+
- Fixes error parsing
7+
- Allows for alternative format of `errors` field
8+
- Corrects available properties of an `EasyPostError` and `ApiError` (`code` and `field` removed from `EasyPostError`, `message` unfurled and explicitly added to `ApiError`)
9+
- Removes unused `Error` model
610
- Corrects the HTTP verb for updating a brand from `GET` to `PATCH`
711
- Removes the deprecated `create_list` tracker endpoint function as it is no longer available via API
812

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ docs:
2121
bundle exec rdoc lib -o docs --title "EasyPost Ruby Docs"
2222

2323
## install-styleguide - Import the style guides (Unix only)
24-
install-styleguide: | update-examples-submodule
24+
install-styleguide: | init-examples-submodule
2525
sh examples/symlink_directory_files.sh examples/style_guides/ruby .
2626

2727
## init-examples-submodule - Initialize the examples submodule

lib/easypost/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def initialize(api_key:, read_timeout: 60, open_timeout: 30, api_base: 'https://
7676
# @param endpoint [String] URI path of the resource
7777
# @param params [Object] (nil) object to be used as the request parameters
7878
# @param api_version [String] the version of API to hit
79-
# @raise [EasyPost::Error] if the response has a non-2xx status code
79+
# @raise [EasyPost::Errors::EasyPostError] if the response has a non-2xx status code
8080
# @return [Hash] JSON object parsed from the response body
8181
def make_request(
8282
method,

lib/easypost/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# @param path [String] URI path of the resource
88
# @param requested_api_key [String] ({EasyPost.api_key}) key set Authorization header.
99
# @param body [String] (nil) body of the request
10-
# @raise [EasyPost::Error] if the response has a non-2xx status code
10+
# @raise [EasyPost::Errors::EasyPostError] if the response has a non-2xx status code
1111
# @return [Hash] JSON object parsed from the response body
1212
def call(method, path, api_key = nil, body = nil)
1313
raise EasyPost::Errors::MissingParameterError.new('api_key') if api_key.nil?

lib/easypost/errors/api/api_error.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
require 'easypost/constants'
55

66
class EasyPost::Errors::ApiError < EasyPost::Errors::EasyPostError
7-
attr_reader :status_code, :code, :errors
7+
attr_reader :message, :status_code, :code, :errors
88

99
def initialize(message, status_code = nil, error_code = nil, sub_errors = nil)
1010
super message
11+
message_list = []
12+
EasyPost::Errors::ApiError.collect_error_messages(message, message_list)
13+
@message = message_list.join(', ')
1114
@status_code = status_code
1215
@code = error_code
1316
@errors = sub_errors
@@ -46,12 +49,9 @@ def self.handle_api_error(response)
4649
# Try to parse the response body as JSON
4750
begin
4851
error_data = JSON.parse(response.body)['error']
49-
5052
error_message = error_data['message']
5153
error_type = error_data['code']
52-
errors = error_data['errors']&.map do |error|
53-
EasyPost::Models::Error.from_api_error_response(error)
54-
end
54+
errors = error_data['errors']
5555
rescue StandardError
5656
error_message = response.code.to_s
5757
error_type = EasyPost::Constants::API_ERROR_DETAILS_PARSING_ERROR

lib/easypost/models.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ module EasyPost::Models
1414
require_relative 'models/customs_info'
1515
require_relative 'models/customs_item'
1616
require_relative 'models/end_shipper'
17-
require_relative 'models/error'
1817
require_relative 'models/event'
1918
require_relative 'models/insurance'
2019
require_relative 'models/order'

lib/easypost/models/error.rb

Lines changed: 0 additions & 21 deletions
This file was deleted.

spec/address_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,23 @@
2828
address = client.address.create(address_data)
2929

3030
expect(address).to be_an_instance_of(EasyPost::Models::Address)
31+
32+
# Delivery verification assertions
3133
expect(address.verifications.delivery.success).to be false
34+
# TODO: details is not deserializing correctly, related to the larger "double EasyPostObject" wrapping issue
35+
# expect(address.verifications.delivery.details).to be_empty
36+
expect(address.verifications.delivery.errors[0].code).to eq('E.ADDRESS.NOT_FOUND')
37+
expect(address.verifications.delivery.errors[0].field).to eq('address')
38+
expect(address.verifications.delivery.errors[0].suggestion).to be nil
39+
expect(address.verifications.delivery.errors[0].message).to eq('Address not found')
40+
41+
# Zip4 verification assertions
42+
expect(address.verifications.zip4.success).to be false
43+
expect(address.verifications.zip4.details).to be nil
44+
expect(address.verifications.zip4.errors[0].code).to eq('E.ADDRESS.NOT_FOUND')
45+
expect(address.verifications.zip4.errors[0].field).to eq('address')
46+
expect(address.verifications.zip4.errors[0].suggestion).to be nil
47+
expect(address.verifications.zip4.errors[0].message).to eq('Address not found')
3248
end
3349

3450
it 'creates an address with verify_strict param' do

spec/cassettes/errors/EasyPost_Errors_api_error_raised_when_API_returns_error.yml renamed to spec/cassettes/errors/EasyPost_Errors_api_error_assigns_properties_of_an_error_correctly.yml

Lines changed: 20 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/cassettes/errors/EasyPost_Errors_api_error_assigns_properties_of_an_error_correctly_when_returned_via_the_alternative_format.yml

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)