-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(MODULES-6281) Return Errors from T-SQL #263
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -90,24 +90,25 @@ def win32_exception | |
end | ||
|
||
class ResultOutput | ||
attr_reader :exitstatus, :error_message, :raw_error_message | ||
|
||
def initialize(has_errors, error_message) | ||
attr_reader :exitstatus, :error_message | ||
|
||
def initialize(has_errors, error_message, connection) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Warning, this variable has the same name as a method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does ole_connection sound? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RandomNoun7 its the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh. I can change it now, or we can call it out of scope for the ticket. I'm fine either way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Derp.....I should've read more carefully. I was meaning the Need more ☕️ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can change the name of the method to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will rename or remove in a maint commit. |
||
@exitstatus = has_errors ? 1 : 0 | ||
if error_message | ||
@raw_error_message = error_message | ||
@error_message = parse_for_error(error_message) | ||
end | ||
|
||
@error_message = extract_messages(connection) || error_message | ||
end | ||
|
||
def has_errors | ||
@exitstatus != 0 | ||
def extract_messages(connection) | ||
return nil if connection.nil? || connection.Errors.count == 0 | ||
|
||
error_count = connection.Errors.count - 1 | ||
|
||
((0..error_count).map { |i| connection.Errors(i).Description.to_s}).join("\n") | ||
end | ||
|
||
private | ||
def parse_for_error(result) | ||
match = result.match(/SQL Server\n\s*(.*)/i) | ||
match[1] unless match == nil | ||
def has_errors | ||
@exitstatus != 0 | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,12 @@ | |
|
||
def stub_connection | ||
@connection = mock() | ||
error_mock = mock() | ||
error_mock.stubs(:count).returns(0) | ||
|
||
subject.stubs(:create_connection).returns(@connection) | ||
@connection.stubs(:State).returns(PuppetX::Sqlserver::CONNECTION_CLOSED) | ||
@connection.stubs(:Errors).returns(error_mock) | ||
subject.stubs(:win32_exception).returns(Exception) | ||
end | ||
|
||
|
@@ -20,11 +24,12 @@ 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")) | ||
@connection.Errors.stubs(:count).returns(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems an odd stub to add. Should instead it be stubbing a an entire Errors construct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have to have it there, or it never attempts to examine the @connection object for errors. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This stub is fine... he has to simulate a populated ADODB Errors connection object, as the existence of errors in this collection changes the behavior of the error reporting. It may be possible to create an instance of the real thing, but that would require Windows... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The need to create and test the real thing is why I modified the acceptance tests to ensure the proper object is examined and just the win32 exception message passed into the rescue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it "feels odd" to me that your stubbing the length of an array, but not setting the content of the Array too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it's a strange object. Calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @glennsarti yeah, higher up he sets There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The two test cases where this was used have been modified to set the non default |
||
@connection.Errors.stubs(:Description).returns("SQL Error in Connection") | ||
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 Error in Connection') | ||
}.to_not raise_error(Exception) | ||
|
||
end | ||
|
@@ -114,20 +119,22 @@ def stub_connection | |
end | ||
context 'return result with errors' do | ||
it { | ||
subject.stubs(:win32_exception).returns(Exception) | ||
stub_connection | ||
@connection.Errors.stubs(:count).returns(1) | ||
@connection.Errors.stubs(:Description).returns("SQL Error in Connection") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe mocking library makes this magically work... but I read this as supplying a value for: @connection.Errors.Description When I think we're interested in: @connection.Errors(0).Description |
||
@connection.stubs(:Execute).raises(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(:close).once | ||
result = | ||
subject.open_and_run_command('SELECT * FROM sys.databases', config) | ||
|
||
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('SQL Error in Connection') | ||
} | ||
end | ||
context 'open connection failure' do | ||
it { | ||
stub_connection | ||
err_message = "SQL Server\n ConnectionFailed" | ||
err_message = "ConnectionFailed" | ||
@connection.stubs(:Open).raises(Exception.new(err_message)) | ||
expect { | ||
result = subject.open_and_run_command('whacka whacka whacka', config) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is
=> e
still necessary if you're not going to do anything with the specific exception?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not. I'll get rid of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, turns out I do still need it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
General rubyism is to prefix with underscore for required, but unused variables
_e