views:

60

answers:

2

How can I redirect 500 Errors from google appengine to another location.

The following is an example scenario:

All requests to http://example.appspot.com/testfile.ext in case of a 500 error should redirect to http://www.example.com/testfile.ext

This is basically combining the 500 error with a 302 redirect. Is it possible if not is there a work around?

A: 

It can redirect a failure with simplest way I know

   try:
    #stuff    
   except:
     self.redirect('http://www.example.com/testfile.ext')
     return

Or this slightly more complicated redirect one can modify from 301 to 302

def redirect_from_appspot(wsgi_app):
    def redirect_if_needed(env, start_response):
        if env["HTTP_HOST"].startswith('my_app_name.appspot.com'):
            import webob, urlparse
            request = webob.Request(env)
            scheme, netloc, path, query, fragment = urlparse.urlsplit(request.url)
            url = urlparse.urlunsplit([scheme, 'www.my_domain.com', path, query, fragment])
            start_response('301 Moved Permanently', [('Location', url)])
            return ["301 Moved Peramanently",
                  "Click Here" % url]
        else:
            return wsgi_app(env, start_response)
    return redirect_if_needed

Case depends if 500 Error is your app or google server. Easiest would be featuring it in gae console we can request app engine team to enable, or yaml or most formal with regex. The easy way is try, except and redirect.

LarsOn
The method you told only works in handling errors in a case by case basis. Isn't there a method to handle all 500 errors on my app.
GeekTantra
A: 

If you are using webapp you can override the handle_exception method of a base class and have all your handlers inherit from that.

class BaseHandler(webapp.RequestHandler):
  def handle_exception(self, exception, debug_mode):
    self.redirect('http://www.example.com/' + self.request.path)

class MyHandler(BaseHandler):
  def get(self):
    # etc...

That would redirect all the errors except ones raised by the google proxy servers and you can't change those...

Brandon Thomson