Open ofl opened 6 years ago
Error -> symbol
Rails.application.config.action_dispatch.rescue_responses
symbol -> status_code
Rack::Utils::SYMBOL_TO_STATUS_CODE
{"error":{"latitude":["is not a number"],"longitude":["is not a number"]}}
Handling Errors in an API Application the Rails Way
Providing useful error responses in a Rails API
Building APIs with Rails: Handling Errors Nicely
{
"errors": [
{
"source": {
"pointer": "/data/attributes/name"
},
"detail": "can't be blank"
},
{
"source": {
"pointer": "/data/attributes/description"
},
"detail": "can't be blank"
}
]
}
How to Build Rails APIs Following the json:api Spec
{
errors: [
status: 422,
source: {pointer: "/data/attributes/rooms"},
detail: "Must be present."
]
}
{
errors: [
{
code: 'unprocessable_entity',
status: '422',
title: 'Validation Error',
detail: "Title can't be blank",
source: { pointer: '/data/attributes/title' },
meta: {
attribute: 'title',
message: "can't be blank"
code: 'blank'
}
}
]
}
{
errors: [
{
code: 'unprocessable_entity',
status: '422',
title: 'Validation Error',
detail: "Body can't be blank",
source: { pointer: '/data/attributes/body' },
meta: {
relationship: {
attribute: 'body',
message: "can't be blank"
code: 'blank'
id: '123',
type: 'comments'
}
}
}
]
}
https://github.com/Netflix/fast_jsonapi/issues/53
class ErrorSerializer
def initialize(model)
@model = model
end
def serialized_json
errors = @model.errors.messages.map do |field, errors|
errors.map do |error_message|
{
source: {pointer: "/data/attributes/#{field}"},
detail: error_message
}
end
end
@model.class.reflect_on_all_associations.each do |relationship|
@model.send(relationship.name).each_with_index do |child, index|
errors << child.errors.messages.map do |field, errors|
errors.map do |error_message|
{
source: {pointer: "/data/attributes/#{child.model_name.plural}[#{index}].#{field}"},
detail: error_message
}
end
end
end
end
errors.flatten
end
end
Railsアプリケーションにおけるエラー処理(例外設計)の考え方 RailsでAPIをつくるときのエラー処理
【Rails】独自例外の置き場所(配置ディレクトリ)について RailsでAPIを作るときのエラー処理について rubyでエラーコード付きの独自例外クラスを作成 + カスタムloggerで出力