module Railsful::Interceptors::Errors

This interceptor checks the given json object for an 'errors' array and checks if any errors are available.

Public Instance Methods

errors(raw_errors) click to toggle source

Transform error output format into more “jsonapi” like.

# File lib/railsful/interceptors/errors.rb, line 25
def errors(raw_errors)
  errors = []

  raw_errors.details.each do |field, array|
    errors += field_errors(field, array)
  end

  errors
end
errors?(options) click to toggle source

Checks if given renderable is an ActiveModel::Error

:reek: UtilityFunction

# File lib/railsful/interceptors/errors.rb, line 54
def errors?(options)
  return false unless options

  options.fetch(:json, nil).is_a?(ActiveModel::Errors)
end
errors_options(options) click to toggle source
# File lib/railsful/interceptors/errors.rb, line 14
def errors_options(options)
  return options unless errors?(options)

  # Fetch all the errors from the passed json value.
  errors = errors(options.fetch(:json))

  # Overwrite the json value and set the errors array.
  options.merge(json: { errors: errors })
end
field_errors(field, array) click to toggle source
# File lib/railsful/interceptors/errors.rb, line 35
def field_errors(field, array)
  array.map do |hash|
    formatted_error(hash, field)
  end
end
formatted_error(hash, field) click to toggle source

Format the error by adding additional status and field information.

:reek: UtilityFunction

# File lib/railsful/interceptors/errors.rb, line 44
def formatted_error(hash, field)
  {
    status: '422',
    field: field
  }.merge(hash)
end
render(options) click to toggle source
Calls superclass method
# File lib/railsful/interceptors/errors.rb, line 10
def render(options)
  super(errors_options(options))
end