Giter VIP home page Giter VIP logo

Comments (5)

mecampbellsoup avatar mecampbellsoup commented on May 30, 2024

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.

tiagopog avatar tiagopog commented on May 30, 2024

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.

mecampbellsoup avatar mecampbellsoup commented on May 30, 2024

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

mecampbellsoup avatar mecampbellsoup commented on May 30, 2024

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

tiagopog avatar tiagopog commented on May 30, 2024

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)

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.