class JsonApiServer::Error

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"
   }
 }
}

Attributes

error_attrs[RW]

Allowable error attributes.

error[R]

Public Class Methods

new(attrs = {}) click to toggle source
# 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

Public Instance Methods

as_json() click to toggle source

Object that's serializable to json.

# File lib/json_api_server/error.rb, line 51
def as_json
  {
    'jsonapi' => jsonapi,
    'errors' => error_as_array
  }
end

Protected Instance Methods

error_as_array() click to toggle source
# File lib/json_api_server/error.rb, line 60
def error_as_array
  [@error].compact
end