Comments (10)
For anyone else reaching here like i did... i ended up implementing the following serializer. It supports nested model errors indexed. Manually tested, no rspec, so no guarantees
class ErrorSerializer
def initialize(model)
@model = model
end
def serialized_json
errors = @model.errors.messages.map do |field, errors|
errors.map do |error_message|
{
source: {pointer: "/data/attributes/#{field}"},
detail: error_message
}
end
end
@model.class.reflect_on_all_associations.each do |relationship|
@model.send(relationship.name).each_with_index do |child, index|
errors << child.errors.messages.map do |field, errors|
errors.map do |error_message|
{
source: {pointer: "/data/attributes/#{child.model_name.plural}[#{index}].#{field}"},
detail: error_message
}
end
end
end
end
errors.flatten
end
end
from fast_jsonapi.
@DVG thanks for chiming in and I agree with your approach. This library does not take an opinionated approach for serializing errors. You could optionally do something as simple as the below (untested) and still be within the json api spec.
data = { errors: [] }
data[:errors] << {id: "movie", title: "invalid title"}
render json: data, status: :unprocessable_entity
from fast_jsonapi.
module Api::V1
class ApiController < ActionController::API
around_action :handle_errors
def handle_errors
yield
rescue ActiveRecord::RecordNotFound => e
render_api_error(e.message, 404)
rescue ActiveRecord::RecordInvalid => e
render_api_error(e.record.errors.full_messages, 422)
rescue JWT::ExpiredSignature => e
render_api_error(e.message, 401)
rescue InvalidTokenError => e
render_api_error(e.message, 422)
rescue MissingTokenError => e
render_api_error(e.message, 422)
end
def render_api_error(messages, code)
data = { errors: { code: code, details: Array.wrap(messages) } }
render json: data, status: code
end
end
end
This was the pattern we used for rendering errors for this gem, compliant with the JSON API spec.
from fast_jsonapi.
JSONAPI only requires that a top level errors array be returned and has many optional member attributes that you may or may not find useful:
http://jsonapi.org/format/#error-objects
Therefore you should just be able to make an error serializer with as much of the spec that you find useful (code, title, detail, for instance)
EDIT to add: Not my project, just wanted to help
from fast_jsonapi.
It would be awesome to have an implementation of the errors in json api format!
from fast_jsonapi.
Alternatively consider using the jsonapi.rb
gem which provides error handling for validation errors and other generic cases you can encounter:
https://github.com/stas/jsonapi.rb#error-handling
Since the work is heavily based on the fast_jsonapi
and the feedback I could find here (hence the small codebase), it should be easy to start using it.
Let me know if you have feedback as well 🙇♂️
from fast_jsonapi.
This is not JSON:API compatible actually From https://jsonapi.org/format/#errors-processing
Error objects MUST be returned as an array keyed by errors in the top level of a JSON:API document.
I think this is exactly why this library should implement errors.
from fast_jsonapi.
I feel like this question has been answered. I'm going to close this for now, please feel free to re-open if you disagree!
from fast_jsonapi.
Additionally, if you like the JSONAPI error spec and want it for general use beyond errors within a model. You can drop something like this right in application_controller.rb
# CUSTOM GENERAL EXCEPTION HANDLING
# NOTE: This MUST come before any more specific exception handling. The order of definition is relevant!
rescue_from StandardError do |e|
logger.error "ApplicationController: GENERAL ERROR caught #{e}\n #{e.backtrace[0..5].join("\n")}"
respond_to do |format|
format.html { raise(e) }
format.json {
error_struct = {:errors => [
:status => '500',
:code => e.class.to_s,
:detail => e.message,
]}
render json: error_struct, status: :internal_server_error
}
format.pdf { Rails.env.development? ? raise(e) : redirect_to(root_path, :flash => { :error => "Error rendering page as PDF file" }) }
end
end
(I threw in PDF to demonstrate the env conditional which I often find useful during dev)
from fast_jsonapi.
@DVG thanks for chiming in and I agree with your approach. This library does not take an opinionated approach for serializing errors. You could optionally do something as simple as the below (untested) and still be within the json api spec.
data = { errors: [] } data[:errors] << {id: "movie", title: "invalid title"} render json: data, status: :unprocessable_entity
This is not JSON:API compatible actually
From https://jsonapi.org/format/#errors-processing
Error objects MUST be returned as an array keyed by errors in the top level of a JSON:API document.
Sorry for chiming in this late
from fast_jsonapi.
Related Issues (20)
- How can I reference dynamic attributes inside of other dynamic attributes? HOT 1
- Relationship scope HOT 1
- do i need to always give serializer name or it can auto pick HOT 1
- Any way to globally output 'pretty' JSON? HOT 1
- Is this project abandoned? No PR's merged or versions released in nearly a year! HOT 10
- Customize JSON Response HOT 3
- passing in list of params for collection?
- Thoughts on adding a value transform option or a 'type' option to Attribute?
- Unexpected behavior when object belongs_to :size HOT 2
- set_id value not respected in relationships HOT 1
- FYI > this repo has been FORKED and is active at new repository HOT 1
- uninitialized constant FooBar::OrganizationSerializer::FastJsonapi HOT 2
- #serialized_json for empty collection HOT 1
- circular association troubles HOT 1
- Integers and Floats are represented as strings HOT 1
- Bug: Associations that have an attribute or method named `map` cannot be serialized HOT 1
- ObjectSerializer not working for single object HOT 2
- Alternative to this gem -> Alba HOT 1
- Issue with accessor with a proc shortcut in ruby 3.0 HOT 1
- Single attribute with serializer parameter
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 fast_jsonapi.