Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 6.1's ActiveModel Errors Revamp breaks error response #33

Closed
KubaSemczuk opened this issue Jul 27, 2021 · 7 comments
Closed

Rails 6.1's ActiveModel Errors Revamp breaks error response #33

KubaSemczuk opened this issue Jul 27, 2021 · 7 comments
Labels
bug Something isn't working

Comments

@KubaSemczuk
Copy link

Rails 6.1 changed how ActiveModel errors are structured (explanation, Pull Request). It affects jsonapi_errors_handler error output.
Error that looked like that:

{
  "errors": [
    {
      "status": 422,
      "title": "Invalid request",
      "detail": "can't be blank",
      "source": {
        "pointer": "/data/attributes/magento_id"
      }
    }
  ]
}

now looks like that:

{
  "errors": [
    {
      "status": 422,
      "title": "Invalid request",
      "detail": null,
      "source":{
        "pointer": "/data/attributes/#<ActiveModel::Error:0x00007fcfe9f8b0e0>"
      }
    }
  ]
}

So information what error is about is lost.

@swilgosz swilgosz added the bug Something isn't working label Jul 27, 2021
@swilgosz
Copy link
Member

@KubaSemczuk Thanks for reporting this! Do you opt for a PR? :)

@KubaSemczuk
Copy link
Author

Hi. I'm not sure I'm ready for it skillwise (meaning I'm afraid it would take me ages and I won't have that much free time in next few weeks). I've never contributed to any gem. But I'll take a look at it on the weekend. Can't say I opt for it for now.

@swilgosz
Copy link
Member

Ok, let me know how it's going!

@KubaSemczuk
Copy link
Author

Sorry for not getting back to you. Life got in the way and I forgot. I have to say that I won't have time to take care of that in nearest future.

@swilgosz
Copy link
Member

No worries, added to my todos

@denialtorres
Copy link

im in rails 6 as well and overwriting the serializable_hash method inside JsonapiErrorsHandler::Errors::Invalid worked for me

class JsonapiErrorsHandler::Errors::Invalid
  def serializable_hash
    errors_array = []
    errors.to_h&.each do |error|
      errors_array << {
        status: status,
        title: title,
        detail: "#{error[0]} #{error[1]}",
        source: { pointer: "/data/attributes/#{error[0]}" }
      }
    end

    errors_array
  end
end

now im getting something like this

{
   :errors=>[
      {
         :status=>422,
         :title=>"Invalid request",
         :detail=>"title can't be blank",
         :source=>{
            :pointer=>"/data/attributes/title"
         }
      },
      {
         :status=>422,
         :title=>"Invalid request",
         :detail=>"content can't be blank",
         :source=>"{
            :"pointer=>"/data/attributes/content"
         }
      },
      {
         :status=>422,
         :title=>"Invalid request",
         :detail=>"slug can't be blank",
         :source=>{
            :pointer=>"/data/attributes/slug"
         }
      }
   ]
}

this is the full commit of my project if someone want to see
denialtorres/api-udemy@3d3ad6c

@swilgosz
Copy link
Member

swilgosz commented Oct 5, 2022

Resolved in: 2de8338

@swilgosz swilgosz closed this as completed Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants