Class: WindowsError::ErrorCode

Inherits:
Object
  • Object
show all
Defined in:
lib/windows_error/error_code.rb

Overview

This is the core class that represents a Windows Error Code. It maps the error code value to the description of the error according to Microsoft documentation found at [Windows Error Codes](msdn.microsoft.com/en-us/library/cc231196.aspx)

Direct Known Subclasses

HResult::HResultCode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, description) ⇒ ErrorCode

Returns a new instance of ErrorCode.

Parameters:

  • name (String)

    the ‘name’ of the error code (i.e STATUS_SUCCESS)

  • value (Integer)

    the return value that represents that error

  • description (String)

    the verbose description of the error

Raises:

  • (ArgumentError)

    if any of the parameters are of an invalid type



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/windows_error/error_code.rb', line 18

def initialize(name, value, description)
  raise ArgumentError, 'Invalid Error Name!' unless name.kind_of? String and !(name.empty?)
  raise ArgumentError, 'Invalid Error Code Value!' unless value.kind_of? Integer
  raise ArgumentError, 'Invalid Error Description!' unless description.kind_of? String and !(description.empty?)
  @name = name
  @value = value
  @description = description
  @name.freeze
  @value.freeze
  @description.freeze
  self.freeze
end

Instance Attribute Details

#descriptionString (readonly)

Returns the description of the error the code represents.

Returns:

  • (String)

    the description of the error the code represents



8
9
10
# File 'lib/windows_error/error_code.rb', line 8

def description
  @description
end

#nameString (readonly)

Returns the name of the error code.

Returns:

  • (String)

    the name of the error code



10
11
12
# File 'lib/windows_error/error_code.rb', line 10

def name
  @name
end

#valueInteger (readonly)

Returns the error code that was given as a return value.

Returns:

  • (Integer)

    the error code that was given as a return value



12
13
14
# File 'lib/windows_error/error_code.rb', line 12

def value
  @value
end

Instance Method Details

#==(other_object) ⇒ Boolean Also known as: ===

Overrides the equality test for ErrorCodes. Equality is always tested against the #value of the error code.

Parameters:

  • other_object (Object)

    the object to test equality against

Returns:

  • (Boolean)

    whether the equality test passed

Raises:

  • (ArgumentError)

    if the other object is not either another ErrorCode or a Integer



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/windows_error/error_code.rb', line 37

def ==(other_object)
  if other_object.kind_of? self.class
    self.value == other_object.value
  elsif other_object.kind_of? Integer
    self.value == other_object
  elsif other_object.nil?
    false
  else
    raise ArgumentError, "Cannot compare a #{self.class} to a #{other_object.class}"
  end
end

#to_sObject



51
52
53
54
# File 'lib/windows_error/error_code.rb', line 51

def to_s
  code = sprintf "%08x", self.value
  "(0x#{code}) #{self.name}: #{self.description}"
end