summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAdam Edwards <adamed@opscode.com>2014-05-17 07:38:07 -0700
committerAdam Edwards <adamed@opscode.com>2014-05-17 07:38:07 -0700
commitaa224a92ce949f1651465e21e68fbf72c0bb7993 (patch)
tree0bc19282bdb7c38a9d89c260fb2ffb93bd14e15d /lib
parentcabc5171e3d65b9a9a9831a0a07588f20c90c3e7 (diff)
downloadwmi-lite-aa224a92ce949f1651465e21e68fbf72c0bb7993.tar.gz
Better default error messages, refactor error message code
Diffstat (limited to 'lib')
-rw-r--r--lib/wmi-lite/wmi.rb10
-rw-r--r--lib/wmi-lite/wmi_exception.rb45
2 files changed, 44 insertions, 11 deletions
diff --git a/lib/wmi-lite/wmi.rb b/lib/wmi-lite/wmi.rb
index b712ba9..6c39479 100644
--- a/lib/wmi-lite/wmi.rb
+++ b/lib/wmi-lite/wmi.rb
@@ -61,12 +61,12 @@ module WmiLite
def start_query(wql_query, diagnostic_class_name = nil)
result = nil
+ connect_to_namespace
begin
- connect_to_namespace
result = @connection.ExecQuery(wql_query)
raise_if_failed(result)
rescue WIN32OLERuntimeError => native_exception
- raise WmiException.new(native_exception, @namespace, wql_query, diagnostic_class_name)
+ raise WmiException.new(native_exception, :ExecQuery, @namespace, wql_query, diagnostic_class_name)
end
result
end
@@ -82,7 +82,11 @@ module WmiLite
if @connection.nil?
namespace = @namespace.nil? ? 'root/cimv2' : @namespace
locator = WIN32OLE.new("WbemScripting.SWbemLocator")
- @connection = locator.ConnectServer('.', namespace)
+ begin
+ @connection = locator.ConnectServer('.', namespace)
+ rescue WIN32OLERuntimeError => native_exception
+ raise WmiException.new(native_exception, :ConnectServer, @namespace)
+ end
end
end
diff --git a/lib/wmi-lite/wmi_exception.rb b/lib/wmi-lite/wmi_exception.rb
index 85c6387..9b23247 100644
--- a/lib/wmi-lite/wmi_exception.rb
+++ b/lib/wmi-lite/wmi_exception.rb
@@ -18,30 +18,59 @@
module WmiLite
class WmiException < Exception
- def initialize(exception, namespace, query, class_name)
+ def initialize(exception, wmi_method_context, namespace, query = nil, class_name = nil)
error_message = exception.message
+ error_code = translate_error_code(error_message)
+
+ case wmi_method_context
+ when :ConnectServer
+ error_message = translate_wmi_connect_error_message(error_message, error_code, namespace)
+ when :ExecQuery
+ error_message = translate_query_error_message(error_message, error_code, namespace, query, class_name)
+ end
+
+ super(error_message)
+ end
+
+ private
+
+ def translate_error_code(error_message)
error_code = nil
# Parse the error to get the error status code
error_code_match = error_message.match(/[^\:]+\:\s*([0-9A-Fa-f]{1,8}).*/)
error_code = error_code_match.captures.first if error_code_match
- error_code = '' if error_code.nil?
+ error_code ? error_code : ''
+ end
+
+ def translate_wmi_connect_error_message(native_message, error_code, namespace)
+ error_message = "An error occurred connecting to the WMI service for namespace \'#{namespace}\'. The namespace may not be valid, access may not be allowed to the WMI service, or the WMI service may not be available.\n#{native_message}"
+
+ if error_code =~ /8004100E/i
+ error_message = "The specified namespace name \'#{namespace}\' is not a valid namespace name or does not exist.\n#{native_message}"
+ end
+
+ error_message
+ end
+
+ def translate_query_error_message(native_message, error_code, namespace, query, class_name)
+ error_message = "An error occurred when querying namespace \'#{namespace}\' with query \'#{query}\'.\n#{native_message}"
+
+ error_code = translate_error_code(error_message)
# Use the status code to generate a more friendly message
case error_code
when /80041010/i
if class_name
- error_message = "The specified class \'#{class_name}\' is not valid in the namespace \'#{namespace}\'.\n#{exception.message}."
+ error_message = "The specified class \'#{class_name}\' is not valid in the namespace \'#{namespace}\'.\n#{native_message}."
else
- error_message = "The specified query \'#{query}\' referenced a class that is not valid in the namespace \'#{namespace}\'\n#{exception.message}."
+ error_message = "The specified query \'#{query}\' referenced a class that is not valid in the namespace \'#{namespace}\'\n#{native_message}."
end
- when /8004100E/i
- error_message = "The specified namespace \'#{namespace}\' is not valid.\n#{exception.message}"
when /80041017/i
- error_message = "The specified query \'#{query}\' is not valid.\n#{exception.message}"
+ error_message = "The specified query \'#{query}\' in namespace \'#{namespace}\' is not a syntactically valid query.\n#{native_message}"
end
- super(error_message)
+ error_message
end
end
end