Skip to content

Commit bbc77d7

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 bbc77d7

2 files changed

Lines changed: 18 additions & 19 deletions

File tree

lib/puppet_x/sqlserver/sql_connection.rb

Lines changed: 9 additions & 14 deletions
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,25 +90,20 @@ 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 = connection.Errors(0).Description if connection && !connection.Errors.count == 0
100+
101+
@error_message ||= error_message
101102
end
102103

103104
def has_errors
104105
@exitstatus != 0
105106
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
112107
end
113108
end
114109
end

spec/unit/puppet_x/sql_connection_spec.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88

99
def stub_connection
1010
@connection = mock()
11+
error = mock()
12+
error.stubs(:Description).returns("SQL Connection error has happened")
13+
error.stubs(:count).returns(1)
1114
subject.stubs(:create_connection).returns(@connection)
1215
@connection.stubs(:State).returns(PuppetX::Sqlserver::CONNECTION_CLOSED)
16+
@connection.stubs(:Errors).returns(error)
1317
subject.stubs(:win32_exception).returns(Exception)
1418
end
1519

@@ -20,11 +24,11 @@ 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+
subject.stubs(:execute).raises(Exception.new("An error has happened"))
2428
expect {
2529
result = subject.open_and_run_command('whacka whacka whacka', config)
2630
expect(result.exitstatus).to eq(1)
27-
expect(result.error_message).to eq('error has happened')
31+
expect(result.error_message).to eq('An error has happened')
2832
}.to_not raise_error(Exception)
2933

3034
end
@@ -116,12 +120,12 @@ def stub_connection
116120
it {
117121
subject.stubs(:win32_exception).returns(Exception)
118122
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"))
123+
subject.expects(:execute).with('SELECT * FROM sys.databases').raises(Exception.new("Invalid syntax provider"))
120124
subject.expects(:close).once
121125
result =
122126
subject.open_and_run_command('SELECT * FROM sys.databases', config)
123127
expect(result.exitstatus).to eq(1)
124-
expect(result.error_message).to eq('invalid syntax provider')
128+
expect(result.error_message).to eq('Invalid syntax provider')
125129
}
126130
end
127131
context 'open connection failure' do
@@ -132,7 +136,7 @@ def stub_connection
132136
expect {
133137
result = subject.open_and_run_command('whacka whacka whacka', config)
134138
expect(result.exitstatus).to eq(1)
135-
expect(result.error_message).to eq 'ConnectionFailed'
139+
expect(result.error_message).to eq "SQL Server\n ConnectionFailed"
136140
}.to_not raise_error(Exception)
137141
}
138142
end

0 commit comments

Comments
 (0)