Adapt error formatting to documentation

This commit is contained in:
Frank Rousseau 2018-04-07 19:48:33 +02:00
parent df11297654
commit 5326ddf6c3
4 changed files with 19 additions and 11 deletions

View file

@ -10,25 +10,33 @@ module Api
rescue_from Exception do |e|
logger.error e.message
logger.error e.backtrace.join("\n")
render json: {error: e.message}, status: 500
render json: error_body(500, e.message), status: 500
end
rescue_from Rack::OAuth2::Server::Resource::Forbidden do |e|
logger.error e.message
render json: error_body(403, e.message), status: 403
end
rescue_from ActiveRecord::RecordNotFound do |e|
logger.error e.message
render json: {error: I18n.t("api.error.not_found")}, status: 404
message = I18n.t("api.error.not_found")
render json: error_body(404, message), status: 404
end
rescue_from ActiveRecord::RecordInvalid do |e|
logger.error e.message
render json: {error: e.to_s}, status: 400
render json: error_body(422, e.to_s), status: 422
end
rescue_from ActionController::ParameterMissing do |e|
logger.error e.message
render json: {
error: I18n.t("api.error.wrong_parameters"),
message: e.message
}, status: 400
message = I18n.t("api.error.wrong_parameters") + ": " + e.message
render json: error_body(422, message), status: 422
end
def error_body(code, message)
{code: code, message: message}
end
def current_user

View file

@ -41,7 +41,7 @@ describe Api::V1::ConversationsController do
context "without valid data" do
it "fails at creating the conversation" do
post api_v1_conversations_path, params: {access_token: access_token}
expect(response.status).to eq 400
expect(response.status).to eq 422
end
end
end

View file

@ -62,7 +62,7 @@ describe Api::V1::MessagesController do
api_v1_conversation_messages_path(@conversation_guid),
params: {access_token: access_token}
)
expect(response.status).to eq 400
expect(response.status).to eq 422
end
end

View file

@ -136,7 +136,7 @@ describe Api::V1::PostsController do
it "doesn't create the post" do
json_body = JSON.parse(response.body)
expect(json_body["error"]).to eq("insufficient_scope")
expect(json_body["code"]).to eq(403)
end
end
end
@ -174,7 +174,7 @@ describe Api::V1::PostsController do
it "doesn't delete the post" do
json_body = JSON.parse(response.body)
expect(json_body["error"]).to eq("insufficient_scope")
expect(json_body["code"]).to eq(403)
expect(response.status).to eq(403)
end
end