Skip to content

Conversation

@tiagopog
Copy link
Owner

@tiagopog tiagopog commented Jun 3, 2016

It fixes #17 and closes #6.

Summary

This PR brings:

  • Better error responses now fully compliant with JSON API's error specs;
  • New method JSONAPI::Utils#jsonapi_render_errors with the capability of rendering errors from exceptions, ActiveRecord objects and Hashes;
  • Better test coverage;
  • Examples and other improvements in README.

Examples

Code:

# app/controllers/users_controller.rb
  # POST /users
  def create
    user = User.new(user_params)
    if user.save
      jsonapi_render json: user, status: :created
    else
      jsonapi_render_errors json: user, status: :unprocessable_entity
    end
  end

  # PATCH /users/:id
  def update
    user = User.find(params[:id])
    if user.update(user_params)
      jsonapi_render json: user
    else
      jsonapi_render_errors Exceptions::MyCustomError.new(user)
    end
  end

# app/controllers/posts_controller.rb
  # POST /posts
  def create
    post = Post.new(post_params)
    if post.save
      jsonapi_render json: post, status: :created
    else
      errors = [{ id: 'validation', title: 'Something went wrong', code: '100' }]
      jsonapi_render_errors json: errors, status: :unprocessable_entity
    end
  end

Responses:

Endpoint:

GET http://localhost:3000/users?fields[foo]=bar&fields[foobar]=bar&include=bar

Response body:

{
  "errors": [
    {
      "title": "Invalid resource",
      "detail": "foo is not a valid resource.",
      "code": "101",
      "status": "400"
    },
    {
      "title": "Invalid resource",
      "detail": "foobar is not a valid resource.",
      "code": "101",
      "status": "400"
    },
    {
      "title": "Invalid field",
      "detail": "bar is not a valid relationship of users",
      "code": "112",
      "status": "400"
    }
  ]
}

Endpoint:

POST http://localhost:3000/users

Request body:

{
  "data": {
    "type": "users",
    "attributes": {
      "first_name": "",
      "last_name": ""
    }
  }
}

Response body:

{
  "errors": [
    {
      "title": "First name can't be blank",
      "id": "first_name",
      "code": "100",
      "status": "422"
    },
    {
      "title": "Last name can't be blank",
      "id": "last_name",
      "code": "100",
      "status": "422"
    }
  ]
}

@tiagopog tiagopog changed the title [WIP] Fix code in error responses via jsonapi_render_errors [WIP] Fix code in error responses Jun 3, 2016
@tiagopog
Copy link
Owner Author

tiagopog commented Jun 6, 2016

:shipit:? @mecampbellsoup @halilim

@tiagopog tiagopog changed the title [WIP] Fix code in error responses Fix code in error responses Jun 6, 2016
@tiagopog tiagopog self-assigned this Jun 6, 2016
@mecampbellsoup
Copy link
Contributor

LGTM 👍

@tiagopog
Copy link
Owner Author

tiagopog commented Jun 6, 2016

@halilim:
screen shot 2016-06-06 at 7 35 08 pm

@halilim
Copy link

halilim commented Jun 6, 2016

👍

@tiagopog
Copy link
Owner Author

tiagopog commented Jun 6, 2016

I will merge the changes and release the patch. Thanks folks.

@tiagopog tiagopog merged commit 51c2cd4 into master Jun 6, 2016
@tiagopog tiagopog deleted the hotfix/fix-type-of-error-codes branch June 6, 2016 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error codes must be strings Add helper/util method for rendering an object's errors?

4 participants