Rev 5927: (jr) New hook server_exception in bzrlib.smart.server to catch any exception in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Fri May 27 13:54:43 UTC 2011


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 5927 [merge]
revision-id: pqm at pqm.ubuntu.com-20110527135441-mc4grvluic3smy04
parent: pqm at pqm.ubuntu.com-20110527124234-ckug8d7n60j1kip1
parent: jriddell at canonical.com-20110527131223-0nql2wzt1nnvdm8q
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Fri 2011-05-27 13:54:41 +0000
message:
  (jr) New hook server_exception in bzrlib.smart.server to catch any exception
   caused while running bzr serve. (Jonathan Riddell,
   #274578) (Jonathan Riddell)
modified:
  bzrlib/smart/server.py         server.py-20061110062051-chzu10y32vx8gvur-1
  bzrlib/tests/blackbox/test_serve.py test_serve.py-20060913064329-8t2pvmsikl4s3xhl-1
  doc/en/release-notes/bzr-2.4.txt bzr2.4.txt-20110114053217-k7ym9jfz243fddjm-1
=== modified file 'bzrlib/smart/server.py'
--- a/bzrlib/smart/server.py	2011-03-30 11:45:54 +0000
+++ b/bzrlib/smart/server.py	2011-05-27 13:12:09 +0000
@@ -1,4 +1,4 @@
-# Copyright (C) 2006-2010 Canonical Ltd
+# Copyright (C) 2006-2011 Canonical Ltd
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -254,6 +254,11 @@
             "Called by the bzr server when it stops serving a directory. "
             "server_stopped is called with the same parameters as the "
             "server_started hook: (backing_urls, public_url).", (0, 16))
+        self.add_hook('server_exception',
+            "Called by the bzr server when an exception occurs. "
+            "server_exception is called with the sys.exc_info() tuple "
+            "return true for the hook if the exception has been handled, "
+            "in which case the server will exit normally.", (2, 4))
 
 SmartTCPServer.hooks = SmartServerHooks()
 
@@ -373,7 +378,6 @@
         for cleanup in reversed(self.cleanups):
             cleanup()
 
-
 def serve_bzr(transport, host=None, port=None, inet=False):
     """This is the default implementation of 'bzr serve'.
     
@@ -385,6 +389,11 @@
     try:
         bzr_server.set_up(transport, host, port, inet)
         bzr_server.smart_server.serve()
+    except:
+        hook_caught_exception = False
+        for hook in SmartTCPServer.hooks['server_exception']:
+            hook_caught_exception = hook(sys.exc_info())
+        if not hook_caught_exception:
+            raise
     finally:
         bzr_server.tear_down()
-

=== modified file 'bzrlib/tests/blackbox/test_serve.py'
--- a/bzrlib/tests/blackbox/test_serve.py	2011-05-26 08:05:45 +0000
+++ b/bzrlib/tests/blackbox/test_serve.py	2011-05-27 13:12:23 +0000
@@ -81,7 +81,7 @@
             'run_bzr_serve_then_func hook')
         # start a TCP server
         try:
-            out, err = self.run_bzr(['serve'] + list(serve_args))
+            out, err = self.run_bzr(['serve'] + list(serve_args), retcode=retcode)
         except KeyboardInterrupt, e:
             out, err = e.args
         return out, err
@@ -93,6 +93,24 @@
         super(TestBzrServe, self).setUp()
         self.disable_missing_extensions_warning()
 
+    def test_server_exception_with_hook(self):
+        """test exception hook works to catch exceptions from server"""
+        def hook(exception):
+            from bzrlib.trace import note
+            note("catching exception")
+            return True
+        SmartTCPServer.hooks.install_named_hook(
+            'server_exception', hook,
+            'test_server_except_hook hook')
+        args = []
+        out, err = self.run_bzr_serve_then_func(args, retcode=0)
+        self.assertEqual('listening on port: 4155\ncatching exception\n', err)
+
+    def test_server_exception_no_hook(self):
+        """test exception without hook returns error"""
+        args = []
+        out, err = self.run_bzr_serve_then_func(args, retcode=3)
+
     def assertInetServerShutsdownCleanly(self, process):
         """Shutdown the server process looking for errors."""
         # Shutdown the server: the server should shut down when it cannot read

=== modified file 'doc/en/release-notes/bzr-2.4.txt'
--- a/doc/en/release-notes/bzr-2.4.txt	2011-05-27 11:05:47 +0000
+++ b/doc/en/release-notes/bzr-2.4.txt	2011-05-27 13:12:09 +0000
@@ -20,6 +20,10 @@
 
 .. New commands, options, etc that users may wish to try out.
 
+* New hook server_exception in bzrlib.smart.server to catch any
+  exception caused while running bzr serve.  (Jonathan Riddell,
+  #274578)
+
 Improvements
 ************
 




More information about the bazaar-commits mailing list