Exception: KStor::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/kstor/error.rb

Overview

Base class of KStor errors.

Each subclass declares a code and is stored in a global registry.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Error

Returns a new instance of Error.



46
47
48
# File 'lib/kstor/error.rb', line 46

def initialize(*args)
  super(format("ERR/%s #{self.class.message}", self.class.code, *args))
end

Class Attribute Details

.codeObject (readonly)

Returns the value of attribute code.



11
12
13
# File 'lib/kstor/error.rb', line 11

def code
  @code
end

.messageObject (readonly)

Returns the value of attribute message.



12
13
14
# File 'lib/kstor/error.rb', line 12

def message
  @message
end

.registryObject (readonly)

Returns the value of attribute registry.



13
14
15
# File 'lib/kstor/error.rb', line 13

def registry
  @registry
end

Class Method Details

.error_code(str) ⇒ Object



15
16
17
# File 'lib/kstor/error.rb', line 15

def error_code(str)
  @code = str
end

.error_message(str) ⇒ Object



19
20
21
# File 'lib/kstor/error.rb', line 19

def error_message(str)
  @message = str
end

.for_code(code, *args) ⇒ Object



23
24
25
# File 'lib/kstor/error.rb', line 23

def for_code(code, *args)
  @registry[code].new(*args)
end

.inherited(subclass) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kstor/error.rb', line 32

def self.inherited(subclass)
  super
  Log.debug("#{subclass} inherits from Error")
  @registry ||= ErrorRegistry.new
  if @registry.key?(subclass.code)
    code = subclass.code
    klass = @registry[code]
    raise "duplicate error code #{code} in #{subclass}, " \
          "already defined in #{klass}"
  end

  @registry << subclass
end

.listObject



27
28
29
# File 'lib/kstor/error.rb', line 27

def list
  @registry.classes
end

Instance Method Details

#responseObject



50
51
52
# File 'lib/kstor/error.rb', line 50

def response
  Response.new('error', 'code' => self.class.code, 'message' => message)
end