Module: Caprese::Adapter::JsonApi::Error

Defined in:
lib/caprese/adapter/json_api/error.rb

Constant Summary collapse

UnknownSourceTypeError =

rubocop:disable Style/AsciiComments

Class.new(ArgumentError)
RESERVED_ATTRIBUTES =

errorSource description:

oneOf
  ☑ pointer   : String
  ☑ parameter : String

description:

pointer: A JSON Pointer RFC6901 to the associated entity in the request document e.g. "/data"
for a primary data object, or "/data/attributes/title" for a specific attribute.
https://tools.ietf.org/html/rfc6901

parameter: A string indicating which query parameter caused the error

structure:

if is_attribute?
  {
    pointer: '/data/attributes/red-button'
  }
else
  {
    parameter: 'pres'
  }
end
%w(id type)

Class Method Summary collapse

Class Method Details

.attribute_error_objects(record, attribute_name, attribute_errors) ⇒ Object

definition:

JSON Object

properties:

☐ id      : String
☐ status  : String
☐ code    : String
☐ title   : String
☑ detail  : String
☐ links
☐ meta
☑ error_source

description:

id     : A unique identifier for this particular occurrence of the problem.
status : The HTTP status code applicable to this problem, expressed as a string value
code   : An application-specific error code, expressed as a string value.
title  : A short, human-readable summary of the problem. It **SHOULD NOT** change from
  occurrence to occurrence of the problem, except for purposes of localization.
detail : A human-readable explanation specific to this occurrence of the problem.

structure:

{
  title: 'SystemFailure',
  detail: 'something went terribly wrong',
  status: '500'
}.merge!(errorSource)


71
72
73
74
75
76
77
78
79
# File 'lib/caprese/adapter/json_api/error.rb', line 71

def self.attribute_error_objects(record, attribute_name, attribute_errors)
  attribute_errors.map do |attribute_error|
    {
      source: error_source(:pointer, record, attribute_name),
      code: attribute_error[:code],
      detail: attribute_error[:message]
    }
  end
end

.document_errors(error_serializer, options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/caprese/adapter/json_api/error.rb', line 21

def self.document_errors(error_serializer, options)
  error_attributes = error_serializer.as_json
  [
    {
      code: error_attributes[:code],
      detail: error_attributes[:message],
      source: error_source(:pointer, nil, error_attributes[:field])
    }
  ]
end

.error_source(source_type, record, attribute_name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/caprese/adapter/json_api/error.rb', line 104

def self.error_source(source_type, record, attribute_name)
  case source_type
  when :pointer
    if attribute_name == :base
      {
        pointer: JsonApi::JsonPointer.new(:base, record, attribute_name)
      }
    # [type ...] and other primary data variables
    elsif RESERVED_ATTRIBUTES.include?(attribute_name.to_s)
      {
        pointer: JsonApi::JsonPointer.new(:primary_data, record, attribute_name)
      }
    elsif record.has_attribute?(attribute_name) || record.caprese_is_attribute?(attribute_name)
      {
        pointer: JsonApi::JsonPointer.new(:attribute, record, attribute_name)
      }
    elsif (relationship_data_items = attribute_name.to_s.split('.')).size > 1
      if RESERVED_ATTRIBUTES.include?(relationship_data_items.last)
        {
          pointer: JsonApi::JsonPointer.new(:relationship_primary_data, record, relationship_data_items)
        }
      else
        {
          pointer: JsonApi::JsonPointer.new(:relationship_attribute, record, attribute_name)
        }
      end
    else
      {
        pointer: JsonApi::JsonPointer.new(:relationship_base, record, attribute_name)
      }
    end
  when :parameter
    {
      parameter: attribute_name
    }
  else
    fail UnknownSourceTypeError, "Unknown source type '#{source_type}' for attribute_name '#{attribute_name}'"
  end
end

.param_errors(error_serializer, options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/caprese/adapter/json_api/error.rb', line 10

def self.param_errors(error_serializer, options)
  error_attributes = error_serializer.as_json
  [
    {
      code: error_attributes[:code],
      detail: error_attributes[:message],
      source: error_source(:parameter, nil, error_attributes[:field])
    }
  ]
end

.resource_errors(error_serializer, options) ⇒ Array<Symbol, Array<String>>

Builds a JSON API Errors Object JSON API Errors



37
38
39
40
41
42
43
# File 'lib/caprese/adapter/json_api/error.rb', line 37

def self.resource_errors(error_serializer, options)
  error_serializer.as_json.flat_map do |attribute_name, attribute_errors|
    attribute_name = JsonApi.send(:transform_key_casing!, attribute_name,
      options)
    attribute_error_objects(error_serializer.object.record, attribute_name, attribute_errors)
  end
end