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) ⇒ KStor::Error

Create new error.

Parameters:

  • args (Array)

    arguments to String#format with the message as a format string.



66
67
68
# File 'lib/kstor/error.rb', line 66

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

Declare error code for a subclass.

Parameters:

  • str (String)

    unique error code.



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

def error_code(str)
  @code = str
end

.error_message(str) ⇒ Object

Declare error message for a subclass.

This will be passed to String#format as a format string.

Parameters:

  • str (String)

    error message or format.



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

def error_message(str)
  @message = str
end

.for_code(code, *args) ⇒ Object

Create a new error for this code.

Parameters:

  • code (String)

    error code

  • args (Array)

    arguments to subclass initialize method.



35
36
37
# File 'lib/kstor/error.rb', line 35

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

.inherited(subclass) ⇒ Object

When subclassed, add child to registry.

Parameters:

  • subclass (Class)

    subclass to add to error registry.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kstor/error.rb', line 48

def self.inherited(subclass)
  super
  @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

List of all subclasses.



40
41
42
# File 'lib/kstor/error.rb', line 40

def list
  @registry.classes
end

Instance Method Details

#response(sid) ⇒ KStor::Message::Error

Create a new server response from an error.

Returns:



73
74
75
76
77
78
# File 'lib/kstor/error.rb', line 73

def response(sid)
  Message::Error.new(
    { 'code' => self.class.code, 'message' => message },
    { session_id: sid }
  )
end