class Grape::Exceptions::Base

Constants

BASE_ATTRIBUTES_KEY
BASE_MESSAGES_KEY
FALLBACK_LOCALE

Attributes

headers[R]
status[R]

Public Class Methods

new(status: nil, message: nil, headers: nil, **_options) click to toggle source
Calls superclass method
# File lib/grape/exceptions/base.rb, line 12
def initialize(status: nil, message: nil, headers: nil, **_options)
  super(message)

  @status  = status
  @headers = headers
end

Public Instance Methods

[](index) click to toggle source
# File lib/grape/exceptions/base.rb, line 19
def [](index)
  send index
end

Protected Instance Methods

compose_message(key, **attributes) click to toggle source

TODO: translate attribute first if BASE_ATTRIBUTES_KEY.key respond to a string message, then short_message is returned if BASE_ATTRIBUTES_KEY.key respond to a Hash, means it may have problem , summary and resolution

# File lib/grape/exceptions/base.rb, line 28
def compose_message(key, **attributes)
  short_message = translate_message(key, **attributes)
  if short_message.is_a? Hash
    @problem = problem(key, **attributes)
    @summary = summary(key, **attributes)
    @resolution = resolution(key, **attributes)
    [['Problem', @problem], ['Summary', @summary], ['Resolution', @resolution]].each_with_object(+'') do |detail_array, message|
      message << "\n#{detail_array[0]}:\n  #{detail_array[1]}" unless detail_array[1].blank?
      message
    end
  else
    short_message
  end
end
fallback_message(key, **options) click to toggle source
# File lib/grape/exceptions/base.rb, line 79
def fallback_message(key, **options)
  if ::I18n.enforce_available_locales && ::I18n.available_locales.exclude?(FALLBACK_LOCALE)
    key
  else
    ::I18n.translate(key, locale: FALLBACK_LOCALE, **options)
  end
end
problem(key, **attributes) click to toggle source
# File lib/grape/exceptions/base.rb, line 43
def problem(key, **attributes)
  translate_message(:"#{key}.problem", **attributes)
end
resolution(key, **attributes) click to toggle source
# File lib/grape/exceptions/base.rb, line 51
def resolution(key, **attributes)
  translate_message(:"#{key}.resolution", **attributes)
end
summary(key, **attributes) click to toggle source
# File lib/grape/exceptions/base.rb, line 47
def summary(key, **attributes)
  translate_message(:"#{key}.summary", **attributes)
end
translate(key, **options) click to toggle source
# File lib/grape/exceptions/base.rb, line 72
def translate(key, **options)
  options = options.dup
  options[:default] &&= options[:default].to_s
  message = ::I18n.translate(key, **options)
  message.presence || fallback_message(key, **options)
end
translate_attributes(keys, **options) click to toggle source
# File lib/grape/exceptions/base.rb, line 55
def translate_attributes(keys, **options)
  keys.map do |key|
    translate("#{BASE_ATTRIBUTES_KEY}.#{key}", default: key, **options)
  end.join(', ')
end
translate_message(key, **options) click to toggle source
# File lib/grape/exceptions/base.rb, line 61
def translate_message(key, **options)
  case key
  when Symbol
    translate("#{BASE_MESSAGES_KEY}.#{key}", default: '', **options)
  when Proc
    key.call
  else
    key
  end
end