Exception: LunaPark::Errors::Base

Inherits:
StandardError
  • Object
show all
Extended by:
LunaPark::Extensions::Exceptions::Substitutive
Defined in:
lib/luna_park/errors/base.rb

Overview

This class extends standard exception with a few things:

  • define custom message with internalization key, if necessary

  • setup handler behavior - raise or catch

  • determine whether this error should be notified

  • and if it should, define severity level

  • send to handler not only message but also details

Examples:

Fatalism class

module Errors
  class Fatalism < LunaPark::Errors::Base
    message  'You cannot change your destiny', i18n: 'errors.fatalism'
    notify: :info
  end
end

error = Error::Fatalism.new(choose: 'The first one')
error.message # => 'You cannot change your destiny'
error.message(lang: :ru) # => 'Вы не можете выбрать свою судьбу'
error.details # => { :choose => "The first one" }

Direct Known Subclasses

Business, System

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LunaPark::Extensions::Exceptions::Substitutive

extended

Constructor Details

#initialize(msg = nil, notify: nil, **details) ⇒ Base

Create new error

TODO: make guards safe: remove these raises from exception constructor (from runtime)

Examples:

without parameters

error = Fatalism.new
error.message     # => 'You cannot change your destiny'
error.notify_lvl  # => :error
error.notify?     # => true

with custom parameters

@error = Fatalism.new 'Forgive me Kuzma, my feet are frozen', notify: false
error.message     # => 'Forgive Kuzma, my feet froze'
error.notify_lvl  # => :error
error.notify?     # => false

Parameters:

  • msg (defaults to: nil)
    • Message text

  • notify (defaults to: nil)
    • defines notifier behaviour (see #self.notify)

  • details
    • additional information to notifier

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
134
# File 'lib/luna_park/errors/base.rb', line 127

def initialize(msg = nil, notify: nil, **details)
  raise ArgumentError, "Unexpected notify value: #{notify}" unless notify.nil? || NOTIFY_VALUES.include?(notify)

  @message = msg
  @notify  = notify
  @details = details
  super(message)
end

Class Attribute Details

.__default_message_block__Object (readonly)

Proc, that receives details hash: { detail_key => detail_value }



53
54
55
# File 'lib/luna_park/errors/base.rb', line 53

def __default_message_block__
  @__default_message_block__
end

.default_notifyBoolean, Symbol

Explains how this error class will be notified

Returns:

  • (Boolean, Symbol)

    the behavior of the notification



43
44
45
# File 'lib/luna_park/errors/base.rb', line 43

def default_notify
  @default_notify
end

.i18n_keyNilClass, String (readonly)

What the key of the translation was selected for this error

Returns:

  • (NilClass, String)

    internationalization key



48
49
50
# File 'lib/luna_park/errors/base.rb', line 48

def i18n_key
  @i18n_key
end

Instance Attribute Details

#detailsObject (readonly)

It is additional information which extends the notification message

Examples:

error = Fatalism.new('Message text', custom: 'Some important', foo: Foo.new )
error.details # => {:custom=>"Some important", :foo=>#<Foo:0x000055b70ef6c370>}


106
107
108
# File 'lib/luna_park/errors/base.rb', line 106

def details
  @details
end

Class Method Details

.inherited(inheritor) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/luna_park/errors/base.rb', line 78

def inherited(inheritor)
  if __default_message_block__
    inheritor.message(i18n_key: i18n_key, &__default_message_block__)
  elsif i18n_key
    inheritor.message(i18n_key: i18n_key)
  end

  inheritor.default_notify = default_notify

  super
end

.message(txt = nil, i18n_key: nil, i18n: nil, &default_message_block) ⇒ NilClass

Specify default error message

Parameters:

  • txt (String) (defaults to: nil)
    • text of message

  • i18n (String) (defaults to: nil)
    • internationalization key

Returns:

  • (NilClass)


72
73
74
75
76
# File 'lib/luna_park/errors/base.rb', line 72

def message(txt = nil, i18n_key: nil, i18n: nil, &default_message_block)
  @__default_message_block__ = block_given? ? default_message_block : txt && ->(_) { txt }
  @i18n_key = i18n || i18n_key
  nil
end

.notify(lvl) ⇒ NilClass

Specifies the expected behavior of the error handler if an error instance of this class is raised

Parameters:

  • - (Symbol)

    set behavior of the notification (see #default_notify)

Returns:

  • (NilClass)


61
62
63
64
65
# File 'lib/luna_park/errors/base.rb', line 61

def notify(lvl)
  self.default_notify = lvl unless lvl.nil?

  nil
end

Instance Method Details

#message(locale: nil) ⇒ String

Error message

The message text is defined in the following order:

  1. In the ‘initialize` method

  2. Translated message, if i18n key was settled in class (see ‘.message`)

  3. In the class method (see ‘.message`)

Examples:

message is not settled

LunaPark::Errors::Base.new.message # => 'LunaPark::Errors::Base'

message is defined in class

class WrongAnswerError < LunaPark::Errors::Base
  message 'Answer is 42'
end

WrongAnswerError.new.message # => 'Answer is 42'

message is in internatialization config

# I18n YML
# ru:
#   errors:
#     frost: Прости Кузьма, замерзли ноги!

class FrostError < LunaPark::Errors::Base
  message 'Forgive Kuzma, my feet froze', i18n: 'errors.frost'
end

error = FrostError.new
error.message(locale: :ru) # => 'Прости Кузьма, замерзли ноги!'

message is defined in class with block

class WrongAnswerError < LunaPark::Errors::Base
  message { |details| "Answer is '#{details[:correct]}' - not '#{details[:wrong]}'" }
end

error = WrongAnswerError.new(correct: 42, wrong: 420)
error.message # => "Answer is '42' - not '420'"

message is in internalization config with i18n interpolation

# I18n YML
# de:
#   errors:
#     wrong_answer: Die richtige Antwort ist '%{correct}', nicht '%{wrong}'

class WrongAnswerError < LunaPark::Errors::Base
  message i18n: 'errors.wrong_answer'
end

error = WrongAnswerError.new(correct: 42, wrong: 420)
error.message(locale: :de) # => "Die richtige Antwort ist '42', nicht '420'"

Parameters:

  • locale (Symbol, String) (defaults to: nil)

Returns:

  • (String)

    message text



216
217
218
219
220
221
# File 'lib/luna_park/errors/base.rb', line 216

def message(locale: nil)
  return @message if @message

  default_message = build_default_message
  localized_message(locale, show_error: default_message.nil?) || default_message || self.class.name
end

#notify?Boolean

Should the handler send this notification ?

Examples:

notify is undefined

error = LunaPark::Errors::Base
error.notify # => false

Returns:

  • (Boolean)

    it should be notified?



143
144
145
# File 'lib/luna_park/errors/base.rb', line 143

def notify?
  @notify || self.class.default_notify ? true : false
end

#notify_lvlSymbol

Severity level for notificator

Examples:

notify is undefined

error = LunaPark::Errors::Base
error.notify_lvl # => :error

Returns:

  • (Symbol)

    expected notification level



155
156
157
158
159
160
# File 'lib/luna_park/errors/base.rb', line 155

def notify_lvl
  return @notify                   if NOTIFY_LEVELS.include? @notify
  return self.class.default_notify if NOTIFY_LEVELS.include? self.class.default_notify

  DEFAULT_NOTIFY_LEVEL
end