Class: JsonApiServer::Errors

Inherits:
Object
  • Object
show all
Includes:
ApiVersion, Serializer
Defined in:
lib/json_api_server/errors.rb

Overview

For one or more errors: jsonapi.org/examples/#error-objects.

Serializes to an arrray of errors. Skips attributes that are nil. Ignores non-jsonapi attributes.

error.to_json
{
 ":jsonapi": {
   ":version": "1.0"
 },
 ":errors": [{
   ":id": 1234
   ":status": "422",
   ":code": 5,
   ":source": {
     ":pointer": "/data/attributes/first-name"
   },
   ":title": "Invalid Attribute",
   ":detail": "First name must contain at least three characters.",
   ":meta": {
     ":attrs": [1,2,3]
   },
   ":links": {
     ":self": "http://example.com/user"
   }
 }]
}

Use for singular or multiple errors.

Instance Method Summary collapse

Methods included from ApiVersion

#jsonapi

Methods included from Serializer

#serializer_options, #to_json

Constructor Details

#initialize(errors) ⇒ Errors

Returns a new instance of Errors.



34
35
36
37
38
39
40
41
# File 'lib/json_api_server/errors.rb', line 34

def initialize(errors)
  errors = errors.is_a?(Array) ? errors : [errors]
  @errors = errors.map do |error|
    JsonApiServer::Error.new(error).error
  end
  @errors.compact!
  @errors
end

Instance Method Details

#as_jsonObject



43
44
45
46
47
48
# File 'lib/json_api_server/errors.rb', line 43

def as_json
  {
    'jsonapi' => jsonapi,
    'errors' => @errors
  }
end