From cabc5171e3d65b9a9a9831a0a07588f20c90c3e7 Mon Sep 17 00:00:00 2001 From: Adam Edwards Date: Sat, 17 May 2014 06:48:53 -0700 Subject: More specific error handling --- lib/wmi-lite/wmi.rb | 42 ++++++++++++++++++++++--------------- lib/wmi-lite/wmi_exception.rb | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 16 deletions(-) create mode 100644 lib/wmi-lite/wmi_exception.rb (limited to 'lib') diff --git a/lib/wmi-lite/wmi.rb b/lib/wmi-lite/wmi.rb index cc6492d..b712ba9 100644 --- a/lib/wmi-lite/wmi.rb +++ b/lib/wmi-lite/wmi.rb @@ -18,32 +18,25 @@ require 'win32ole' if RUBY_PLATFORM =~ /mswin|mingw32|windows/ require 'wmi-lite/wmi_instance' +require 'wmi-lite/wmi_exception' module WmiLite class Wmi def initialize(namespace = nil) - @namespace = namespace + @namespace = namespace.nil? ? 'root/cimv2' : namespace @connection = nil end def query(wql_query) - results = start_query(wql_query) - - result_set = [] - - results.each do | result | - result_set.push(wmi_result_to_snapshot(result)) - end - - result_set + query_with_context(wql_query) end def instances_of(wmi_class) - query("select * from #{wmi_class}") + query_with_context("select * from #{wmi_class}", wmi_class) end def first_of(wmi_class) - query_result = start_query("select * from #{wmi_class}") + query_result = start_query("select * from #{wmi_class}", wmi_class) first_result = nil query_result.each do | record | first_result = record @@ -54,10 +47,27 @@ module WmiLite private - def start_query(wql_query) - connect_to_namespace - result = @connection.ExecQuery(wql_query) - raise_if_failed(result) + def query_with_context(wql_query, diagnostic_class_name = nil) + results = start_query(wql_query, diagnostic_class_name) + + result_set = [] + + results.each do | result | + result_set.push(wmi_result_to_snapshot(result)) + end + + result_set + end + + def start_query(wql_query, diagnostic_class_name = nil) + result = nil + 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) + end result end diff --git a/lib/wmi-lite/wmi_exception.rb b/lib/wmi-lite/wmi_exception.rb new file mode 100644 index 0000000..85c6387 --- /dev/null +++ b/lib/wmi-lite/wmi_exception.rb @@ -0,0 +1,48 @@ +# +# Author:: Adam Edwards () +# Copyright:: Copyright 2014 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +module WmiLite + class WmiException < Exception + def initialize(exception, namespace, query, class_name) + error_message = exception.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? + + # 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}." + else + error_message = "The specified query \'#{query}\' referenced a class that is not valid in the namespace \'#{namespace}\'\n#{exception.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}" + end + + super(error_message) + end + end +end + -- cgit v1.2.1