Class: JsonApiServer::Error

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

Overview

Implements a single error based on spec: jsonapi.org/examples/#error-objects.

Serializes to something like this. 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"
   }
 }
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApiVersion

#jsonapi

Methods included from Serializer

#serializer_options, #to_json

Constructor Details

#initialize(attrs = {}) ⇒ Error

Returns a new instance of Error.



40
41
42
43
44
45
46
# File 'lib/json_api_server/error.rb', line 40

def initialize(attrs = {})
  @error =
    if attrs.respond_to?(:keys)
      h = attrs.select { |k, _v| self.class.error_attrs.include?(k.to_s) }
      h.empty? ? nil : h
    end
end

Class Attribute Details

.error_attrsObject

Allowable error attributes.



35
36
37
# File 'lib/json_api_server/error.rb', line 35

def error_attrs
  @error_attrs
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



48
49
50
# File 'lib/json_api_server/error.rb', line 48

def error
  @error
end

Instance Method Details

#as_jsonObject

Object that’s serializable to json.



51
52
53
54
55
56
# File 'lib/json_api_server/error.rb', line 51

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