git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCHv2 4/6] git-p4: raise exceptions from p4CmdList based on error from p4 server

From
Luke Diamand <luke@diamand.org>
Date
Jun 8, 2018, 20:32 UTC
Message-ID
<20180608203248.16311-5-luke@diamand.org>
In-Reply-To
<20180608203248.16311-4-luke@diamand.org>

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>
---
 git-p4.py | 44 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 40 insertions(+), 4 deletions(-)
diff --git a/git-p4.py b/git-p4.py
index 3de12a4a0a..f4b5deeb83 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -566,10 +566,30 @@ def isModeExec(mode):
     # otherwise False.
     return mode[-3:] == "755"
 
+class P4Exception(Exception):
+    """ Base class for exceptions from the p4 client """
+    def __init__(self, exit_code):
+        self.p4ExitCode = exit_code
+
+class P4ServerException(P4Exception):
+    """ Base class for exceptions where we get some kind of marshalled up result from the server """
+    def __init__(self, exit_code, p4_result):
+        super(P4ServerException, self).__init__(exit_code)
+        self.p4_result = p4_result
+        self.code = p4_result[0]['code']
+        self.data = p4_result[0]['data']
+
+class P4RequestSizeException(P4ServerException):
+    """ One of the maxresults or maxscanrows errors """
+    def __init__(self, exit_code, p4_result, limit):
+        super(P4RequestSizeException, self).__init__(exit_code, p4_result)
+        self.limit = limit
+
 def isModeExecChanged(src_mode, dst_mode):
     return isModeExec(src_mode) != isModeExec(dst_mode)
 
-def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False):
+def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
+        errors_as_exceptions=False):
 
     if isinstance(cmd,basestring):
         cmd = "-G " + cmd
@@ -616,9 +636,25 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False):
         pass
     exitCode = p4.wait()
     if exitCode != 0:
-        entry = {}
-        entry["p4ExitCode"] = exitCode
-        result.append(entry)
+        if errors_as_exceptions:
+            if len(result) > 0:
+                data = result[0].get('data')
+                if data:
+                    m = re.search('Too many rows scanned \(over (\d+)\)', data)
+                    if not m:
+                        m = re.search('Request too large \(over (\d+)\)', data)
+
+                    if m:
+                        limit = int(m.group(1))
+                        raise P4RequestSizeException(exitCode, result, limit)
+
+                raise P4ServerException(exitCode, result)
+            else:
+                raise P4Exception(exitCode)
+        else:
+            entry = {}
+            entry["p4ExitCode"] = exitCode
+            result.append(entry)
 
     return result
 
-- 
2.17.0.392.gdeb1a6e9b7
Previous: Luke DiamandNext: Luke Diamand
Message 5 of 13 in “[PATCHv2 0/6] git-p4: some small fixes updated”
  1. Luke DiamandJun 8, 2018
  2. 1/6 git-p4: disable-rebase: allow setting this via configurationLuke Diamand, Jun 8, 2018
  3. 2/6 git-p4: add option to disable syncing of p4/master with p4Luke Diamand, Jun 8, 2018
  4. 3/6 git-p4: better error reporting when p4 failsLuke Diamand, Jun 8, 2018
  5. 4/6 git-p4: raise exceptions from p4CmdList based on error from p4 serverLuke Diamand, Jun 8, 2018
  6. 5/6 git-p4: narrow the scope of exceptions caught when parsing an intLuke Diamand, Jun 8, 2018
  7. 6/6 git-p4: auto-size the blockLuke Diamand, Jun 8, 2018
  8. Junio C HamanoJun 12, 2018
  9. Luke DiamandJun 12, 2018
  10. Junio C HamanoJun 12, 2018
  11. Luke DiamandJun 12, 2018
  12. Eric SunshineJun 12, 2018
  13. Luke DiamandJun 12, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.