Code Monkey home page Code Monkey logo

Comments (10)

tommyalvarez avatar tommyalvarez commented on July 17, 2024 13

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.

grossadamm avatar grossadamm commented on July 17, 2024 9

@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.

KelseyDH avatar KelseyDH commented on July 17, 2024 5
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.

DVG avatar DVG commented on July 17, 2024 2

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.

pioz avatar pioz commented on July 17, 2024 1

It would be awesome to have an implementation of the errors in json api format!

from fast_jsonapi.

stas avatar stas commented on July 17, 2024 1

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 🙇‍♂️

Related issues #102 #408

from fast_jsonapi.

danielb2 avatar danielb2 commented on July 17, 2024 1

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.

grossadamm avatar grossadamm commented on July 17, 2024

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.

Genkilabs avatar Genkilabs commented on July 17, 2024

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.

juanmanuelramallo avatar juanmanuelramallo commented on July 17, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.