active_model_serializers/docs/jsonapi/errors.md
Benjamin Fleischer 96107c56aa Require explicit adapter/serializer to render JSON API errors
- Separate collection errors from resource errors in adapter
- Refactor to ErrorsSerializer; first-class json error methods
- DOCS
- Rails 4.0 requires assert exact exception class, boo
2016-03-06 12:03:17 -06:00

1.8 KiB

Back to Guides

JSON API Errors

Rendering error documents requires specifying the serializer and the adapter:

  • adapter: :'json_api/error'
  • Serializer:
    • For a single resource: serializer: ActiveModel::Serializer::ErrorSerializer.
    • For a collection: serializer: ActiveModel::Serializer::ErrorsSerializer, each_serializer: ActiveModel::Serializer::ErrorSerializer.

The resource MUST have a non-empty associated #errors object. The errors object must have a #messages method that returns a hash of error name to array of descriptions.

Use in controllers

resource = Profile.new(name: 'Name 1',
                       description: 'Description 1',
                       comments: 'Comments 1')
resource.errors.add(:name, 'cannot be nil')
resource.errors.add(:name, 'must be longer')
resource.errors.add(:id, 'must be a uuid')

render json: resource, status: 422, adapter: 'json_api/error', serializer: ActiveModel::Serializer::ErrorSerializer
# #=>
#  { :errors =>
#    [
#      { :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' },
#      { :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' },
#      { :source => { :pointer => '/data/attributes/id' }, :detail => 'must be a uuid' }
#    ]
#  }.to_json

Direct error document generation

options = nil
resource = ModelWithErrors.new
resource.errors.add(:name, 'must be awesome')

serializable_resource = ActiveModel::SerializableResource.new(
  resource, {
    serializer: ActiveModel::Serializer::ErrorSerializer,
    adapter: 'json_api/error'
  })
serializable_resource.as_json(options)
# #=>
# {
#   :errors =>
#     [
#       { :source => { :pointer => '/data/attributes/name' }, :detail => 'must be awesome' }
#     ]
# }