title: Python/GAE web request error handling
body: <p>I am developing an application on the Google App Engine using Python.</p>
  
  <p>I have a handler that can return a variety of outputs (html and json at the moment), I am testing for obvious errors in the system based on invalid parameters sent to the request handler.</p>
  
  <p>However what I am doing feels dirty (see below):</p>
  
  <pre><code>class FeedHandler(webapp.RequestHandler):
  def get(self):
      app = self.request.get("id")
      name = self.request.get("name") 
      output_type = self.request.get("output", default_value = "html")
      pretty = self.request.get("pretty", default_value = "")
  
  
      application = model.Application.GetByKey(app)
  
      if application is None:
          if output_type == "json":
              self.response.out.write(simplejson.dumps({ "errorCode" : "Application not found."}))
          self.set_status(404)
          return
  
      category = model.FeedCategory.GetByKey(application, name)
  
      if category is None:
          if output_type == "json":
              self.response.out.write(simplejson.dumps({ "errorCode" : "Category not found."}))
          self.set_status(404)
          return
  </code></pre>
  
  <p>I am specifically handling cases per output type and also and per "assert".</p>
  
  <p>I am keen to here suggestions, patterns and examples on how to clear it up (I know it is going to be a nightmare to try and maintain what I am doing).  </p>
  
  <p>I am toying with the idea of having and raising custom exceptions and having a decorator that will automatically work out how to display the error messages - I think it is a good idea but I am would love to get some feedback and suggestions based on how people have done this in the past.</p>