Skip to content

Commit 2a0dd2e

Browse files
committed
Merge pull request #20 from wildcardlabs/pass-response-to-error-serializer
Pass response to error serializer
2 parents a4d9087 + 03a1a1f commit 2a0dd2e

File tree

8 files changed

+103
-43
lines changed

8 files changed

+103
-43
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## 0.3.0
44

5+
* Support `status` and `code` for Error Objects
6+
7+
When an error is handed back to the user, we now provide the status and the
8+
code keys. This adds another feature to the API that is supported by the
9+
JSON API specification.
10+
11+
[Ben A. Morgan](https://github.com/BenMorganIO)
12+
513
* Add Friendly ID Support for `Spree::Product`
614

715
When a request came in for `/api/v2/products/example-product` and there was

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ Below, you'll see a bullet list of requirements that are both specific to the ap
3737
- [ ] `id`
3838
- [ ] `links`
3939
- [ ] `about`
40-
- [ ] `status`
41-
- [ ] `code`
40+
- [x] `status`
41+
- [x] `code`
4242
- [x] `title`
4343
- [x] `detail`
4444
- [ ] `source`

app/controllers/concerns/spree/api/v2/renderable.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ def filter_params
2424

2525
private
2626

27-
def error_response(resource)
28-
Spree::ErrorSerializer.new(resource).as_json
27+
def render_error(resource, options = {})
28+
response.status = options.delete(:status) || 400
29+
options[:response] = response
30+
render json: error_response(resource, options)
31+
end
32+
33+
def error_response(resource, options = {})
34+
Spree::ErrorSerializer.new(resource, options).as_json
2935
end
3036

3137
def filter_attributes

app/controllers/spree/api/v2/line_items_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ def line_item_params
2020
end
2121

2222
def render_range_error
23-
render json: error_response(:quantity_too_high), status: 400
23+
render_error :quantity_too_high
2424
end
2525

2626
def render_insufficient_stock_error
27-
render json: error_response(:product_out_of_stock), status: 400
27+
render_error :product_out_of_stock
2828
end
2929

3030
def render_record_not_found_error
31-
render json: error_response(:record_not_found), status: 400
31+
render_error :record_not_found
3232
end
3333
end
3434
end

app/serializers/spree/error_serializer.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module Spree
22
class ErrorSerializer
3-
attr_accessor :resource, :options
3+
attr_accessor :resource, :options, :response
44

55
def initialize(resource, options = {})
66
@resource = resource
7+
@response = options.delete(:response)
78
@options = options
89
end
910

@@ -40,7 +41,15 @@ def error_object(title, detail, pointer: '')
4041
title: title.to_s.titleize
4142
}
4243

44+
error.merge!(response_attributes) if response.present?
4345
pointer.present? ? error.merge(pointer: pointer) : error
4446
end
47+
48+
def response_attributes
49+
{
50+
status: response.status_message,
51+
code: response.code
52+
}
53+
end
4554
end
4655
end

docs/source/includes/_line_items.md

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,35 @@ curl "https://example.com/api/v2/line_items"
1313

1414
```json
1515
{
16-
"data" : {
17-
"attributes" : {
18-
"additional_tax_total" : "0",
19-
"adjustment_total" : "0.0",
20-
"amount" : "10.0",
21-
"cost_price" : "17.0",
22-
"currency" : "CAD",
23-
"display_amount" : "$10.00 CAD",
24-
"display_price" : "$10.00 CAD",
25-
"display_total" : "$10.00 CAD",
26-
"order_id" : 1,
27-
"price" : "10.0",
28-
"quantity" : 1,
29-
"total" : "10.0",
30-
"variant_id" : 1
16+
"data": {
17+
"attributes": {
18+
"additional_tax_total": "0",
19+
"adjustment_total": "0.0",
20+
"amount": "10.0",
21+
"cost_price": "17.0",
22+
"currency": "CAD",
23+
"display_amount": "$10.00 CAD",
24+
"display_price": "$10.00 CAD",
25+
"display_total": "$10.00 CAD",
26+
"order_id": 1,
27+
"price": "10.0",
28+
"quantity": 1,
29+
"total": "10.0",
30+
"variant_id": 1
3131
},
32-
"relationships" : {
33-
"order" : {
34-
"data" : {
35-
"type" : "spree_orders"
32+
"relationships": {
33+
"order": {
34+
"data": {
35+
"type": "spree_orders"
3636
}
3737
},
38-
"variant" : {
39-
"data" : {
40-
"type" : "spree_variants"
38+
"variant": {
39+
"data": {
40+
"type": "spree_variants"
4141
}
4242
}
4343
},
44-
"type" : "spree_line_items"
44+
"type": "spree_line_items"
4545
}
4646
}
4747
```
@@ -63,11 +63,13 @@ curl "https://example.com/api/v2/line_items"
6363

