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/win32/api/error.rb | 1 + lib/chef/win32/api/net.rb | 10 ------- lib/chef/win32/error.rb | 18 ++++++++----- lib/chef/win32/net.rb | 63 +++++++++++---------------------------------- 4 files changed, 27 insertions(+), 65 deletions(-) (limited to 'lib/chef/win32') 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 - <