Skip to content
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

wip Preserve errors so they will be logged to stderr with backtraces #342

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions splunklib/searchcommands/search_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ def _process_protocol_v1(self, argv, ifile, ofile):
except:
self._report_unexpected_error()
self.flush()
exit(1)
raise

debug('%s.process finished under protocol_version=1', class_name)

Expand Down Expand Up @@ -688,7 +688,7 @@ def _process_protocol_v2(self, argv, ifile, ofile):
self._record_writer = RecordWriterV2(ofile)
self._report_unexpected_error()
self.finish()
exit(1)
raise

# Write search command configuration for consumption by splunkd
# noinspection PyBroadException
Expand Down Expand Up @@ -768,7 +768,7 @@ def _process_protocol_v2(self, argv, ifile, ofile):
self._record_writer.write_metadata(self._configuration)
self._report_unexpected_error()
self.finish()
exit(1)
raise

self._record_writer.write_metadata(self._configuration)

Expand All @@ -784,7 +784,7 @@ def _process_protocol_v2(self, argv, ifile, ofile):
except:
self._report_unexpected_error()
self.finish()
exit(1)
raise

debug('%s.process completed', class_name)

Expand Down
43 changes: 20 additions & 23 deletions tests/searchcommands/test_search_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@

import pytest

class CustomTestCommandException(Exception):
pass

@Configuration()
class TestCommand(SearchCommand):

Expand All @@ -44,10 +47,7 @@ class TestCommand(SearchCommand):
def echo(self, records):
for record in records:
if record.get('action') == 'raise_exception':
if six.PY2:
raise StandardError(self)
else:
raise Exception(self)
raise CustomTestCommandException()
yield record

def _execute(self, ifile, process):
Expand Down Expand Up @@ -83,6 +83,8 @@ class ConfigurationSettings(SearchCommand.ConfigurationSettings):

# endregion

class CustomRuntimeError(RuntimeError):
pass

@Configuration()
class TestStreamingCommand(StreamingCommand):
Expand All @@ -91,7 +93,7 @@ def stream(self, records):
for record in records:
action = record['action']
if action == 'raise_error':
raise RuntimeError('Testing')
raise CustomRuntimeError('Testing')
value = self.search_results_info if action == 'get_search_results_info' else None
yield {'_serial': serial_number, 'data': value}
serial_number += 1
Expand Down Expand Up @@ -126,7 +128,7 @@ def test_process_scpv1(self):
command = TestCommand()
result = BytesIO()

self.assertRaises(SystemExit, command.process, argv, ofile=result)
self.assertRaises(RuntimeError, command.process, argv, ofile=result)
self.assertRegexpMatches(result.getvalue().decode('UTF-8'), expected)

# TestCommand.process should return configuration settings on Getinfo probe
Expand Down Expand Up @@ -292,15 +294,14 @@ def test_process_scpv1(self):
try:
# noinspection PyTypeChecker
command.process(argv, ifile, ofile=result)
except SystemExit as error:
self.assertNotEqual(error.code, 0)
except CustomRuntimeError:
self.assertRegexpMatches(
result.getvalue().decode('UTF-8'),
r'^error_message=RuntimeError at ".+", line \d+ : Testing\r\n\r\n$')
except BaseException as error:
self.fail('Expected SystemExit, but caught {}: {}'.format(type(error).__name__, error))
self.fail('Expected CustomRuntimeError, but caught {}: {}'.format(type(error).__name__, error))
else:
self.fail('Expected SystemExit, but no exception was raised')
self.fail('Expected CustomRuntimeError, but no exception was raised')

# Command.process should provide access to search results info
info_path = os.path.join(
Expand Down Expand Up @@ -676,12 +677,12 @@ def test_process_scpv2(self):

try:
command.process(argv, ifile, ofile=result)
except SystemExit as error:
self.assertNotEqual(0, error.code)
except CustomTestCommandException:
pass # Expected exception was preserved.
except BaseException as error:
self.fail('{0}: {1}: {2}\n'.format(type(error).__name__, error, result.getvalue().decode('utf-8')))
else:
self.fail('Expected SystemExit, not a return from TestCommand.process: {}\n'.format(result.getvalue().decode('utf-8')))
self.fail('Expected CustomTestCommandException, not a return from TestCommand.process: {}\n'.format(result.getvalue().decode('utf-8')))

self.assertEqual(command.logging_configuration, logging_configuration)
self.assertEqual(command.logging_level, logging_level)
Expand All @@ -692,17 +693,13 @@ def test_process_scpv2(self):

finished = r'\"finished\":true'

if six.PY2:
inspector = \
r'\"inspector\":\{\"messages\":\[\[\"ERROR\",\"StandardError at \\\".+\\\", line \d+ : test ' \
r'logging_configuration=\\\".+\\\" logging_level=\\\"WARNING\\\" record=\\\"f\\\" ' \
r'required_option_1=\\\"value_1\\\" required_option_2=\\\"value_2\\\" show_configuration=\\\"f\\\"\"\]\]\}'
else:
inspector = \
r'\"inspector\":\{\"messages\":\[\[\"ERROR\",\"Exception at \\\".+\\\", line \d+ : test ' \
r'logging_configuration=\\\".+\\\" logging_level=\\\"WARNING\\\" record=\\\"f\\\" ' \
r'required_option_1=\\\"value_1\\\" required_option_2=\\\"value_2\\\" show_configuration=\\\"f\\\"\"\]\]\}'
inspector = \
r'\"inspector\":\{\"messages\":\[\[\"ERROR\",\"CustomTestCommandException at \\\".+\\\", line \d+ : test ' \
r'logging_configuration=\\\".+\\\" logging_level=\\\"WARNING\\\" record=\\\"f\\\" ' \
r'required_option_1=\\\"value_1\\\" required_option_2=\\\"value_2\\\" show_configuration=\\\"f\\\"\"\]\]\}'

res = result.getvalue().decode('utf-8')
print('result = '+res)
self.assertRegexpMatches(
result.getvalue().decode('utf-8'),
r'^chunked 1.0,2,0\n'
Expand Down