Skip to content

Commit ce79f4e

Browse files
committed
Update Error Response Structure
The Partner API is changing the response structure of their errors. We should reflect these changes in the Gem to stay at parity. Link to Partner API error response structure: batchlinesolutions/partner-api#297 This change addresses the need by: * Introducing a ApiError object class * Parsing the error response body and parsing the new structure to generate better error messages
1 parent 89d8fa5 commit ce79f4e

11 files changed

Lines changed: 70 additions & 39 deletions

File tree

‎lib/intelligent_foods.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
require "intelligent_foods/api_client"
99
require "intelligent_foods/authorization"
1010
require "intelligent_foods/authorization/basic"
11+
require "intelligent_foods/resources/api_error"
1112
require "intelligent_foods/authorization/bearer"
12-
require "intelligent_foods/errors"
1313
require "intelligent_foods/resources/object"
1414
require "intelligent_foods/resources/order"
1515
require "intelligent_foods/resources/order_item"
@@ -20,6 +20,7 @@
2020
require "intelligent_foods/resources/recipient"
2121
require "intelligent_foods/serializers/recipient_serializer"
2222
require "intelligent_foods/version"
23+
require "intelligent_foods/errors"
2324

2425
module IntelligentFoods
2526
class Error < StandardError; end

‎lib/intelligent_foods/api_client.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def handle_response(response:)
6060

6161
def handle_authentication_error!
6262
@access_token = nil
63-
raise AuthenticationError.new("Authentication failed")
63+
raise AuthenticationError.new(status: 401,
64+
title: "Authentication Failed")
6465
end
6566

6667
def parse_response_body(response)

‎lib/intelligent_foods/errors.rb‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# frozen_string_literal: true
22

33
module IntelligentFoods
4-
class MenuNotFoundError < StandardError; end
4+
class MenuNotFoundError < ApiError; end
55

6-
class OrderNotCancelledError < StandardError; end
6+
class OrderNotCancelledError < ApiError; end
77

8-
class OrderNotCreatedError < StandardError; end
8+
class OrderNotCreatedError < ApiError; end
99

10-
class AuthenticationError < StandardError; end
10+
class AuthenticationError < ApiError; end
1111
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
module IntelligentFoods
4+
class ApiError < StandardError
5+
attr_reader :status, :title, :details
6+
7+
def initialize(status:, title:, details: nil)
8+
@status = status
9+
@title = title
10+
@details = details
11+
super
12+
end
13+
14+
def message
15+
if details.blank?
16+
"#{status} - #{title}"
17+
else
18+
"#{status} #{title} - #{description}"
19+
end
20+
end
21+
22+
def self.build(response)
23+
data = response.data
24+
data => { status:, title:, details: }
25+
new(status: status, title: title, details: details)
26+
end
27+
28+
protected
29+
30+
def description
31+
details[:description]
32+
end
33+
end
34+
end

‎lib/intelligent_foods/resources/menu.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def self.find(menu_id)
3232
if response.success?
3333
Menu.build_from_response(response.data)
3434
else
35-
raise MenuNotFoundError
35+
raise MenuNotFoundError.build(response)
3636
end
3737
end
3838
end

‎lib/intelligent_foods/resources/object.rb‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,8 @@ def self.build(data)
88

99
protected
1010

11-
def raise_error(error_class, response)
12-
error_message = build_error_message(response)
13-
raise error_class.new(error_message)
14-
end
15-
1611
def client
1712
@client ||= IntelligentFoods.client
1813
end
19-
20-
def build_error_message(response)
21-
response_body = response.data
22-
status_code = response_body[:status]
23-
error_title = response_body[:title]
24-
error_detail = response_body[:detail]
25-
"#{status_code} #{error_title} - #{error_detail}"
26-
end
2714
end
2815
end

‎lib/intelligent_foods/resources/order.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def create!
2828
Order::build_from_response(response.data)
2929
else
3030
mark_as_invalid
31-
raise_error(OrderNotCreatedError, response)
31+
raise OrderNotCreatedError.build(response)
3232
end
3333
end
3434

@@ -41,7 +41,7 @@ def cancel!
4141
self
4242
else
4343
mark_as_invalid
44-
raise_error(OrderNotCancelledError, response)
44+
raise OrderNotCancelledError.build(response)
4545
end
4646
end
4747

‎spec/intelligent_foods/api_client_spec.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636

3737
context "there is an error with the request" do
3838
it "raises an error" do
39-
response = error_response(message: "Could not perform request")
39+
response_body = { error: "Could not perform request" }
40+
response = error_response(body: response_body)
4041
stub_api_response response: response
4142
client = IntelligentFoods::ApiClient.new(id: "id", secret: "secret")
4243

‎spec/intelligent_foods/resources/menu_spec.rb‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
context "the id does not match a menu" do
5555
it "raises a IntelligentFoods::MenuNotFound error" do
5656
menu_id = "2023-01-01"
57-
body = build_error("Menu not found", menu_id)
58-
response = build_response(body: body, http_status_code: 400)
57+
response = error_response(message: "Menu not found",
58+
http_status_code: 400)
5959
stub_api_response response: response
6060

6161
expect {
@@ -64,15 +64,4 @@
6464
end
6565
end
6666
end
67-
68-
def build_error(message, menu_id)
69-
{
70-
type: "https://api.sunbasket.com/partner/problem/menu_not_found",
71-
status: 404,
72-
title: message,
73-
instance: "https://api.sunbasket.com/partner/menu/2020-09-03",
74-
detail: message,
75-
extra: { menu_id: menu_id },
76-
}
77-
end
7867
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"status": 400,
3+
"title": "Request Validation Error",
4+
"detail": "string does not match regex \"^[0-9a-f]",
5+
"details": {
6+
"description": "string does not match regex \"^[0-9a-f]",
7+
"reference_id": 1234,
8+
"item_id": 1234
9+
}
10+
}

0 commit comments

Comments
 (0)