Skip to content

Commit

Permalink
(MODULES-6281) Return Errors from T-SQL
Browse files Browse the repository at this point in the history
The sqlserver_tsql resource does not handle errors returned from T-SQL
statements properly. The errors come back from the query but unless the
phrase 'SQL Server' was included in the error text, the error was not
then passed back to the user/logs. This change ensure that errors
returned are passed back properly by inspecting the Errors property of
the ADO object used to run the query. Note that this may not behave as
expected if the error that occurrs is of sufficently low severity that a
rowset, even if empty, is also returned. If a rowset is returned by the
ADO object, the object will not populate the Errors property, and there
is no way to tell a low severity error occurred. For instance 'Select
1/0' returns a low severity division by zero error, and an empty rowset.
Because the empty rowset exists, the error cannot be seen. It is up to
the user to ensure that the errors they want to catch are of sufficienty
severity that execution of the query is halted.
  • Loading branch information
RandomNoun7 committed Feb 23, 2018
1 parent 45d9721 commit 63abc02
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions lib/puppet_x/sqlserver/sql_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def open_and_run_command(query, config)
open(config)
execute(query)
rescue win32_exception => e
return ResultOutput.new(true, e.message)
return ResultOutput.new(true, @connection.Errors(0).Description)
ensure
close
end
Expand Down Expand Up @@ -95,20 +95,13 @@ class ResultOutput
def initialize(has_errors, error_message)
@exitstatus = has_errors ? 1 : 0
if error_message
@raw_error_message = error_message
@error_message = parse_for_error(error_message)
@error_message = error_message
end
end

def has_errors
@exitstatus != 0
end

private
def parse_for_error(result)
match = result.match(/SQL Server\n\s*(.*)/i)
match[1] unless match == nil
end
end
end
end

0 comments on commit 63abc02

Please sign in to comment.