git-p4: raise exceptions from p4CmdList based on error from p4 server

This change lays some groundwork for better handling of rowcount errors from the server, where it fails to send us results because we requested too many. It adds an option to p4CmdList() to return errors as a Python exception. The exceptions are derived from P4Exception (something went wrong), P4ServerException (the server sent us an error code) and P4RequestSizeException (we requested too many rows/results from the server database). This makes the code that handles the errors a bit easier. The default behavior is unchanged; the new code is enabled with a flag. Signed-off-by: Luke Diamand <luke@diamand.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Luke Diamand committed Jun 8, 2018 at 21:32 UTC 55bb3e3611c7b3f7bbbaf8d9ce98a4ed4196c3eb
1 file changed +40 -4
git-p4.py
+40 -4
@@ -561,10 +561,30 @@ def isModeExec(mode):
561 # otherwise False.
562 return mode[-3:] == "755"
563
564 +class P4Exception(Exception):
565 + """ Base class for exceptions from the p4 client """
566 + def __init__(self, exit_code):
567 + self.p4ExitCode = exit_code
568 +
569 +class P4ServerException(P4Exception):
570 + """ Base class for exceptions where we get some kind of marshalled up result from the server """
571 + def __init__(self, exit_code, p4_result):
572 + super(P4ServerException, self).__init__(exit_code)
573 + self.p4_result = p4_result
574 + self.code = p4_result[0]['code']
575 + self.data = p4_result[0]['data']
576 +
577 +class P4RequestSizeException(P4ServerException):
578 + """ One of the maxresults or maxscanrows errors """
579 + def __init__(self, exit_code, p4_result, limit):
580 + super(P4RequestSizeException, self).__init__(exit_code, p4_result)
581 + self.limit = limit
582 +
583 def isModeExecChanged(src_mode, dst_mode):
584 return isModeExec(src_mode) != isModeExec(dst_mode)
585
567 -def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False):
586 +def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
587 + errors_as_exceptions=False):
588
589 if isinstance(cmd,basestring):
590 cmd = "-G " + cmd
@@ -611,9 +631,25 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False):
631 pass
632 exitCode = p4.wait()
633 if exitCode != 0:
614 - entry = {}
615 - entry["p4ExitCode"] = exitCode
616 - result.append(entry)
634 + if errors_as_exceptions:
635 + if len(result) > 0:
636 + data = result[0].get('data')
637 + if data:
638 + m = re.search('Too many rows scanned \(over (\d+)\)', data)
639 + if not m:
640 + m = re.search('Request too large \(over (\d+)\)', data)
641 +
642 + if m:
643 + limit = int(m.group(1))
644 + raise P4RequestSizeException(exitCode, result, limit)
645 +
646 + raise P4ServerException(exitCode, result)
647 + else:
648 + raise P4Exception(exitCode)
649 + else:
650 + entry = {}
651 + entry["p4ExitCode"] = exitCode
652 + result.append(entry)
653
654 return result
655