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