6464
```json
6565
{
66-
"errors" : [
66+
"errors": [
6767
{
68-
"detail" : "Quantity is too High",
69-
"meta" : {},
70-
"title" : "The quantity that you have submitted is astronomically high, please tone it down a bit."
68+
"code": "400",
69+
"detail": "Quantity is too High",
70+
"meta": {},
71+
"status": "Bad Request",
72+
"title": "The quantity that you have submitted is astronomically high, please tone it down a bit."
7173
}
7274
]
7375
}
@@ -90,11 +92,13 @@ curl "https://example.com/api/v2/line_items"
9092

9193
```json
9294
{
93-
"errors" : [
95+
"errors": [
9496
{
95-
"detail" : "Record Not Found",
96-
"meta" : {},
97-
"title" : "One of the records that you were looking for could not be found. Please check to see if the record exists or if you're permitted to read it"
97+
"code": "400",
98+
"detail": "Record Not Found",
99+
"meta": {},
100+
"status": "Bad Request",
101+
"title": "One of the records that you were looking for could not be found. Please check to see if the record exists or if you're permitted to read it"
98102
}
99103
]
100104
}
@@ -118,11 +122,13 @@ curl "https://example.com/api/v2/line_items"
118122

119123
```json
120124
{
121-
"errors" : [
125+
"errors": [
122126
{
123-
"detail" : "Product is out of Stock",
124-
"meta" : {},
125-
"title" : "This product is out of stock for the selected quantity."
127+
"code": "400",
128+
"detail": "Product is out of Stock",
129+
"meta": {},
130+
"status": "Bad Request",
131+
"title": "This product is out of stock for the selected quantity."
126132
}
127133
]
128134
}

spec/controllers/spree/api/v2/line_items_controller_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
{
3737
"errors" : [
3838
{
39+
"code": "400",
3940
"detail" : #{Spree.t('api.errors.quantity_too_high.detail').gsub("\n", '').to_json},
4041
"meta" : {},
42+
"status": "Bad Request",
4143
"title" : #{Spree.t('api.errors.quantity_too_high.title').titleize.to_json}
4244
}
4345
]
@@ -57,8 +59,10 @@
5759
{
5860
"errors" : [
5961
{
62+
"code": "400",
6063
"detail" : #{Spree.t('api.errors.record_not_found.detail').gsub("\n", '').to_json},
6164
"meta" : {},
65+
"status": "Bad Request",
6266
"title" : #{Spree.t('api.errors.record_not_found.title').to_json}
6367
}
6468
]
@@ -82,8 +86,10 @@
8286
{
8387
"errors" : [
8488
{
89+
"code": "400",
8590
"detail" : "#{Spree.t('api.errors.product_out_of_stock.detail').gsub("\n", '')}",
8691
"meta" : {},
92+
"status": "Bad Request",
8793
"title" : #{Spree.t('api.errors.product_out_of_stock.title').titleize.to_json}
8894
}
8995
]

spec/serializers/spree/error_serializer_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,30 @@
112112
}
113113
JSON
114114
end
115+
116+
context 'can set a response when it is present' do
117+
let(:response) do
118+
Response ||= Struct.new(:status_message, :code)
119+
Response.new('OK', 200)
120+
end
121+
122+
subject { described_class.new(:test, response: response).as_json }
123+
124+
it do
125+
should be_json_eql <<-JSON
126+
{
127+
"errors" : [
128+
{
129+
"code" : 200,
130+
"detail" : #{Spree.t('api.errors.test.detail').to_json},
131+
"meta" : {},
132+
"status" : "OK",
133+
"title" : #{Spree.t('api.errors.test.title').titleize.to_json}
134+
}
135+
]
136+
}
137+
JSON
138+
end
139+
end
115140
end
116141
end

0 commit comments

Comments
 (0)