Exception: LontaraUtilities::BaseError

Inherits:
StandardError
  • Object
show all
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.message
end

or inherited by other classes:

class RMQ::ConnectionError < LontaraUtilities::BaseError
  def initialize(message = "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(message = '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 "#{timestamp} | #{code} | #{message}"
    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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = 'BaseError', code: self.class.name, timestamp: Time.now, handler: nil) ⇒ BaseError

Returns a new instance of BaseError.

Parameters:

  • message (String) (defaults to: 'BaseError')

    Error message. Default is ‘LontaraUtilities::BaseError’.

  • code (String) (defaults to: self.class.name)

    Error code. Default is LontaraUtilities::BaseError, or the class name of the inherited class.

  • timestamp (Time) (defaults to: Time.now)

    Error timestamp. Default is Time.now.

  • handler (Proc | Any) (defaults to: nil)

    Error handler. Default is nil.


48
49
50
51
52
53
54
55
# File 'lib/lontara_utilities/base_error.rb', line 48

def initialize(message = 'BaseError', code: self.class.name, timestamp: Time.now, handler: nil)
  @message = message
  @code = code.to_s.split('::').last
  @timestamp = timestamp
  @handler = handler.is_a?(Proc) ? handler.call : handler

  super(message)
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.


57
58
59
# File 'lib/lontara_utilities/base_error.rb', line 57

def code
  @code
end

#handlerObject (readonly)

Returns the value of attribute handler.


57
58
59
# File 'lib/lontara_utilities/base_error.rb', line 57

def handler
  @handler
end

#messageObject (readonly)

Returns the value of attribute message.


57
58
59
# File 'lib/lontara_utilities/base_error.rb', line 57

def message
  @message
end

#timestampObject (readonly)

Returns the value of attribute timestamp.


57
58
59
# File 'lib/lontara_utilities/base_error.rb', line 57

def timestamp
  @timestamp
end