app/soc/views/helper/decorators.py
changeset 365 74dec172944e
child 367 5d5730f65fc8
equal deleted inserted replaced
364:ab47d3f494b3 365:74dec172944e
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Views decorators.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Pawel Solyga" <pawel.solyga@gmail.com>',
       
    22   ]
       
    23 
       
    24 
       
    25 import logging
       
    26 
       
    27 from functools import wraps
       
    28 
       
    29 from google.appengine.runtime import DeadlineExceededError
       
    30 
       
    31 from django import http
       
    32 
       
    33 
       
    34 def view(func):
       
    35   """Decorator that insists that exceptions are handled by view."""
       
    36   @wraps(func)
       
    37   def view_wrapper(request, *args, **kwds):
       
    38     try:
       
    39       return func(request, *args, **kwds)
       
    40     except DeadlineExceededError:
       
    41       logging.exception('DeadlineExceededError')
       
    42       return http.HttpResponse('DeadlineExceededError')
       
    43     except MemoryError:
       
    44       logging.exception('MemoryError')
       
    45       return http.HttpResponse('MemoryError')
       
    46     except AssertionError:
       
    47       logging.exception('AssertionError')
       
    48       return http.HttpResponse('AssertionError')
       
    49 
       
    50   return view_wrapper