From b74d0028914b03bce785b940f0cad5390444ae3a Mon Sep 17 00:00:00 2001 From: Matt Wrock Date: Wed, 3 Feb 2016 09:58:18 -0800 Subject: fixes #4515 and use FormatMessage to get correct error messages from native API instead of retyping them with our fingers --- lib/chef/exceptions.rb | 16 --------- lib/chef/util/windows/net_group.rb | 24 ++++++------- lib/chef/win32/api/error.rb | 1 + lib/chef/win32/api/net.rb | 10 ------ lib/chef/win32/error.rb | 18 ++++++---- lib/chef/win32/net.rb | 63 ++++++++-------------------------- spec/functional/resource/group_spec.rb | 2 +- 7 files changed, 40 insertions(+), 94 deletions(-) diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb index 6a0cdb9b95..9a2e9ae3a3 100644 --- a/lib/chef/exceptions.rb +++ b/lib/chef/exceptions.rb @@ -138,22 +138,6 @@ class Chef # Errors originating from calls to the Win32 API class Win32APIError < RuntimeError; end - class Win32NetAPIError < Win32APIError - attr_reader :msg, :error_code - def initialize(msg, error_code) - @msg = msg - @error_code = error_code - - formatted_message = "" - formatted_message << "---- Begin Win32 API output ----\n" - formatted_message << "Net Api Error Code: #{error_code}\n" - formatted_message << "Net Api Error Message: #{msg}\n" - formatted_message << "---- End Win32 API output ----\n" - - super(formatted_message) - end - end - # Thrown when Win32 API layer binds to non-existent Win32 function. Occurs # when older versions of Windows don't support newer Win32 API functions. class Win32APIFunctionNotImplemented < NotImplementedError; end diff --git a/lib/chef/util/windows/net_group.rb b/lib/chef/util/windows/net_group.rb index f1c510591e..cfb06ed1f1 100644 --- a/lib/chef/util/windows/net_group.rb +++ b/lib/chef/util/windows/net_group.rb @@ -37,40 +37,40 @@ class Chef::Util::Windows::NetGroup def local_get_members begin Chef::ReservedNames::Win32::NetUser::net_local_group_get_members(nil, groupname) - rescue Chef::Exceptions::Win32NetAPIError => e - raise ArgumentError, e.msg + rescue Chef::Exceptions::Win32APIError => e + raise ArgumentError, e end end def local_add begin Chef::ReservedNames::Win32::NetUser::net_local_group_add(nil, groupname) - rescue Chef::Exceptions::Win32NetAPIError => e - raise ArgumentError, e.msg + rescue Chef::Exceptions::Win32APIError => e + raise ArgumentError, e end end def local_set_members(members) begin Chef::ReservedNames::Win32::NetUser::net_local_group_set_members(nil, groupname, members) - rescue Chef::Exceptions::Win32NetAPIError => e - raise ArgumentError, e.msg + rescue Chef::Exceptions::Win32APIError => e + raise ArgumentError, e end end def local_add_members(members) begin Chef::ReservedNames::Win32::NetUser::net_local_group_add_members(nil, groupname, members) - rescue Chef::Exceptions::Win32NetAPIError => e - raise ArgumentError, e.msg + rescue Chef::Exceptions::Win32APIError => e + raise ArgumentError, e end end def local_delete_members(members) begin Chef::ReservedNames::Win32::NetUser::net_local_group_del_members(nil, groupname, members) - rescue Chef::Exceptions::Win32NetAPIError => e - raise ArgumentError, e.msg + rescue Chef::Exceptions::Win32APIError => e + raise ArgumentError, e end end @@ -78,8 +78,8 @@ class Chef::Util::Windows::NetGroup def local_delete begin Chef::ReservedNames::Win32::NetUser::net_local_group_del(nil, groupname) - rescue Chef::Exceptions::Win32NetAPIError => e - raise ArgumentError, e.msg + rescue Chef::Exceptions::Win32APIError => e + raise ArgumentError, e end end end diff --git a/lib/chef/win32/api/error.rb b/lib/chef/win32/api/error.rb index ecc1625353..0b10fc6e3d 100644 --- a/lib/chef/win32/api/error.rb +++ b/lib/chef/win32/api/error.rb @@ -848,6 +848,7 @@ class Chef ERROR_INVALID_COLORINDEX = 2022 ERROR_CONNECTED_OTHER_PASSWORD = 2108 ERROR_BAD_USERNAME = 2202 + ERROR_USER_NOT_FOUND = 2221 ERROR_NOT_CONNECTED = 2250 ERROR_OPEN_FILES = 2401 ERROR_ACTIVE_CONNECTIONS = 2402 diff --git a/lib/chef/win32/api/net.rb b/lib/chef/win32/api/net.rb index 848499b403..fdbe55579f 100644 --- a/lib/chef/win32/api/net.rb +++ b/lib/chef/win32/api/net.rb @@ -46,16 +46,6 @@ class Chef USE_LOTS_OF_FORCE = 2 #every windows API should support this flag NERR_Success = 0 - NERR_InvalidComputer = 2351 - NERR_NotPrimary = 2226 - NERR_SpeGroupOp = 2234 - NERR_LastAdmin = 2452 - NERR_BadUsername = 2202 - NERR_BadPassword = 2203 - NERR_PasswordTooShort = 2245 - NERR_UserNotFound = 2221 - NERR_GroupNotFound = 2220 - ERROR_ACCESS_DENIED = 5 ERROR_MORE_DATA = 234 ffi_lib "netapi32" diff --git a/lib/chef/win32/error.rb b/lib/chef/win32/error.rb index 1b3c104e85..8c3ff2f08f 100644 --- a/lib/chef/win32/error.rb +++ b/lib/chef/win32/error.rb @@ -59,13 +59,17 @@ class Chef # Chef::Exceptions::Win32APIError::: def self.raise!(message = nil, code = get_last_error) msg = format_message(code).strip - formatted_message = "" - formatted_message << message if message - formatted_message << "---- Begin Win32 API output ----\n" - formatted_message << "System Error Code: #{code}\n" - formatted_message << "System Error Message: #{msg}\n" - formatted_message << "---- End Win32 API output ----\n" - raise Chef::Exceptions::Win32APIError, msg + "\n" + formatted_message + if code == ERROR_USER_NOT_FOUND + raise Chef::Exceptions::UserIDNotFound, msg + else + formatted_message = "" + formatted_message << message if message + formatted_message << "---- Begin Win32 API output ----\n" + formatted_message << "System Error Code: #{code}\n" + formatted_message << "System Error Message: #{msg}\n" + formatted_message << "---- End Win32 API output ----\n" + raise Chef::Exceptions::Win32APIError, msg + "\n" + formatted_message + end end end end diff --git a/lib/chef/win32/net.rb b/lib/chef/win32/net.rb index 62eb869195..4ee278aa28 100644 --- a/lib/chef/win32/net.rb +++ b/lib/chef/win32/net.rb @@ -69,39 +69,6 @@ class Chef end end - def self.net_api_error!(code) - msg = case code - when NERR_InvalidComputer - "The user does not have access to the requested information." - when NERR_NotPrimary - "The operation is allowed only on the primary domain controller of the domain." - when NERR_SpeGroupOp - "This operation is not allowed on this special group." - when NERR_LastAdmin - "This operation is not allowed on the last administrative account." - when NERR_BadUsername - "The user name or group name parameter is invalid." - when NERR_BadPassword - "The password parameter is invalid." - when NERR_UserNotFound - raise Chef::Exceptions::UserIDNotFound, code - when NERR_PasswordTooShort - <