Exception: RightScale::Platform::Win32Error

Inherits:
Exceptions::PlatformError show all
Defined in:
lib/right_agent/platform/windows/platform.rb

Overview

exceptions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, error_code = nil) ⇒ Win32Error

Returns a new instance of Win32Error.

Parameters:

  • context (String)

    for message

  • error_code (Integer) (defaults to: nil)

    for formatting or nil for last error



48
49
50
51
# File 'lib/right_agent/platform/windows/platform.rb', line 48

def initialize(context, error_code = nil)
  @error_code = error_code || ::RightScale::Platform.windows_common.GetLastError()
  super(format_error_message(context, @error_code))
end

Instance Attribute Details

#error_codeObject (readonly)

Returns the value of attribute error_code.



44
45
46
# File 'lib/right_agent/platform/windows/platform.rb', line 44

def error_code
  @error_code
end

Instance Method Details

#error_message_for(error_code) ⇒ String

Queries raw error message for given error code.

Parameters:

  • error_code (Integer)

    for query

Returns:

  • (String)

    system error message or empty



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/right_agent/platform/windows/platform.rb', line 73

def error_message_for(error_code)
  # this API won't respond with required buffer size if buffer is too
  # small; it returns zero and there is no way to tell if there is no
  # valid message or the buffer is too small. use a 4KB buffer to avoid
  # failure due to buffer size.
  buffer = 0.chr * 4096
  flags = ::RightScale::Platform::WindowsCommon::FORMAT_MESSAGE_FROM_SYSTEM |
          ::RightScale::Platform::WindowsCommon::FORMAT_MESSAGE_IGNORE_INSERTS
  length = ::RightScale::Platform.windows_common.FormatMessage(
    flags, nil, error_code, 0, buffer, buffer.size, nil)
  buffer[0, length].strip
end

#format_error_message(context, error_code) ⇒ String

Formats using system error message, if any.

Parameters:

  • context (String)

    for message

  • error_code (Integer)

    for formatting

Returns:

  • (String)

    formatted error message



59
60
61
62
63
64
65
66
# File 'lib/right_agent/platform/windows/platform.rb', line 59

def format_error_message(context, error_code)
  error_message = error_message_for(error_code)
  result = []
  result << context.to_s if context && !context.empty?
  result << "Win32 error code = #{error_code}"
  result << error_message if error_message && !error_message.empty?
  result.join("\n")
end