Last active
August 8, 2018 14:53
-
-
Save arefaslani/81a5fe231e6c5b90627bcd6f23eb946f to your computer and use it in GitHub Desktop.
JSON API Errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"errors": [ | |
{ | |
"status": "400", | |
"source": { "pointer": "/data/attributes" }, | |
"title": "parameter missing", | |
"detail": "parameter missed or empty: /data/attributes" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
def create | |
begin | |
render json: UserFactory.run(user_params), status: :created | |
rescue ActionController::ParameterMissing => e | |
return render json: { | |
errors: [ | |
{ | |
status: "422", | |
source: { pointer: e.param }, | |
title: "parameter missing", | |
detail: "#{e.param} missed" | |
} | |
] | |
}, status: :unprocessable_entity | |
end | |
end | |
def user_params | |
params | |
.require(:data) | |
.require(:attributes) | |
.permit(:mobile, :email, :password) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment