Module: LunaPark::Extensions::HasErrors::ClassMethods

Defined in:
lib/luna_park/extensions/has_errors.rb

Instance Method Summary collapse

Instance Method Details

#business_error(title, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &default_message_block) ⇒ Object Also known as: error

Define business error

Examples:

class Service
  include LunaPark::Extensions::HasErrors

  business_error(:logic_error) { (1 + 1).to_s }
end

logic_error = Service::LogicError.new
logic_error.is_a? LunaPark::Errors::Business # => true
logic_error.message # => '2'


74
75
76
# File 'lib/luna_park/extensions/has_errors.rb', line 74

def business_error(title, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &default_message_block) # rubocop:disable Metrics/ParameterLists
  custom_error title, Errors::Business, txt, i18n: i18n || i18n_key, notify: notify, &default_message_block
end

#custom_error(title, inherit_from, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &default_message_block) ⇒ Object

Define error with a custom superclass. The superclass must be inherited from LunaPark::Errors::Base.

rubocop:disable Metrics/ParameterLists

Examples:

class BaseError < LunaPark::Errors::Business
  alias description message
end

class Service
  include LunaPark::Extensions::HasErrors

  custom_error :custom_error, BaseError, 'Error message'
end

custom_error = Service::CustomError.new
custom_error.is_a? BaseError # => true
custom_error.description # => 'Error message'


118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/luna_park/extensions/has_errors.rb', line 118

def custom_error(title, inherit_from, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &default_message_block)
  unless inherit_from < Errors::Base
    raise ArgumentError, 'inherit_from must be a superclass of LunaPark::Errors::Base'
  end

  error_class = Class.new(inherit_from)
  error_class.inherited(inherit_from)
  error_class.notify(notify) unless notify.nil?

  message_present = ![txt, i18n || i18n_key, default_message_block].all?(&:nil?)
  error_class.message(txt, i18n: i18n || i18n_key, &default_message_block) if message_present

  const_set(error_class_name(title), error_class)
end

#error_class_name(title) ⇒ Object

Get error class name

Examples:

when title is string

error_class_name('CamelCase') # => 'CamelCase'

when title is symbol

error_class_name(:snake_case) # => 'SnakeCase'

Parameters:

  • title (String|Symbol)
    • short alias for error



144
145
146
147
148
149
150
# File 'lib/luna_park/extensions/has_errors.rb', line 144

def error_class_name(title)
  case title
  when String then title
  when Symbol then title.to_s.split('_').collect!(&:capitalize).join
  else raise ArgumentError, "Unknown type `#{title}` for error title"
  end
end

#system_error(title, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &default_message_block) ⇒ Object

Define business error

Examples:

class Service
  include LunaPark::Extensions::HasErrors

  system_error :tech_error, 'Error message'
end

tech_error = Service::TechError.new
tech_error.is_a? LunaPark::Errors::System # => true
tech_error.message # => 'Error message'


95
96
97
# File 'lib/luna_park/extensions/has_errors.rb', line 95

def system_error(title, txt = nil, i18n_key: nil, i18n: nil, notify: nil, &default_message_block) # rubocop:disable Metrics/ParameterLists
  custom_error title, Errors::System, txt, i18n: i18n || i18n_key, notify: notify, &default_message_block
end