Exception: LontaraUtilities::BaseError
- Inherits:
-
StandardError
- Object
- StandardError
- LontaraUtilities::BaseError
- Defined in:
- lib/lontara_utilities/base_error.rb
Overview
This Base Error gives you a standard way to raise & handle errors with some additional features.
Use this class directly:
begin
raise LontaraUtilities::BaseError, 'This is a test'
rescue LontaraUtilities::BaseError => e
puts e.
end
or inherited by other classes:
class RMQ::ConnectionError < LontaraUtilities::BaseError
def initialize( = "Can't established connection") = super
end
Note: You must call ‘super` in the initialize method of the inherited class.
Parameter ‘handler` is a Proc or Any. If it is a Proc, it will be called. If it is not a Proc, it will be assigned to the `handler` attribute.
class RMQ::QueueNotDefined < LontaraUtilities::BaseError
def initialize( = 'Queue not defined', handler: -> { logger }) = super
# Write logs to 'rmq.log'
def logger
File.open(File.join(__dir__, 'log', 'rmq.log'), 'a') do |f|
f.puts "#{} | #{code} | #{}"
end
end
end
You also can pass a Backtrace after the message in raise method.
raise LontaraUtilities::BaseError, 'This is a test', caller
Important: You cannot pass other arguments except ‘message`, and `backtrace` to the raise method. `backtrace` only callable from rescue block also.
Direct Known Subclasses
RMQ::Errors::ClientParameterRequired, RMQ::Errors::DefaultExchangeParameterRequired, RMQ::Errors::ExchangeParameterRequired
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Instance Method Summary collapse
-
#initialize(message = 'BaseError', code: self.class.name, timestamp: Time.now, handler: nil) ⇒ BaseError
constructor
A new instance of BaseError.
Constructor Details
#initialize(message = 'BaseError', code: self.class.name, timestamp: Time.now, handler: nil) ⇒ BaseError
Returns a new instance of BaseError.
48 49 50 51 52 53 54 55 |
# File 'lib/lontara_utilities/base_error.rb', line 48 def initialize( = 'BaseError', code: self.class.name, timestamp: Time.now, handler: nil) @message = @code = code.to_s.split('::').last @timestamp = @handler = handler.is_a?(Proc) ? handler.call : handler super() end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
57 58 59 |
# File 'lib/lontara_utilities/base_error.rb', line 57 def code @code end |
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
57 58 59 |
# File 'lib/lontara_utilities/base_error.rb', line 57 def handler @handler end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
57 58 59 |
# File 'lib/lontara_utilities/base_error.rb', line 57 def @message end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
57 58 59 |
# File 'lib/lontara_utilities/base_error.rb', line 57 def @timestamp end |