[PATCH 3 of 3] hgweb: send proper error messages to the client

Sune Foldager cryo at cyanite.org
Fri Oct 30 16:42:44 UTC 2009


# HG changeset patch
# User Sune Foldager <cryo at cyanite.org>
# Date 1256920872 -3600
# Node ID 24e4b80180ffee01ad3896f4b5257b87170518cb
# Parent  60aedecda1538d2ce1932f466b8f1925ae3c91a8
hgweb: send proper error messages to the client

Fixes a bug in protocol which caused an exception during exception handling in
some cases on Windows. Also makes sure the server error message is correctly
propagated to the client, instead of being thrown away.

diff --git a/mercurial/hgweb/common.py b/mercurial/hgweb/common.py
--- a/mercurial/hgweb/common.py
+++ b/mercurial/hgweb/common.py
@@ -31,8 +31,8 @@
     responses = BaseHTTPRequestHandler.responses
     return responses.get(code, ('Error', 'Unknown error'))[0]
 
-def statusmessage(code):
-    return '%d %s' % (code, _statusmessage(code))
+def statusmessage(code, message=None):
+    return '%d %s' % (code, message or _statusmessage(code))
 
 def get_mtime(repo_path):
     store_path = os.path.join(repo_path, ".hg")
diff --git a/mercurial/hgweb/protocol.py b/mercurial/hgweb/protocol.py
--- a/mercurial/hgweb/protocol.py
+++ b/mercurial/hgweb/protocol.py
@@ -181,18 +181,19 @@
         except ValueError, inst:
             raise ErrorResponse(HTTP_OK, inst)
         except (OSError, IOError), inst:
-            filename = getattr(inst, 'filename', '')
-            # Don't send our filesystem layout to the client
-            if filename.startswith(repo.root):
-                filename = filename[len(repo.root)+1:]
-            else:
-                filename = ''
             error = getattr(inst, 'strerror', 'Unknown error')
             if inst.errno == errno.ENOENT:
                 code = HTTP_NOT_FOUND
             else:
                 code = HTTP_SERVER_ERROR
-            raise ErrorResponse(code, '%s: %s' % (error, filename))
+            filename = getattr(inst, 'filename', '')
+            # Don't send our filesystem layout to the client
+            if filename and filename.startswith(repo.root):
+                filename = filename[len(repo.root)+1:]
+                text = '%s: %s' % (error, filename)
+            else:
+                text = error.replace(repo.root + os.path.sep, '')
+            raise ErrorResponse(code, text)
     finally:
         fp.close()
         os.unlink(tempname)
diff --git a/mercurial/hgweb/request.py b/mercurial/hgweb/request.py
--- a/mercurial/hgweb/request.py
+++ b/mercurial/hgweb/request.py
@@ -77,7 +77,7 @@
 
             if isinstance(status, ErrorResponse):
                 self.header(status.headers)
-                status = statusmessage(status.code)
+                status = statusmessage(status.code, status.message)
             elif status == 200:
                 status = '200 Script output follows'
             elif isinstance(status, int):



More information about the Mercurial-devel mailing list