class Puppet::Util::Windows::EventLog::EventLogError

represents an error resulting from a Win32 error code

Constants

ERROR_ACCESS_DENIED
ERROR_FILE_NOT_FOUND
FORMAT_MESSAGE_ALLOCATE_BUFFER
FORMAT_MESSAGE_ARGUMENT_ARRAY
FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_IGNORE_INSERTS
FORMAT_MESSAGE_MAX_WIDTH_MASK

Attributes

code[R]

Public Class Methods

format_error_code(code) click to toggle source

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

   # File lib/puppet/util/windows/error.rb
22 def self.format_error_code(code)
23   # specifying 0 will look for LANGID in the following order
24   # 1.Language neutral
25   # 2.Thread LANGID, based on the thread's locale value
26   # 3.User default LANGID, based on the user's default locale value
27   # 4.System default LANGID, based on the system default locale value
28   # 5.US English
29   dwLanguageId = 0
30   flags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
31           FORMAT_MESSAGE_FROM_SYSTEM |
32           FORMAT_MESSAGE_ARGUMENT_ARRAY |
33           FORMAT_MESSAGE_IGNORE_INSERTS |
34           FORMAT_MESSAGE_MAX_WIDTH_MASK
35   error_string = String.new
36 
37   # this pointer actually points to a :lpwstr (pointer) since we're letting Windows allocate for us
38   FFI::MemoryPointer.new(:pointer, 1) do |buffer_ptr|
39     length = FormatMessageW(flags, FFI::Pointer::NULL, code, dwLanguageId,
40       buffer_ptr, 0, FFI::Pointer::NULL)
41 
42     if length == FFI::WIN32_FALSE
43       # can't raise same error type here or potentially recurse infinitely
44       raise Puppet::Error.new(_("FormatMessageW could not format code %{code}") % { code: code })
45     end
46 
47     # returns an FFI::Pointer with autorelease set to false, which is what we want
48     buffer_ptr.read_win32_local_pointer do |wide_string_ptr|
49       if wide_string_ptr.null?
50         raise Puppet::Error.new(_("FormatMessageW failed to allocate buffer for code %{code}") % { code: code })
51       end
52 
53       error_string = wide_string_ptr.read_wide_string(length)
54     end
55   end
56 
57   error_string
58 end
new(message, code = FFI.errno, original = nil) click to toggle source

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

Calls superclass method Puppet::Error::new
   # File lib/puppet/util/windows/error.rb
15 def initialize(message, code = FFI.errno, original = nil)
16   super(message + ":  #{self.class.format_error_code(code)}", original)
17 
18   @code = code
19 end