Skip to content

Commit 55bb3e3

Browse files
luked99gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0ef67ac commit 55bb3e3

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

git-p4.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -561,10 +561,30 @@ def isModeExec(mode):
561561
# otherwise False.
562562
return mode[-3:] == "755"
563563

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+
564583
def isModeExecChanged(src_mode, dst_mode):
565584
return isModeExec(src_mode) != isModeExec(dst_mode)
566585

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):
568588

569589
if isinstance(cmd,basestring):
570590
cmd = "-G " + cmd
@@ -611,9 +631,25 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False):
611631
pass
612632
exitCode = p4.wait()
613633
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)
617653

618654
return result
619655

0 commit comments

Comments
 (0)