Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #286 from edx/dsjen/503
Browse files Browse the repository at this point in the history
Update performance page to catch and throw 503.
  • Loading branch information
dsjen committed Mar 10, 2015
2 parents 83b469a + e0f1a24 commit d0c77a9
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 29 deletions.
4 changes: 2 additions & 2 deletions acceptance_tests/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,6 @@ class AccessDeniedErrorPage(ErrorPage):
error_title = u'Access Denied'


class BadGatewayErrorPage(ErrorPage):
error_code = 502
class ServiceUnavailableErrorPage(ErrorPage):
error_code = 503
error_title = u"We're having trouble loading this page. Please try again in a minute."
5 changes: 3 additions & 2 deletions acceptance_tests/test_error_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from bok_choy.web_app_test import WebAppTest

from acceptance_tests import PLATFORM_NAME, APPLICATION_NAME, SUPPORT_URL, ENABLE_ERROR_PAGE_TESTS
from acceptance_tests.pages import ServerErrorPage, NotFoundErrorPage, AccessDeniedErrorPage, BadGatewayErrorPage
from acceptance_tests.pages import ServerErrorPage, NotFoundErrorPage, AccessDeniedErrorPage, \
ServiceUnavailableErrorPage


@skipUnless(ENABLE_ERROR_PAGE_TESTS, 'Error page tests are not enabled.')
class ErrorPagesTests(WebAppTest):
error_page_classes = [ServerErrorPage, NotFoundErrorPage, AccessDeniedErrorPage, BadGatewayErrorPage]
error_page_classes = [ServerErrorPage, NotFoundErrorPage, AccessDeniedErrorPage, ServiceUnavailableErrorPage]

def test_valid_pages(self):
for page_class in self.error_page_classes:
Expand Down
4 changes: 2 additions & 2 deletions analytics_dashboard/core/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class BadGatewayError(Exception):
class ServiceUnavailableError(Exception):
"""
Raise if bad gateway (502).
Raise if service unavailable (503).
"""
pass
8 changes: 4 additions & 4 deletions analytics_dashboard/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from django.template.response import TemplateResponse

from core.exceptions import BadGatewayError
from core.exceptions import ServiceUnavailableError

logger = logging.getLogger(__name__)

Expand All @@ -22,12 +22,12 @@ def get_user_language_preference(self, user):
return user.language


class BadGatewayExceptionMiddleware(object):
class ServiceUnavailableExceptionMiddleware(object):
"""
Display an error template for 502 errors.
"""

def process_exception(self, request, exception):
if type(exception) is BadGatewayError:
if type(exception) is ServiceUnavailableError:
logger.exception(exception)
return TemplateResponse(request, '502.html', status=502)
return TemplateResponse(request, '503.html', status=503)
18 changes: 9 additions & 9 deletions analytics_dashboard/core/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from lang_pref_middleware.tests import LangPrefMiddlewareTestCaseMixin
from testfixtures import LogCapture

from core.exceptions import BadGatewayError
from core.middleware import BadGatewayExceptionMiddleware, LanguagePreferenceMiddleware
from core.exceptions import ServiceUnavailableError
from core.middleware import ServiceUnavailableExceptionMiddleware, LanguagePreferenceMiddleware
from core.models import User


Expand Down Expand Up @@ -38,21 +38,21 @@ def set_user_language_preference(self, user, language):
user.save()


class BadGatewayMiddlewareTests(MiddlewareAssertionMixin, MiddlewareTestCase):
middleware_class = BadGatewayExceptionMiddleware
class ServiceUnavaliableMiddlewareTests(MiddlewareAssertionMixin, MiddlewareTestCase):
middleware_class = ServiceUnavailableExceptionMiddleware

def assertIsBadGatewayErrorResponse(self, response):
self.assertEqual(response.status_code, 502)
def assertIsServiceUnavailableErrorResponse(self, response):
self.assertEqual(response.status_code, 503)
self.assertIs(type(response), TemplateResponse)
self.assertEqual(response.template_name, '502.html')
self.assertEqual(response.template_name, '503.html')

def test_process_exception(self):
request = self.factory.get('/')
self.assertStandardExceptions(request)
with LogCapture(level=logging.WARN) as l:
exception = BadGatewayError()
exception = ServiceUnavailableError()
response = self.middleware.process_exception(request, exception)
self.assertIsBadGatewayErrorResponse(response)
self.assertIsServiceUnavailableErrorResponse(response)

# Verify the exception was logged
l.check(('core.middleware', 'ERROR', str(exception)),)
6 changes: 3 additions & 3 deletions analytics_dashboard/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ def logout_then_login(request, login_url=reverse_lazy('login'), current_app=None
return django.contrib.auth.views.logout_then_login(request, login_url, current_app, extra_context)


class BadGatewayView(TemplateView):
class ServiceUnavailableView(TemplateView):
"""
Bad gateway error page requesting users to wait and reload the page.
Service unavailable error page requesting users to wait and reload the page.
"""
template_name = "502.html"
template_name = "503.html"


class LandingView(TemplateView):
Expand Down
6 changes: 3 additions & 3 deletions analytics_dashboard/courses/views/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from slugify import slugify
from slumber.exceptions import SlumberBaseException

from core.exceptions import BadGatewayError
from core.exceptions import ServiceUnavailableError
from courses.presenters.performance import CoursePerformancePresenter
from courses.views import CourseTemplateWithNavView, CourseAPIMixin

Expand Down Expand Up @@ -45,8 +45,8 @@ def dispatch(self, request, *args, **kwargs):
if response.status_code == 404:
logger.info('Course API data not found for %s: %s', self.course_id, e)
raise Http404
elif response.status_code == 502:
raise BadGatewayError
elif response.status_code == 503:
raise ServiceUnavailableError

# Not a 404. Continue raising the error.
logger.error('An error occurred while using Slumber to communicate with an API: %s', e)
Expand Down
2 changes: 1 addition & 1 deletion analytics_dashboard/help/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class HelpURLMiddleware(object):

def process_template_response(self, _request, response):
# Error responses do not have context.
if response.status_code in [500, 502]:
if response.status_code in [500, 503]:
return response

page_token = response.context_data.get(HELP_CONTEXT_TOKEN_NAME)
Expand Down
2 changes: 1 addition & 1 deletion analytics_dashboard/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'waffle.middleware.WaffleMiddleware',
'core.middleware.LanguagePreferenceMiddleware',
'core.middleware.BadGatewayExceptionMiddleware',
'core.middleware.ServiceUnavailableExceptionMiddleware',
'courses.middleware.CourseMiddleware',
'courses.middleware.CoursePermissionsExceptionMiddleware',
'social.apps.django_app.middleware.SocialAuthExceptionMiddleware',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
{% block error-title %}{% trans "We're having trouble loading this page. Please try again in a minute." %}{% endblock %}
{% block error-copy %}
{{ block.super }}
{% trans "(Technical details: 502 Bad Gateway.)" %}
{% trans "(Technical details: 503 Service Unavailable.)" %}
{% endblock %}
2 changes: 1 addition & 1 deletion analytics_dashboard/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
url(r'^403/$', 'django.views.defaults.permission_denied'),
url(r'^404/$', 'django.views.defaults.page_not_found'),
url(r'^500/$', 'django.views.defaults.server_error'),
url(r'^502/$', views.BadGatewayView.as_view(), name='bad_gateway'),
url(r'^503/$', views.ServiceUnavailableView.as_view()),
)

if os.environ.get('ENABLE_DJANGO_TOOLBAR', False):
Expand Down

0 comments on commit d0c77a9

Please sign in to comment.