From 40dd9b08beb6b10c20a67bac73eff79b6feea6a2 Mon Sep 17 00:00:00 2001 From: Chris Church Date: Tue, 28 May 2013 14:11:19 -0400 Subject: [PATCH] Added error handling views and templates, added setting to log request errors to a file. --- ansibleworks/main/views.py | 21 +++++++++++++++++++ ansibleworks/middleware/exceptions.py | 7 ++++--- ansibleworks/settings/defaults.py | 12 ++++++----- ansibleworks/templates/403.html | 4 ++++ ansibleworks/templates/404.html | 4 ++++ ansibleworks/templates/500.html | 4 ++++ ansibleworks/templates/admin/403.html | 20 ++++++++++++++++++ ansibleworks/templates/admin/404.html | 18 ++++++++++++++++ ansibleworks/templates/admin/500.html | 20 ++++++++++++++++++ ansibleworks/templates/admin/base_site.html | 4 ++-- .../templates/rest_framework/api.html | 4 ++-- ansibleworks/urls.py | 11 ++++++++++ 12 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 ansibleworks/templates/403.html create mode 100644 ansibleworks/templates/404.html create mode 100644 ansibleworks/templates/500.html create mode 100644 ansibleworks/templates/admin/403.html create mode 100644 ansibleworks/templates/admin/404.html create mode 100644 ansibleworks/templates/admin/500.html diff --git a/ansibleworks/main/views.py b/ansibleworks/main/views.py index f813b3d528..4d414de5ca 100644 --- a/ansibleworks/main/views.py +++ b/ansibleworks/main/views.py @@ -2,6 +2,8 @@ # All Rights Reserved. from django.http import HttpResponse +from django.shortcuts import render_to_response +from django.template import RequestContext from django.views.decorators.csrf import csrf_exempt from django.shortcuts import get_object_or_404 from ansibleworks.main.models import * @@ -26,6 +28,25 @@ import json as python_json from base_views import * from ansibleworks.main.access import * +def handle_error(request, status=404): + context = {} + print request.path, status + if request.path.startswith('/admin/'): + template_name = 'admin/%d.html' % status + else: + template_name = '%d.html' % status + return render_to_response(template_name, context, + context_instance=RequestContext(request)) + +def handle_403(request): + return handle_error(request, 403) + +def handle_404(request): + return handle_error(request, 404) + +def handle_500(request): + return handle_error(request, 500) + class ApiRootView(APIView): ''' Ansible Commander REST API diff --git a/ansibleworks/middleware/exceptions.py b/ansibleworks/middleware/exceptions.py index 293f8eae47..9bfc948513 100644 --- a/ansibleworks/middleware/exceptions.py +++ b/ansibleworks/middleware/exceptions.py @@ -7,6 +7,7 @@ from django.http import HttpResponse class ExceptionMiddleware(object): def process_exception(self, request, exception): - # FIXME: Should only format plain text for API exceptions. - return HttpResponse(traceback.format_exc(exception), content_type="text/plain", status=500) - + if request.path.startswith('/api/'): + # FIXME: For GA, we shouldn't provide this level of detail to the + # end user. + return HttpResponse(traceback.format_exc(exception), content_type="text/plain", status=500) diff --git a/ansibleworks/settings/defaults.py b/ansibleworks/settings/defaults.py index a1a275654b..3dbaa999be 100644 --- a/ansibleworks/settings/defaults.py +++ b/ansibleworks/settings/defaults.py @@ -110,7 +110,6 @@ TEMPLATE_CONTEXT_PROCESSORS += ( ) MIDDLEWARE_CLASSES += ( - 'django.contrib.auth.middleware.AuthenticationMiddleware', 'ansibleworks.middleware.exceptions.ExceptionMiddleware', 'django.middleware.transaction.TransactionMiddleware', # middleware loaded after this point will be subject to transactions @@ -221,24 +220,27 @@ LOGGING = { 'handlers': { 'console': { 'level': 'DEBUG', - #'filters': ['require_debug_true'], + 'filters': ['require_debug_true'], 'class': 'logging.StreamHandler', }, 'null': { 'class': 'django.utils.log.NullHandler', }, + 'file': { + 'class': 'django.utils.log.NullHandler', + }, 'mail_admins': { 'level': 'ERROR', 'filters': ['require_debug_false'], - 'class': 'django.utils.log.AdminEmailHandler' - } + 'class': 'django.utils.log.AdminEmailHandler', + }, }, 'loggers': { 'django': { 'handlers': ['console'], }, 'django.request': { - 'handlers': ['mail_admins', 'console'], + 'handlers': ['mail_admins', 'console', 'file'], 'level': 'ERROR', 'propagate': False, }, diff --git a/ansibleworks/templates/403.html b/ansibleworks/templates/403.html new file mode 100644 index 0000000000..afd194228d --- /dev/null +++ b/ansibleworks/templates/403.html @@ -0,0 +1,4 @@ +{% extends "admin/403.html" %} + +{% block breadcrumbs %} +{% endblock %} diff --git a/ansibleworks/templates/404.html b/ansibleworks/templates/404.html new file mode 100644 index 0000000000..0ccb3c59c5 --- /dev/null +++ b/ansibleworks/templates/404.html @@ -0,0 +1,4 @@ +{% extends "admin/404.html" %} + +{% block breadcrumbs %} +{% endblock %} diff --git a/ansibleworks/templates/500.html b/ansibleworks/templates/500.html new file mode 100644 index 0000000000..2937275395 --- /dev/null +++ b/ansibleworks/templates/500.html @@ -0,0 +1,4 @@ +{% extends "admin/500.html" %} + +{% block breadcrumbs %} +{% endblock %} diff --git a/ansibleworks/templates/admin/403.html b/ansibleworks/templates/admin/403.html new file mode 100644 index 0000000000..8e38808645 --- /dev/null +++ b/ansibleworks/templates/admin/403.html @@ -0,0 +1,20 @@ +{% extends "admin/base_site.html" %} +{% load i18n %} + +{% block title %}{% trans "Forbidden" %}{% endblock %} + +{% block nav-global %}{% endblock %} + +{% block content_title %}{% endblock %} + +{% block breadcrumbs %} + +{% endblock %} + +{% block content %} +

