mirror of
https://github.com/ansible/awx.git
synced 2026-02-24 22:46:01 -03:30
Added error handling views and templates, added setting to log request errors to a file.
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
# All Rights Reserved.
|
# All Rights Reserved.
|
||||||
|
|
||||||
from django.http import HttpResponse
|
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.views.decorators.csrf import csrf_exempt
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from ansibleworks.main.models import *
|
from ansibleworks.main.models import *
|
||||||
@@ -26,6 +28,25 @@ import json as python_json
|
|||||||
from base_views import *
|
from base_views import *
|
||||||
from ansibleworks.main.access 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):
|
class ApiRootView(APIView):
|
||||||
'''
|
'''
|
||||||
Ansible Commander REST API
|
Ansible Commander REST API
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from django.http import HttpResponse
|
|||||||
class ExceptionMiddleware(object):
|
class ExceptionMiddleware(object):
|
||||||
|
|
||||||
def process_exception(self, request, exception):
|
def process_exception(self, request, exception):
|
||||||
# FIXME: Should only format plain text for API exceptions.
|
if request.path.startswith('/api/'):
|
||||||
return HttpResponse(traceback.format_exc(exception), content_type="text/plain", status=500)
|
# 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)
|
||||||
|
|||||||
@@ -110,7 +110,6 @@ TEMPLATE_CONTEXT_PROCESSORS += (
|
|||||||
)
|
)
|
||||||
|
|
||||||
MIDDLEWARE_CLASSES += (
|
MIDDLEWARE_CLASSES += (
|
||||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
||||||
'ansibleworks.middleware.exceptions.ExceptionMiddleware',
|
'ansibleworks.middleware.exceptions.ExceptionMiddleware',
|
||||||
'django.middleware.transaction.TransactionMiddleware',
|
'django.middleware.transaction.TransactionMiddleware',
|
||||||
# middleware loaded after this point will be subject to transactions
|
# middleware loaded after this point will be subject to transactions
|
||||||
@@ -221,24 +220,27 @@ LOGGING = {
|
|||||||
'handlers': {
|
'handlers': {
|
||||||
'console': {
|
'console': {
|
||||||
'level': 'DEBUG',
|
'level': 'DEBUG',
|
||||||
#'filters': ['require_debug_true'],
|
'filters': ['require_debug_true'],
|
||||||
'class': 'logging.StreamHandler',
|
'class': 'logging.StreamHandler',
|
||||||
},
|
},
|
||||||
'null': {
|
'null': {
|
||||||
'class': 'django.utils.log.NullHandler',
|
'class': 'django.utils.log.NullHandler',
|
||||||
},
|
},
|
||||||
|
'file': {
|
||||||
|
'class': 'django.utils.log.NullHandler',
|
||||||
|
},
|
||||||
'mail_admins': {
|
'mail_admins': {
|
||||||
'level': 'ERROR',
|
'level': 'ERROR',
|
||||||
'filters': ['require_debug_false'],
|
'filters': ['require_debug_false'],
|
||||||
'class': 'django.utils.log.AdminEmailHandler'
|
'class': 'django.utils.log.AdminEmailHandler',
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
'loggers': {
|
'loggers': {
|
||||||
'django': {
|
'django': {
|
||||||
'handlers': ['console'],
|
'handlers': ['console'],
|
||||||
},
|
},
|
||||||
'django.request': {
|
'django.request': {
|
||||||
'handlers': ['mail_admins', 'console'],
|
'handlers': ['mail_admins', 'console', 'file'],
|
||||||
'level': 'ERROR',
|
'level': 'ERROR',
|
||||||
'propagate': False,
|
'propagate': False,
|
||||||
},
|
},
|
||||||
|
|||||||
4
ansibleworks/templates/403.html
Normal file
4
ansibleworks/templates/403.html
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{% extends "admin/403.html" %}
|
||||||
|
|
||||||
|
{% block breadcrumbs %}
|
||||||
|
{% endblock %}
|
||||||
4
ansibleworks/templates/404.html
Normal file
4
ansibleworks/templates/404.html
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{% extends "admin/404.html" %}
|
||||||
|
|
||||||
|
{% block breadcrumbs %}
|
||||||
|
{% endblock %}
|
||||||
4
ansibleworks/templates/500.html
Normal file
4
ansibleworks/templates/500.html
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{% extends "admin/500.html" %}
|
||||||
|
|
||||||
|
{% block breadcrumbs %}
|
||||||
|
{% endblock %}
|
||||||
20
ansibleworks/templates/admin/403.html
Normal file
20
ansibleworks/templates/admin/403.html
Normal 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> ›
|
||||||
|
{% 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 %}
|
||||||
18
ansibleworks/templates/admin/404.html
Normal file
18
ansibleworks/templates/admin/404.html
Normal 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> ›
|
||||||
|
{% 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 %}
|
||||||
20
ansibleworks/templates/admin/500.html
Normal file
20
ansibleworks/templates/admin/500.html
Normal 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> ›
|
||||||
|
{% 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 %}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
{% extends "admin/base.html" %}
|
{% extends "admin/base.html" %}
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
|
|
||||||
{% block title %}{{ title }} | {% trans 'Ansible Commander Admin' %}{% endblock %}
|
{% block title %}{{ title }} | {% trans 'AnsibleWorks Admin' %}{% endblock %}
|
||||||
|
|
||||||
{% block extrastyle %}
|
{% block extrastyle %}
|
||||||
{{ block.super }}
|
{{ block.super }}
|
||||||
@@ -172,7 +172,7 @@ if (django.jQuery) {
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block branding %}
|
{% block branding %}
|
||||||
<h1 id="site-name">{% trans 'Ansible Commander Admin' %}</h1>
|
<h1 id="site-name">{% trans 'AnsibleWorks Admin' %}</h1>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block userlinks %}
|
{% block userlinks %}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{% extends 'rest_framework/base.html' %}
|
{% extends 'rest_framework/base.html' %}
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
|
|
||||||
{% block title %}{% trans 'Ansible Commander API' %}{% endblock %}
|
{% block title %}{% trans 'AnsibleWorks API' %}{% endblock %}
|
||||||
|
|
||||||
{% block style %}
|
{% block style %}
|
||||||
{{ block.super }}
|
{{ block.super }}
|
||||||
@@ -52,7 +52,7 @@ html body .str a {
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block branding %}
|
{% block branding %}
|
||||||
{% trans 'Ansible Commander API' %}
|
{% trans 'AnsibleWorks API' %}
|
||||||
<span class="powered-by">({% trans 'powered by' %} {{ block.super }})</span>
|
<span class="powered-by">({% trans 'powered by' %} {{ block.super }})</span>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,10 @@
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.conf.urls import *
|
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('',
|
urlpatterns = patterns('',
|
||||||
url(r'', include('ansibleworks.ui.urls', namespace='ui', app_name='ui')),
|
url(r'', include('ansibleworks.ui.urls', namespace='ui', app_name='ui')),
|
||||||
url(r'^api/', include('ansibleworks.main.urls', namespace='main', app_name='main')),
|
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('',
|
urlpatterns += patterns('',
|
||||||
url(r'^admin/', include(admin.site.urls)),
|
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'),
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user