Skip to content

Commit 63abc02

Browse files
committed
(MODULES-6281) Return Errors from T-SQL
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.
1 parent 45d9721 commit 63abc02

1 file changed

Lines changed: 2 additions & 9 deletions

File tree

lib/puppet_x/sqlserver/sql_connection.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def open_and_run_command(query, config)
99
open(config)
1010
execute(query)
1111
rescue win32_exception => e
12-
return ResultOutput.new(true, e.message)
12+
return ResultOutput.new(true, @connection.Errors(0).Description)
1313
ensure
1414
close
1515
end
@@ -95,20 +95,13 @@ class ResultOutput
9595
def initialize(has_errors, error_message)
9696
@exitstatus = has_errors ? 1 : 0
9797
if error_message
98-
@raw_error_message = error_message
99-
@error_message = parse_for_error(error_message)
98+
@error_message = error_message
10099
end
101100
end
102101

103102
def has_errors
104103
@exitstatus != 0
105104
end
106-
107-
private
108-
def parse_for_error(result)
109-
match = result.match(/SQL Server\n\s*(.*)/i)
110-
match[1] unless match == nil
111-
end
112105
end
113106
end
114107
end

0 commit comments

Comments
 (0)