Exception: LunaPark::Errors::Base
- Inherits:
-
StandardError
- Object
- StandardError
- LunaPark::Errors::Base
- 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
Class Attribute Summary collapse
-
.__default_message_block__ ⇒ Object
readonly
Proc, that receives details hash: { detail_key => detail_value }.
-
.default_notify ⇒ Boolean, Symbol
readonly
Explains how this error class will be notified.
-
.i18n_key ⇒ NilClass, String
readonly
What the key of the translation was selected for this error.
Instance Attribute Summary collapse
-
#details ⇒ Object
readonly
It is additional information which extends the notification message.
Class Method Summary collapse
- .inherited(inheritor) ⇒ Object
-
.message(txt = nil, i18n_key: nil, i18n: nil, &default_message_block) ⇒ NilClass
Specify default error message.
-
.notify(lvl) ⇒ NilClass
Specifies the expected behavior of the error handler if an error instance of this class is raised.
Instance Method Summary collapse
-
#initialize(msg = nil, notify: nil, **details) ⇒ Base
constructor
Create new error.
-
#message(locale: nil) ⇒ String
Error message.
-
#notify? ⇒ Boolean
Should the handler send this notification ?.
-
#notify_lvl ⇒ Symbol
Severity level for notificator.
Methods included from LunaPark::Extensions::Exceptions::Substitutive
Constructor Details
#initialize(msg = nil, notify: nil, **details) ⇒ Base
Create new error
TODO: make guards safe: remove these raises from exception constructor (from runtime)
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) = msg @notify = notify @details = details super() 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 end |
.default_notify ⇒ Boolean, Symbol
Explains how this error class will be notified
43 44 45 |
# File 'lib/luna_park/errors/base.rb', line 43 def default_notify @default_notify end |
.i18n_key ⇒ NilClass, String (readonly)
What the key of the translation was selected for this error
48 49 50 |
# File 'lib/luna_park/errors/base.rb', line 48 def i18n_key @i18n_key end |
Instance Attribute Details
#details ⇒ Object (readonly)
It is additional information which extends the notification message
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 inheritor.(i18n_key: i18n_key, &) elsif i18n_key inheritor.(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
72 73 74 75 76 |
# File 'lib/luna_park/errors/base.rb', line 72 def (txt = nil, i18n_key: nil, i18n: nil, &) = block_given? ? : 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
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:
-
In the ‘initialize` method
-
Translated message, if i18n key was settled in class (see ‘.message`)
-
In the class method (see ‘.message`)
216 217 218 219 220 221 |
# File 'lib/luna_park/errors/base.rb', line 216 def (locale: nil) return if = (locale, show_error: .nil?) || || self.class.name end |
#notify? ⇒ Boolean
Should the handler send this notification ?
143 144 145 |
# File 'lib/luna_park/errors/base.rb', line 143 def notify? @notify || self.class.default_notify ? true : false end |
#notify_lvl ⇒ Symbol
Severity level for notificator
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 |