Skip to content

Commit 4348a99

Browse files
author
David Noble
committed
Adds error reporting to splunk ui as well as the log file
Signed-off-by: David Noble <[email protected]>
1 parent b176840 commit 4348a99

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

splunklib/results.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,18 @@ def _parse_results(self, stream):
246246
elem.clear()
247247

248248
elif elem.tag in ('text', 'v') and event == 'end':
249-
values.append(elem.text.encode('utf8'))
249+
text = elem.text if elem.text is not None else ""
250+
values.append(text.encode('utf8'))
250251
elem.clear()
251252

252253
elif elem.tag == 'msg':
253254
if event == 'start':
254255
msg_type = elem.attrib['type']
255256
elif event == 'end':
256-
yield Message(msg_type, elem.text.encode('utf8'))
257+
text = elem.text if elem.text is not None else ""
258+
yield Message(msg_type, text.encode('utf8'))
257259
elem.clear()
258-
except et.ParseError as pe:
260+
except SyntaxError as pe:
259261
# This is here to handle the same incorrect return from
260262
# splunk that is described in __init__.
261263
if 'no element found' in pe.msg:

splunklib/searchcommands/generating_command.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15+
from __future__ import absolute_import
16+
1517
from . search_command import SearchCommand
1618

1719

@@ -79,9 +81,7 @@ def _execute(self, operation, reader, writer):
7981
writer.writerow(record)
8082
except Exception as e:
8183
from traceback import format_exc
82-
from sys import exit
83-
self.logger.error(format_exc())
84-
exit(1)
84+
self._exit(format_exc(), e, 1)
8585

8686
def _prepare(self, argv, input_file):
8787
ConfigurationSettings = type(self).ConfigurationSettings

splunklib/searchcommands/reporting_command.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ def _execute(self, operation, reader, writer):
7777
writer.writerow(record)
7878
except Exception as e:
7979
from traceback import format_exc
80-
from sys import exit
81-
self.logger.error(format_exc())
82-
exit(1)
80+
self._exit(format_exc(), e, 1)
8381

8482
def _prepare(self, argv, input_file):
8583
if len(argv) >= 3 and argv[2] == '__map__':

splunklib/searchcommands/search_command.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from inspect import getmembers
2727
from logging import _levelNames, getLevelName
2828
from os import path
29-
from sys import argv, stdin, stdout
29+
from sys import argv, exit, stdin, stdout
3030
from urlparse import urlsplit
3131
from xml.etree import ElementTree
3232

@@ -59,6 +59,7 @@ def __init__(self):
5959
self._configuration = None
6060
self._fieldnames = None
6161
self._option_view = None
62+
self._output_file = None
6263
self._search_results_info = None
6364
self._service = None
6465

@@ -281,6 +282,7 @@ def process(self, args=argv, input_file=stdin, output_file=stdout):
281282
282283
"""
283284
self.logger.debug('%s arguments: %s' % (type(self).__name__, args))
285+
self._outputfile = output_file
284286
self._configuration = None
285287

286288
if len(args) >= 2 and args[1] == '__GETINFO__':
@@ -290,10 +292,8 @@ def process(self, args=argv, input_file=stdin, output_file=stdout):
290292
try:
291293
self.parser.parse(args, self)
292294
except (SyntaxError, ValueError) as e:
293-
writer = csv.DictWriter(output_file, self, fieldnames=['ERROR'])
294-
writer.writerow({'ERROR': e})
295-
self.logger.error(e)
296-
return
295+
from traceback import format_exc
296+
self._exit(format_exc(), e, 1)
297297

298298
self._configuration = ConfigurationSettings(self)
299299

@@ -310,11 +310,8 @@ def process(self, args=argv, input_file=stdin, output_file=stdout):
310310
try:
311311
self.parser.parse(args, self)
312312
except (SyntaxError, ValueError) as e:
313-
from sys import exit
314-
self.messages.append("error_message", e)
315-
self.messages.write(output_file)
316-
self.logger.error(e)
317-
exit(1)
313+
from traceback import format_exc
314+
self._exit(format_exc(), e, 1)
318315

319316
self._configuration = ConfigurationSettings(self)
320317

@@ -339,9 +336,7 @@ def process(self, args=argv, input_file=stdin, output_file=stdout):
339336
'supports_getinfo = true | '
340337
'supports_rawargs = true | '
341338
'outputheader = true'.format(type(self).name, file_name))
342-
self.messages.append('error_message', message)
343-
self.messages.write(output_file)
344-
self.logger.error(message)
339+
self._exit(message, message, 1)
345340

346341
@staticmethod
347342
def records(reader):
@@ -355,6 +350,12 @@ def _prepare(self, argv, input_file):
355350
def _execute(self, operation, reader, writer):
356351
raise NotImplementedError('SearchCommand._configure(self, argv)')
357352

353+
def _exit(self, log, error, status_code):
354+
writer = csv.DictWriter(self.output_file, self, fieldnames=['ERROR'])
355+
writer.writerow({'ERROR': error})
356+
self.logger.error(log)
357+
exit(status_code)
358+
358359
#endregion
359360

360361
#region Types

splunklib/searchcommands/streaming_command.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15+
from __future__ import absolute_import
16+
1517
from . search_command import SearchCommand
1618
from . import csv
1719

@@ -74,9 +76,7 @@ def _execute(self, operation, reader, writer):
7476
writer.writerow(record)
7577
except Exception as e:
7678
from traceback import format_exc
77-
from sys import exit
78-
self.logger.error(format_exc())
79-
exit(1)
79+
self._exit(format_exc(), e, 1)
8080

8181
def _prepare(self, argv, input_file):
8282
ConfigurationSettings = type(self).ConfigurationSettings

0 commit comments

Comments
 (0)