Added error handling views and templates, added setting to log request errors to a file.

This commit is contained in:
Chris Church 2013-05-28 14:11:19 -04:00
parent e61a966464
commit 40dd9b08be
12 changed files with 117 additions and 12 deletions

View File

@ -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

View File

@ -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)

View File

@ -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,
},

View File

@ -0,0 +1,4 @@
{% extends "admin/403.html" %}
{% block breadcrumbs %}
{% endblock %}

View File

@ -0,0 +1,4 @@
{% extends "admin/404.html" %}
{% block breadcrumbs %}
{% endblock %}

View File

@ -0,0 +1,4 @@
{% extends "admin/500.html" %}
{% block breadcrumbs %}
{% endblock %}

View File

@ -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 %}
<div class="breadcrumbs">
<a href="{% url "admin:index" %}">{% trans "Home" %}</a> &rsaquo;
{% trans "Forbidden" %}
</div>
{% endblock %}
{% block content %}
<h1 style="margin-bottom: 0.7em;">{% trans 'Forbidden' %}</h1>
<p>{% trans "You don't have permission to access the requested page." %}</p>
{% endblock %}

View File

@ -0,0 +1,18 @@
{% extends "admin/base_site.html" %}
{% load i18n %}
{% block title %}{% trans "Not Found" %}{% endblock %}
{% block nav-global %}{% endblock %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url "admin:index" %}">{% trans "Home" %}</a> &rsaquo;
{% trans "Not Found" %}
</div>
{% endblock %}
{% block content %}
<h1 style="margin-bottom: 0.7em;">{% trans 'Not Found' %}</h1>
<p>{% trans "We're sorry, but the requested page could not be found." %}</p>
{% endblock %}

View File

@ -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 %}
<div class="breadcrumbs">
<a href="{% url "admin:index" %}">{% trans "Home" %}</a> &rsaquo;
{% trans "Server Error" %}
</div>
{% endblock %}
{% block content %}
<h1 style="margin-bottom: 0.7em;">{% trans 'Server Error' %}</h1>
<p>{% trans "A server error has occurred." %}</p>
{% endblock %}

View File

@ -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 %}
<h1 id="site-name">{% trans 'Ansible Commander Admin' %}</h1>
<h1 id="site-name">{% trans 'AnsibleWorks Admin' %}</h1>
{% endblock %}
{% block userlinks %}

View File

@ -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' %}
<span class="powered-by">({% trans 'powered by' %} {{ block.super }})</span>
{% endblock %}

View File

@ -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'),
)