Exception: Puppet::Util::Windows::Error Private

Inherits:
Error
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/puppet/util/windows/error.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

represents an error resulting from a Win32 error code

Constant Summary collapse

ERROR_FILE_NOT_FOUND =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

2
ERROR_ACCESS_DENIED =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

5
FORMAT_MESSAGE_ALLOCATE_BUFFER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

0x00000100
FORMAT_MESSAGE_IGNORE_INSERTS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

0x00000200
FORMAT_MESSAGE_FROM_SYSTEM =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

0x00001000
FORMAT_MESSAGE_ARGUMENT_ARRAY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

0x00002000
FORMAT_MESSAGE_MAX_WIDTH_MASK =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

0x000000FF

Instance Attribute Summary collapse

Attributes inherited from Error

#original

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, code = FFI.errno, original = nil) ⇒ Error

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

NOTE: FFI.errno only works properly when prior Win32 calls have been made through FFI bindings. Calls made through Win32API do not have their error codes captured by FFI.errno



15
16
17
18
19
# File 'lib/puppet/util/windows/error.rb', line 15

def initialize(message, code = FFI.errno, original = nil)
  super(message + ":  #{self.class.format_error_code(code)}", original)

  @code = code
end

Instance Attribute Details

#codeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
# File 'lib/puppet/util/windows/error.rb', line 10

def code
  @code
end

Class Method Details

.format_error_code(code) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper method that wraps FormatMessage that returns a human readable string.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/puppet/util/windows/error.rb', line 22

def self.format_error_code(code)
  # specifying 0 will look for LANGID in the following order
  # 1.Language neutral
  # 2.Thread LANGID, based on the thread's locale value
  # 3.User default LANGID, based on the user's default locale value
  # 4.System default LANGID, based on the system default locale value
  # 5.US English
  dwLanguageId = 0
  flags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
          FORMAT_MESSAGE_FROM_SYSTEM |
          FORMAT_MESSAGE_ARGUMENT_ARRAY |
          FORMAT_MESSAGE_IGNORE_INSERTS |
          FORMAT_MESSAGE_MAX_WIDTH_MASK
  error_string = ''.dup

  # this pointer actually points to a :lpwstr (pointer) since we're letting Windows allocate for us
  FFI::MemoryPointer.new(:pointer, 1) do |buffer_ptr|
    length = FormatMessageW(flags, FFI::Pointer::NULL, code, dwLanguageId,
      buffer_ptr, 0, FFI::Pointer::NULL)

    if length == FFI::WIN32_FALSE
      # can't raise same error type here or potentially recurse infinitely
      raise Puppet::Error.new(_("FormatMessageW could not format code %{code}") % { code: code })
    end

    # returns an FFI::Pointer with autorelease set to false, which is what we want
    buffer_ptr.read_win32_local_pointer do |wide_string_ptr|
      if wide_string_ptr.null?
        raise Puppet::Error.new(_("FormatMessageW failed to allocate buffer for code %{code}") % { code: code })
      end

      error_string = wide_string_ptr.read_wide_string(length)
    end
  end

  error_string
end