from django.http import HttpResponseServerError 
import sys, traceback
def exception2response(func):
    """a decorator function that converts the python exception into HttpResponse
    """
    def new_func(request, *args, **kwargs):
        try:
            return func(request, *args, **kwargs)
        except Exception, e:
            traceback.print_exc()
            if request.is_ajax():
                (exc_type, exc_info, tb) = sys.exc_info()
                response = "%s\n" % exc_type.__name__
                response += "%s\n\n" % exc_info
                response += "TRACEBACK:\n"    
                for tb in traceback.format_tb(tb):
                    response += "%s\n" % tb
                print response
                return HttpResponseServerError(response)
            raise
    return new_func