{% trans 'Forbidden' %}

+

{% trans "You don't have permission to access the requested page." %}

+{% endblock %} diff --git a/ansibleworks/templates/admin/404.html b/ansibleworks/templates/admin/404.html new file mode 100644 index 0000000000..0ec799602e --- /dev/null +++ b/ansibleworks/templates/admin/404.html @@ -0,0 +1,18 @@ +{% extends "admin/base_site.html" %} +{% load i18n %} + +{% block title %}{% trans "Not Found" %}{% endblock %} + +{% block nav-global %}{% endblock %} + +{% block breadcrumbs %} + +{% endblock %} + +{% block content %} +

{% trans 'Not Found' %}

+

{% trans "We're sorry, but the requested page could not be found." %}

+{% endblock %} diff --git a/ansibleworks/templates/admin/500.html b/ansibleworks/templates/admin/500.html new file mode 100644 index 0000000000..3a015f0942 --- /dev/null +++ b/ansibleworks/templates/admin/500.html @@ -0,0 +1,20 @@ +{% extends "admin/base_site.html" %} +{% load i18n %} + +{% block title %}{% trans "Server Error" %}{% endblock %} + +{% block nav-global %}{% endblock %} + +{% block content_title %}{% endblock %} + +{% block breadcrumbs %} + +{% endblock %} + +{% block content %} +

{% trans 'Server Error' %}

+

{% trans "A server error has occurred." %}

+{% endblock %} diff --git a/ansibleworks/templates/admin/base_site.html b/ansibleworks/templates/admin/base_site.html index c99e253c11..d3e2a08ab1 100644 --- a/ansibleworks/templates/admin/base_site.html +++ b/ansibleworks/templates/admin/base_site.html @@ -2,7 +2,7 @@ {% extends "admin/base.html" %} {% load i18n %} -{% block title %}{{ title }} | {% trans 'Ansible Commander Admin' %}{% endblock %} +{% block title %}{{ title }} | {% trans 'AnsibleWorks Admin' %}{% endblock %} {% block extrastyle %} {{ block.super }} @@ -172,7 +172,7 @@ if (django.jQuery) { {% endblock %} {% block branding %} -

{% trans 'Ansible Commander Admin' %}

+

{% trans 'AnsibleWorks Admin' %}

{% endblock %} {% block userlinks %} diff --git a/ansibleworks/templates/rest_framework/api.html b/ansibleworks/templates/rest_framework/api.html index 61057cdc14..2b3f82577a 100644 --- a/ansibleworks/templates/rest_framework/api.html +++ b/ansibleworks/templates/rest_framework/api.html @@ -1,7 +1,7 @@ {% extends 'rest_framework/base.html' %} {% load i18n %} -{% block title %}{% trans 'Ansible Commander API' %}{% endblock %} +{% block title %}{% trans 'AnsibleWorks API' %}{% endblock %} {% block style %} {{ block.super }} @@ -52,7 +52,7 @@ html body .str a { {% endblock %} {% block branding %} - {% trans 'Ansible Commander API' %} + {% trans 'AnsibleWorks API' %} ({% trans 'powered by' %} {{ block.super }}) {% endblock %} diff --git a/ansibleworks/urls.py b/ansibleworks/urls.py index b189327620..c715dc2361 100644 --- a/ansibleworks/urls.py +++ b/ansibleworks/urls.py @@ -4,6 +4,10 @@ from django.conf import settings from django.conf.urls import * +handler403 = 'ansibleworks.main.views.handle_403' +handler404 = 'ansibleworks.main.views.handle_404' +handler500 = 'ansibleworks.main.views.handle_500' + urlpatterns = patterns('', url(r'', include('ansibleworks.ui.urls', namespace='ui', app_name='ui')), url(r'^api/', include('ansibleworks.main.urls', namespace='main', app_name='main')), @@ -15,3 +19,10 @@ if 'django.contrib.admin' in settings.INSTALLED_APPS: urlpatterns += patterns('', url(r'^admin/', include(admin.site.urls)), ) + +if settings.DEBUG: + urlpatterns += patterns('ansibleworks.main.views', + url(r'^(?:admin/)?403.html$', 'handle_403'), + url(r'^(?:admin/)?404.html$', 'handle_404'), + url(r'^(?:admin/)?500.html$', 'handle_500'), + )