Robert Brewer avatar Robert Brewer committed eac72ce

Fix for #413 (Improve configuration error behavior).

Comments (0)

Files changed (3)

cherrypy/_cperror.py

     pass
 
 class WrongConfigValue(Error):
-    """ Happens when unrepr can't parse a config value """
+    """ Happens when a config value can't be parsed, or is otherwise illegal. """
     pass
 
 class RequestHandled(Exception):

cherrypy/_cpserver.py

         self.blocking = not initOnly
         self.httpserverclass = serverClass
         
+        # Hmmm...we *could* check config in _start instead, but I think
+        # most people would like CP to fail before autoreload kicks in.
+        check_config()
+        
         # Autoreload, but check serverClass. If None, we're not starting
         # our own webserver, and therefore could do Very Bad Things when
         # autoreload calls sys.exit.
         self.start(serverClass=serverClass)
 
 
+def check_config():
+    err = cherrypy.WrongConfigValue
+    for name, section in cherrypy.config.configs.iteritems():
+        for k, v in section.iteritems():
+            if k == "server.environment":
+                if v and v not in cherrypy.config.environments:
+                    raise err("'%s' is not a registered environment." % v)
+
+
 def configure():
     """Perform one-time actions to prepare the CherryPy core."""
     if cherrypy.codecoverage:

cherrypy/test/test_states.py

             self.assertRaises(cherrypy.NotReady, self.getPage, "/")
             self.assertEqual(db_connection.running, False)
             self.assertEqual(len(db_connection.threads), 0)
+    
+    def test_3_ConfigErrors(self):
+        cherrypy.config.update({'server.environment': 'destruction'})
+        try:
+            self.assertRaises(cherrypy.WrongConfigValue,
+                              cherrypy.server.start, True, self.serverClass)
+        finally:
+            cherrypy.server.stop()
 
 
 db_connection = None
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.