Skip to content

Commit 9b8a0fe

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 dc2512a commit 9b8a0fe

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
1616
- During acceptance testing, only execute master provisioning steps if there is
1717
a master in the hosts array.
1818
- Stop running ```gem update bundler``` during Travis runs. ([MODULES-6339](https://tickets.puppetlabs.com/browse/MODULES6339))
19+
- The `sqlserver_tsql` resource now returns errors from sql queries properly. ([MODULES-6281](https://tickets.puppetlabs.com/browse/MODULES-6281))
1920

2021
## [2.1.0] - 2017-12-8
2122

lib/puppet_x/sqlserver/sql_connection.rb

+15-14
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ 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, e.message, @connection)
1313
ensure
1414
close
1515
end
1616

17-
ResultOutput.new(false, nil)
17+
ResultOutput.new(false, nil, @connection)
1818
end
1919

2020
private
@@ -90,24 +90,25 @@ def win32_exception
9090
end
9191

9292
class ResultOutput
93-
attr_reader :exitstatus, :error_message, :raw_error_message
9493

95-
def initialize(has_errors, error_message)
94+
attr_reader :exitstatus, :error_message
95+
96+
def initialize(has_errors, error_message, connection)
9697
@exitstatus = has_errors ? 1 : 0
97-
if error_message
98-
@raw_error_message = error_message
99-
@error_message = parse_for_error(error_message)
100-
end
98+
99+
@error_message = extract_messages(connection) || error_message
101100
end
102101

103-
def has_errors
104-
@exitstatus != 0
102+
def extract_messages(connection)
103+
return nil if connection.nil? || connection.Errors.count == 0
104+
105+
error_count = connection.Errors.count - 1
106+
107+
((0..error_count).map { |i| connection.Errors(i).Description.to_s}).join("\n")
105108
end
106109

107-
private
108-
def parse_for_error(result)
109-
match = result.match(/SQL Server\n\s*(.*)/i)
110-
match[1] unless match == nil
110+
def has_errors
111+
@exitstatus != 0
111112
end
112113
end
113114
end

spec/acceptance/sqlserver_tsql_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def ensure_sqlserver_database(db_name, ensure_val = 'present')
191191
}
192192
MANIFEST
193193
execute_manifest(pp, {:acceptable_exit_codes => [0,1]}) do |r|
194-
expect(r.stderr).to match(/Error/i)
194+
expect(r.stderr).to match(/Incorrect syntax/i)
195195
end
196196
end
197197

@@ -210,7 +210,7 @@ def ensure_sqlserver_database(db_name, ensure_val = 'present')
210210
}
211211
MANIFEST
212212
execute_manifest(pp, {:acceptable_exit_codes => [0,1]}) do |r|
213-
expect(r.stderr).to match(/Error/i)
213+
expect(r.stderr).to match(/Non-Existing-Database/i)
214214
end
215215
end
216216
end

spec/unit/puppet_x/sql_connection_spec.rb

+15-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88

99
def stub_connection
1010
@connection = mock()
11+
error_mock = mock()
12+
error_mock.stubs(:count).returns(0)
13+
1114
subject.stubs(:create_connection).returns(@connection)
1215
@connection.stubs(:State).returns(PuppetX::Sqlserver::CONNECTION_CLOSED)
16+
@connection.stubs(:Errors).returns(error_mock)
1317
subject.stubs(:win32_exception).returns(Exception)
1418
end
1519

@@ -20,11 +24,12 @@ def stub_connection
2024
@connection.stubs(:Open).with('Provider=SQLNCLI11;Initial Catalog=master;Application Name=Puppet;Data Source=.;DataTypeComptibility=80;User ID=sa;Password=Pupp3t1@')
2125
end
2226
it 'should not raise an error but populate has_errors with message' do
23-
subject.stubs(:execute).raises(Exception.new("SQL Server\n error has happened"))
27+
@connection.Errors.stubs(:count).returns(1)
28+
@connection.Errors.stubs(:Description).returns("SQL Error in Connection")
2429
expect {
2530
result = subject.open_and_run_command('whacka whacka whacka', config)
2631
expect(result.exitstatus).to eq(1)
27-
expect(result.error_message).to eq('error has happened')
32+
expect(result.error_message).to eq('SQL Error in Connection')
2833
}.to_not raise_error(Exception)
2934

3035
end
@@ -114,20 +119,22 @@ def stub_connection
114119
end
115120
context 'return result with errors' do
116121
it {
117-
subject.stubs(:win32_exception).returns(Exception)
122+
stub_connection
123+
@connection.Errors.stubs(:count).returns(1)
124+
@connection.Errors.stubs(:Description).returns("SQL Error in Connection")
125+
@connection.stubs(:Execute).raises(Exception)
118126
subject.expects(:open).with({:admin_user => 'sa', :admin_pass => 'Pupp3t1@', :instance_name => 'MSSQLSERVER'})
119-
subject.expects(:execute).with('SELECT * FROM sys.databases').raises(Exception.new("SQL Server\ninvalid syntax provider"))
120127
subject.expects(:close).once
121-
result =
122-
subject.open_and_run_command('SELECT * FROM sys.databases', config)
128+
129+
result = subject.open_and_run_command('SELECT * FROM sys.databases', config)
123130
expect(result.exitstatus).to eq(1)
124-
expect(result.error_message).to eq('invalid syntax provider')
131+
expect(result.error_message).to eq('SQL Error in Connection')
125132
}
126133
end
127134
context 'open connection failure' do
128135
it {
129136
stub_connection
130-
err_message = "SQL Server\n ConnectionFailed"
137+
err_message = "ConnectionFailed"
131138
@connection.stubs(:Open).raises(Exception.new(err_message))
132139
expect {
133140
result = subject.open_and_run_command('whacka whacka whacka', config)

0 commit comments

Comments
 (0)