Module: ActiveModelSerializers::Adapter::JsonApi::Error

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

Constant Summary collapse

UnknownSourceTypeError =

rubocop:disable Style/AsciiComments

Class.new(ArgumentError)

Class Method Summary collapse

Class Method Details

.attribute_error_objects(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)


49
50
51
52
53
54
55
56
# File 'lib/active_model_serializers/adapter/json_api/error.rb', line 49

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

.error_source(source_type, attribute_name) ⇒ Object

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


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/active_model_serializers/adapter/json_api/error.rb', line 80

def self.error_source(source_type, attribute_name)
  case source_type
  when :pointer
    {
      pointer: ActiveModelSerializers::JsonPointer.new(:attribute, attribute_name)
    }
  when :parameter
    {
      parameter: attribute_name
    }
  else
    fail UnknownSourceTypeError, "Unknown source type '#{source_type}' for attribute_name '#{attribute_name}'"
  end
end

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

Builds a JSON API Errors Object JSON API Errors

Parameters:

Returns:

  • (Array<Symbol, Array<String>>)

    i.e. attribute_name, [attribute_errors]



15
16
17
18
19
20
21
# File 'lib/active_model_serializers/adapter/json_api/error.rb', line 15

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(attribute_name, attribute_errors)
  end
end