app/soc/middleware/exception_handler.py
changeset 2901 6c8ba67289a6
child 2906 6fb53ed7aff4
equal deleted inserted replaced
2900:c9a723492dd5 2901:6c8ba67289a6
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2009 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 """Middleware to handle exceptions.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22   ]
       
    23 
       
    24 
       
    25 import logging
       
    26 
       
    27 from google.appengine.runtime import DeadlineExceededError
       
    28 from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError
       
    29 
       
    30 from django import http
       
    31 
       
    32 from soc.views.helper import responses
       
    33 from soc.views import out_of_band
       
    34 
       
    35 
       
    36 class ExceptionHandlerMiddleware(object):
       
    37   """Middleware class to handle exceptions..
       
    38   """
       
    39 
       
    40   def process_exception(self, request, exception):
       
    41     template = None
       
    42     context = responses.getUniversalContext(request)
       
    43 
       
    44     if isinstance(exception, DeadlineExceededError):
       
    45       template = 'soc/deadline_exceeded.html'
       
    46     if isinstance(exception, MemoryError):
       
    47       template = 'soc/memory_error.html'
       
    48     if isinstance(exception, AssertionError):
       
    49       template = 'soc/assertion_error.html'
       
    50     if isinstance(exception, out_of_band.Error):
       
    51       return responses.errorResponse(error, request)
       
    52 
       
    53     if template:
       
    54       logging.exception(exception)
       
    55       return responses.respond(request, template, context=context)
       
    56 
       
    57     # let Django handle it
       
    58     return None