Skip to content

Add links attribute and gem setup fixes #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -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
2 changes: 1 addition & 1 deletion lib/jsonapi/error_serializer.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/dummy.rb
Original file line number Diff line number Diff line change
@@ -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
41 changes: 25 additions & 16 deletions spec/errors_spec.rb
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -166,7 +173,8 @@
'source' => nil,
'title' => 'Not Found',
'detail' => nil,
'code' => nil
'code' => nil,
'links' => nil
}
)
end
@@ -185,7 +193,8 @@
'source' => nil,
'title' => 'Internal Server Error',
'detail' => nil,
'code' => nil
'code' => nil,
'links' => nil
}
)
end