Adapt error formatting to documentation
This commit is contained in:
parent
79fe1c2a7e
commit
a7ea3ba254
4 changed files with 19 additions and 11 deletions
|
|
@ -10,25 +10,33 @@ module Api
|
||||||
rescue_from Exception do |e|
|
rescue_from Exception do |e|
|
||||||
logger.error e.message
|
logger.error e.message
|
||||||
logger.error e.backtrace.join("\n")
|
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
|
end
|
||||||
|
|
||||||
rescue_from ActiveRecord::RecordNotFound do |e|
|
rescue_from ActiveRecord::RecordNotFound do |e|
|
||||||
logger.error e.message
|
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
|
end
|
||||||
|
|
||||||
rescue_from ActiveRecord::RecordInvalid do |e|
|
rescue_from ActiveRecord::RecordInvalid do |e|
|
||||||
logger.error e.message
|
logger.error e.message
|
||||||
render json: {error: e.to_s}, status: 400
|
render json: error_body(422, e.to_s), status: 422
|
||||||
end
|
end
|
||||||
|
|
||||||
rescue_from ActionController::ParameterMissing do |e|
|
rescue_from ActionController::ParameterMissing do |e|
|
||||||
logger.error e.message
|
logger.error e.message
|
||||||
render json: {
|
message = I18n.t("api.error.wrong_parameters") + ": " + e.message
|
||||||
error: I18n.t("api.error.wrong_parameters"),
|
render json: error_body(422, message), status: 422
|
||||||
message: e.message
|
end
|
||||||
}, status: 400
|
|
||||||
|
def error_body(code, message)
|
||||||
|
{code: code, message: message}
|
||||||
end
|
end
|
||||||
|
|
||||||
def current_user
|
def current_user
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ describe Api::V1::ConversationsController do
|
||||||
context "without valid data" do
|
context "without valid data" do
|
||||||
it "fails at creating the conversation" do
|
it "fails at creating the conversation" do
|
||||||
post api_v1_conversations_path, params: {access_token: access_token}
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ describe Api::V1::MessagesController do
|
||||||
api_v1_conversation_messages_path(@conversation_guid),
|
api_v1_conversation_messages_path(@conversation_guid),
|
||||||
params: {access_token: access_token}
|
params: {access_token: access_token}
|
||||||
)
|
)
|
||||||
expect(response.status).to eq 400
|
expect(response.status).to eq 422
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,7 @@ describe Api::V1::PostsController do
|
||||||
|
|
||||||
it "doesn't create the post" do
|
it "doesn't create the post" do
|
||||||
json_body = JSON.parse(response.body)
|
json_body = JSON.parse(response.body)
|
||||||
expect(json_body["error"]).to eq("insufficient_scope")
|
expect(json_body["code"]).to eq(403)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -174,7 +174,7 @@ describe Api::V1::PostsController do
|
||||||
|
|
||||||
it "doesn't delete the post" do
|
it "doesn't delete the post" do
|
||||||
json_body = JSON.parse(response.body)
|
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)
|
expect(response.status).to eq(403)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue