jsonapi-rb / jsonapi-rails

Rails gem for fast jsonapi-compliant APIs.
http://jsonapi-rb.org
MIT License
319 stars 63 forks source link

render jsonapi_errors source #63

Closed arefaslani closed 6 years ago

arefaslani commented 6 years ago

I use ActiveInteraction gem with Rails API and jsonapi-rails gem for creating my JSON API. My problem is when I render errors using jsonapi_errors, the source of the error response is empty. How should I fill this field? This is the response:

{
    "errors": [
        {
            "title": "Invalid credit",
            "detail": "Credit must be greater than or equal to 2000000",
            "source": {}
        }
    ],
    "jsonapi": {
        "version": "1.0"
    }
}
artem-galas commented 6 years ago

You should create such an object that would look like the following: for example error handling that occurs when Devise::RegistrationsController#create

def render_create_error
  error = {detail: resource_errors[:full_messages][0]}
  render jsonapi_errors: error
end
{
    "errors": [
        {
            "detail": "Email has already been taken"
        }
    ],
    "jsonapi": {
        "version": "1.0"
    }
}
beauby commented 6 years ago

@arefaslani In order for the source to be properly populated using the default serializer, you should deserialize the payload using deserializable_resource (it is possible to avoid that but it needs a bit more work).

aamir-pk commented 6 years ago

@beauby I will be thankful for your help if you can clarify the point of deserializing the payload while we are actually serializing the object.

Tnaks