Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/jsonapi/active_model_error_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class ActiveModelErrorSerializer < ErrorSerializer
object.type.to_s.delete("''").parameterize.tr('-', '_')
end

attribute :links do
{ "service-doc": 'https://example.com/docs' }
end

attribute :detail do |object, _params|
object.full_message
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/error_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ErrorSerializer
set_type :error

# Object/Hash attribute helpers.
[:status, :source, :title, :detail, :code].each do |attr_name|
[:status, :source, :title, :detail, :code, :links].each do |attr_name|
attribute attr_name do |object|
object.try(attr_name) || object.try(:fetch, attr_name, nil)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class UserSerializer
end

class Dummy < Rails::Application
secrets.secret_key_base = '_'
config.secret_key_base = '_'
config.hosts << 'www.example.com' if config.respond_to?(:hosts)

routes.draw do
Expand Down
41 changes: 25 additions & 16 deletions spec/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@
{
'status' => '422',
'source' => { 'pointer' => '' },
'title' => 'Unprocessable Entity',
'title' => match(/Unprocessable (Entity|Content)/),
'detail' => nil,
'code' => nil
'code' => nil,
'links' => nil
}
)
end
Expand All @@ -63,9 +64,10 @@
{
'status' => '422',
'source' => { 'pointer' => '/data/relationships/user' },
'title' => 'Unprocessable Entity',
'title' => match(/Unprocessable (Entity|Content)/),
'detail' => expected_detail,
'code' => 'blank'
'code' => 'blank',
'links' => { 'service-doc' => 'https://example.com/docs' }
}
)
end
Expand All @@ -85,23 +87,26 @@
{
'status' => '422',
'source' => { 'pointer' => '/data/attributes/title' },
'title' => 'Unprocessable Entity',
'title' => match(/Unprocessable (Entity|Content)/),
'detail' => 'Title is invalid',
'code' => 'invalid'
'code' => 'invalid',
'links' => { 'service-doc' => 'https://example.com/docs' }
},
{
'status' => '422',
'source' => { 'pointer' => '/data/attributes/title' },
'title' => 'Unprocessable Entity',
'title' => match(/Unprocessable (Entity|Content)/),
'detail' => 'Title has typos',
'code' => 'invalid'
'code' => 'invalid',
'links' => { 'service-doc' => 'https://example.com/docs' }
},
{
'status' => '422',
'source' => { 'pointer' => '/data/attributes/quantity' },
'title' => 'Unprocessable Entity',
'title' => match(/Unprocessable (Entity|Content)/),
'detail' => 'Quantity must be less than 100',
'code' => 'less_than'
'code' => 'less_than',
'links' => { 'service-doc' => 'https://example.com/docs' }
}
)
end
Expand All @@ -121,9 +126,10 @@
{
'status' => '422',
'source' => { 'pointer' => '' },
'title' => 'Unprocessable Entity',
'title' => match(/Unprocessable (Entity|Content)/),
'detail' => 'Title has slurs',
'code' => 'title_has_slurs'
'code' => 'title_has_slurs',
'links' => { 'service-doc' => 'https://example.com/docs' }
}
)
end
Expand All @@ -144,9 +150,10 @@
{
'status' => '422',
'source' => { 'pointer' => '/data/attributes/title' },
'title' => 'Unprocessable Entity',
'title' => match(/Unprocessable (Entity|Content)/),
'detail' => nil,
'code' => nil
'code' => nil,
'links' => nil
}
)
end
Expand All @@ -166,7 +173,8 @@
'source' => nil,
'title' => 'Not Found',
'detail' => nil,
'code' => nil
'code' => nil,
'links' => nil
}
)
end
Expand All @@ -185,7 +193,8 @@
'source' => nil,
'title' => 'Internal Server Error',
'detail' => nil,
'code' => nil
'code' => nil,
'links' => nil
}
)
end
Expand Down