Skip to content

Commit 288a437

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 288a437

3 files changed

Lines changed: 10 additions & 10 deletions

File tree

lib/puppet_x/sqlserver/sql_connection.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def open_and_run_command(query, config)
88
begin
99
open(config)
1010
execute(query)
11-
rescue win32_exception => e
12-
return ResultOutput.new(true, e.message)
11+
rescue win32_exception
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

spec/spec_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@
1111
end
1212
end
1313

14+
Class Error_mock
15+
def Description
16+
"SQL Server\n error has happened"
17+
end
18+
end

spec/unit/puppet_x/sql_connection_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
def stub_connection
1010
@connection = mock()
11+
error_mock = Error_mock.new
1112
subject.stubs(:create_connection).returns(@connection)
1213
@connection.stubs(:State).returns(PuppetX::Sqlserver::CONNECTION_CLOSED)
14+
@connection.stubs(:Errors).returns(error_mock)
1315
subject.stubs(:win32_exception).returns(Exception)
1416
end
1517

0 commit comments

Comments
 (0)