Skip to content

Commit

Permalink
Merge pull request #20 from wildcardlabs/pass-response-to-error-seria…
Browse files Browse the repository at this point in the history
…lizer

Pass response to error serializer
  • Loading branch information
BenMorganIO committed Jan 24, 2016
2 parents a4d9087 + 03a1a1f commit 2a0dd2e
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 43 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## 0.3.0

* Support `status` and `code` for Error Objects

When an error is handed back to the user, we now provide the status and the
code keys. This adds another feature to the API that is supported by the
JSON API specification.

[Ben A. Morgan](https://github.com/BenMorganIO)

* Add Friendly ID Support for `Spree::Product`

When a request came in for `/api/v2/products/example-product` and there was
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ Below, you'll see a bullet list of requirements that are both specific to the ap
- [ ] `id`
- [ ] `links`
- [ ] `about`
- [ ] `status`
- [ ] `code`
- [x] `status`
- [x] `code`
- [x] `title`
- [x] `detail`
- [ ] `source`
Expand Down
10 changes: 8 additions & 2 deletions app/controllers/concerns/spree/api/v2/renderable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ def filter_params

private

def error_response(resource)
Spree::ErrorSerializer.new(resource).as_json
def render_error(resource, options = {})
response.status = options.delete(:status) || 400
options[:response] = response
render json: error_response(resource, options)
end

def error_response(resource, options = {})
Spree::ErrorSerializer.new(resource, options).as_json
end

def filter_attributes
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/spree/api/v2/line_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ def line_item_params
end

def render_range_error
render json: error_response(:quantity_too_high), status: 400
render_error :quantity_too_high
end

def render_insufficient_stock_error
render json: error_response(:product_out_of_stock), status: 400
render_error :product_out_of_stock
end

def render_record_not_found_error
render json: error_response(:record_not_found), status: 400
render_error :record_not_found
end
end
end
Expand Down
11 changes: 10 additions & 1 deletion app/serializers/spree/error_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module Spree
class ErrorSerializer
attr_accessor :resource, :options
attr_accessor :resource, :options, :response

def initialize(resource, options = {})
@resource = resource
@response = options.delete(:response)
@options = options
end

Expand Down Expand Up @@ -40,7 +41,15 @@ def error_object(title, detail, pointer: '')
title: title.to_s.titleize
}

error.merge!(response_attributes) if response.present?
pointer.present? ? error.merge(pointer: pointer) : error
end

def response_attributes
{
status: response.status_message,
code: response.code
}
end
end
end
76 changes: 41 additions & 35 deletions docs/source/includes/_line_items.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,35 @@ curl "https://example.com/api/v2/line_items"

```json
{
"data" : {
"attributes" : {
"additional_tax_total" : "0",
"adjustment_total" : "0.0",
"amount" : "10.0",
"cost_price" : "17.0",
"currency" : "CAD",
"display_amount" : "$10.00 CAD",
"display_price" : "$10.00 CAD",
"display_total" : "$10.00 CAD",
"order_id" : 1,
"price" : "10.0",
"quantity" : 1,
"total" : "10.0",
"variant_id" : 1
"data": {
"attributes": {
"additional_tax_total": "0",
"adjustment_total": "0.0",
"amount": "10.0",
"cost_price": "17.0",
"currency": "CAD",
"display_amount": "$10.00 CAD",
"display_price": "$10.00 CAD",
"display_total": "$10.00 CAD",
"order_id": 1,
"price": "10.0",
"quantity": 1,
"total": "10.0",
"variant_id": 1
},
"relationships" : {
"order" : {
"data" : {
"type" : "spree_orders"
"relationships": {
"order": {
"data": {
"type": "spree_orders"
}
},
"variant" : {
"data" : {
"type" : "spree_variants"
"variant": {
"data": {
"type": "spree_variants"
}
}
},
"type" : "spree_line_items"
"type": "spree_line_items"
}
}
```
Expand All @@ -63,11 +63,13 @@ curl "https://example.com/api/v2/line_items"

```json
{
"errors" : [
"errors": [
{
"detail" : "Quantity is too High",
"meta" : {},
"title" : "The quantity that you have submitted is astronomically high, please tone it down a bit."
"code": "400",
"detail": "Quantity is too High",
"meta": {},
"status": "Bad Request",
"title": "The quantity that you have submitted is astronomically high, please tone it down a bit."
}
]
}
Expand All @@ -90,11 +92,13 @@ curl "https://example.com/api/v2/line_items"

```json
{
"errors" : [
"errors": [
{
"detail" : "Record Not Found",
"meta" : {},
"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"
"code": "400",
"detail": "Record Not Found",
"meta": {},
"status": "Bad Request",
"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"
}
]
}
Expand All @@ -118,11 +122,13 @@ curl "https://example.com/api/v2/line_items"

```json
{
"errors" : [
"errors": [
{
"detail" : "Product is out of Stock",
"meta" : {},
"title" : "This product is out of stock for the selected quantity."
"code": "400",
"detail": "Product is out of Stock",
"meta": {},
"status": "Bad Request",
"title": "This product is out of stock for the selected quantity."
}
]
}
Expand Down
6 changes: 6 additions & 0 deletions spec/controllers/spree/api/v2/line_items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
{
"errors" : [
{
"code": "400",
"detail" : #{Spree.t('api.errors.quantity_too_high.detail').gsub("\n", '').to_json},
"meta" : {},
"status": "Bad Request",
"title" : #{Spree.t('api.errors.quantity_too_high.title').titleize.to_json}
}
]
Expand All @@ -57,8 +59,10 @@
{
"errors" : [
{
"code": "400",
"detail" : #{Spree.t('api.errors.record_not_found.detail').gsub("\n", '').to_json},
"meta" : {},
"status": "Bad Request",
"title" : #{Spree.t('api.errors.record_not_found.title').to_json}
}
]
Expand All @@ -82,8 +86,10 @@
{
"errors" : [
{
"code": "400",
"detail" : "#{Spree.t('api.errors.product_out_of_stock.detail').gsub("\n", '')}",
"meta" : {},
"status": "Bad Request",
"title" : #{Spree.t('api.errors.product_out_of_stock.title').titleize.to_json}
}
]
Expand Down
25 changes: 25 additions & 0 deletions spec/serializers/spree/error_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,30 @@
}
JSON
end

context 'can set a response when it is present' do
let(:response) do
Response ||= Struct.new(:status_message, :code)
Response.new('OK', 200)
end

subject { described_class.new(:test, response: response).as_json }

it do
should be_json_eql <<-JSON
{
"errors" : [
{
"code" : 200,
"detail" : #{Spree.t('api.errors.test.detail').to_json},
"meta" : {},
"status" : "OK",
"title" : #{Spree.t('api.errors.test.title').titleize.to_json}
}
]
}
JSON
end
end
end
end

0 comments on commit 2a0dd2e

Please sign in to comment.