Comments (5)
This would be the relevant part of the JSON API spec to follow when implementing this: http://jsonapi.org/format/#error-objects
@tiagopog what do you think?
from jsonapi-utils.
Sorry for the long delay, @mecampbellsoup. These last days were really tough at work.
Well, initially we did not get focused on error templates, so now it's open for developers to write their own exception classes. Like this one we have in a production project:
lib/api/v2/exceptions.rb
module API
module V2
module Exceptions
include do
helper_method :consumer_auth_error, :active_record_error, :unauthorized_error #...
end
#...
# 5
def active_record_error(object)
raise ActiveRecordError.new(object)
end
##########################################
# 5. Any ActiveRecord errors
##########################################
class ActiveRecordError < ::JSONAPI::Exceptions::Error
attr_accessor :object, :status
def initialize(object)
@object = object
@status = :unprocessable_entity
end
def errors
[JSONAPI::Error.new(code: JSONAPI::ACTIVE_RECORD_ERROR,
status: @status,
title: "Impossible to change this #{@object.class.name}",
detail: @object.errors)]
end
end
#...
end
end
end
The actual render goes in the base controle:
app/controllers/api/v2/base_controller.rb
module API
module V2
class BaseController < JSONAPI::ResourceController
include JSONAPI::Utils
include Exceptions
rescue_from Exceptions::ActiveRecordError, with: :jsonapi_render_errors
def jsonapi_render_errors(exception)
render json: { errors: exception.errors }, status: exception.status
end
end
end
end
Then when there is a validation error, for instance:
app/controllers/api/v2/users_controller.rb
# POST /api/v2/businesses
def create
@business = Business.new(custom_business_params)
@business.save!
jsonapi_render json: @business
rescue ActiveRecord::ActiveRecordError
active_record_error @business
end
That will produce the following error response:
HTTP 422 Unprocessable Entity
{
"errors": [
{
"title": "Impossible to change this Business",
"detail": {
"name": [
"can't be blank"
]
},
"id": null,
"href": null,
"code": 125,
"source": null,
"links": null,
"status": "422",
"meta": null
}
]
}
By the way, I guess it's an awesome idea to deal with these kinda common errors in some handy helper methods. I'll be really excited to work on this functionality in the coming days. For now could you try to implement something like the example above on your project?
from jsonapi-utils.
@tylermachen some helpful stuff in here regarding our current work on serializing errors in accordance with JSON API spec 😄
@tiagopog Yea, we have been working towards a solution for standardizing this, so we may even be able to just open a PR with said helper in it - give us a little time, but also happy to look at anything you produce in the coming days too 😉
from jsonapi-utils.
@tiagopog I guess such a json_render_errors
method would generate "standard" error objects in the style of JSON API's examples: http://jsonapi.org/examples/#error-objects-basics
I think I'll revisit this once we've seen how we end up handling API errors. The code you pasted above is looking similar to how we're going to approach it, i.e. catch an exception, instantiate an Error
object that has an #errors
method that returns a list of descriptive hashes for each error.
from jsonapi-utils.
Hey there, @mecampbellsoup. I started working on it on this weekend. I really appreciated your feedback and I'm sure it will be a great feature. For now I had to stop this work in order to review our test suite first – I'm turning it into something simpler –, but I may go back to that feature asap.
from jsonapi-utils.
Related Issues (20)
- Trying to override pagination...
- Decimals formatted as strings in json response
- Update jsonapi-resources 0.9.12 HOT 10
- Bump the rails version to 6.1 for tagged version 7.13.0 HOT 1
- Release a new version? HOT 1
- Support for operation processors? HOT 3
- Rendering scopes with `includes` is slow for no reason HOT 2
- Passing resource relationship in POST request HOT 1
- resource_params => NoMethodError: undefined method `keys' for nil:NilClass HOT 2
- Render multiple invalid AR objects
- Bring support to fast_jsonapi HOT 9
- Use the cache with jsonapi_render. HOT 1
- Getting Invalid resource for custom resource response on patch request
- Non-REST actions?
- JSONAPI::Utils::Exceptions::BadRequest undefined
- Relax jsonapi-resources dependency HOT 3
- Support for sorting and pagination from JR
- jsonapi/utils.rb:9:in `include': wrong number of arguments (given 0, expected 1+) (ArgumentError)
- Formatting of error's "detail" attribute
- Accept and Content-Type headers are not verified
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonapi-utils.