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 26, 2018
1 parent 45d9721 commit 4ebc3e2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
24 changes: 12 additions & 12 deletions lib/puppet_x/sqlserver/sql_connection.rb
Expand Up @@ -9,12 +9,12 @@ 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, e.message, @connection)
ensure
close
end

ResultOutput.new(false, nil)
ResultOutput.new(false, nil, @connection)
end

private
Expand Down Expand Up @@ -92,23 +92,23 @@ def win32_exception
class ResultOutput
attr_reader :exitstatus, :error_message, :raw_error_message

def initialize(has_errors, error_message)
def initialize(has_errors, error_message, connection)
@exitstatus = has_errors ? 1 : 0
if error_message
@raw_error_message = error_message
@error_message = parse_for_error(error_message)

if !connection.nil?
if !connection.Errors(0).nil?
@error_message = connection.Errors(0).Description
end
end

if (@error_message.nil?) && (!error_message.nil?)
@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
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -10,4 +10,3 @@
e.syntax = [:should, :expect]
end
end

13 changes: 8 additions & 5 deletions spec/unit/puppet_x/sql_connection_spec.rb
Expand Up @@ -8,8 +8,11 @@

def stub_connection
@connection = mock()
error = mock()
error.stubs(:Description).returns("SQL Connection error has happened")
subject.stubs(:create_connection).returns(@connection)
@connection.stubs(:State).returns(PuppetX::Sqlserver::CONNECTION_CLOSED)
@connection.stubs(:Errors).returns(error)
subject.stubs(:win32_exception).returns(Exception)
end

Expand All @@ -20,11 +23,11 @@ def stub_connection
@connection.stubs(:Open).with('Provider=SQLNCLI11;Initial Catalog=master;Application Name=Puppet;Data Source=.;DataTypeComptibility=80;User ID=sa;Password=Pupp3t1@')
end
it 'should not raise an error but populate has_errors with message' do
subject.stubs(:execute).raises(Exception.new("SQL Server\n error has happened"))
subject.stubs(:execute).raises(Exception.new("An error has happened"))
expect {
result = subject.open_and_run_command('whacka whacka whacka', config)
expect(result.exitstatus).to eq(1)
expect(result.error_message).to eq('error has happened')
expect(result.error_message).to eq('SQL Connection error has happened')
}.to_not raise_error(Exception)

end
Expand Down Expand Up @@ -116,12 +119,12 @@ def stub_connection
it {
subject.stubs(:win32_exception).returns(Exception)
subject.expects(:open).with({:admin_user => 'sa', :admin_pass => 'Pupp3t1@', :instance_name => 'MSSQLSERVER'})
subject.expects(:execute).with('SELECT * FROM sys.databases').raises(Exception.new("SQL Server\ninvalid syntax provider"))
subject.expects(:execute).with('SELECT * FROM sys.databases').raises(Exception.new("Invalid syntax provider"))
subject.expects(:close).once
result =
subject.open_and_run_command('SELECT * FROM sys.databases', config)
expect(result.exitstatus).to eq(1)
expect(result.error_message).to eq('invalid syntax provider')
expect(result.error_message).to eq('Invalid syntax provider')
}
end
context 'open connection failure' do
Expand All @@ -132,7 +135,7 @@ def stub_connection
expect {
result = subject.open_and_run_command('whacka whacka whacka', config)
expect(result.exitstatus).to eq(1)
expect(result.error_message).to eq 'ConnectionFailed'
expect(result.error_message).to eq 'SQL Connection error has happened'
}.to_not raise_error(Exception)
}
end
Expand Down

0 comments on commit 4ebc3e2

Please sign in to comment.