module Caprese::Adapter::JsonApi::Error

Constants

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
UnknownSourceTypeError

rubocop:disable Style/AsciiComments

Public Class Methods

attribute_error_objects(record, attribute_name, attribute_errors) click to toggle source

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)
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Builds a JSON API Errors Object {jsonapi.org/format/#errors JSON API Errors}

@param [Caprese::Serializer::ErrorSerializer] error_serializer @return [Array<Symbol, Array<String>>] i.e. attribute_name, [attribute_errors]

# 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