Different handler404 and handler500 for API (bug 548959)
This commit is contained in:
Родитель
4afb015648
Коммит
b07fb5e031
|
@ -14,6 +14,7 @@ from django_arecibo.tasks import post
|
|||
from statsd import statsd
|
||||
|
||||
import amo
|
||||
import api
|
||||
import files.tasks
|
||||
from amo.decorators import no_login_required, post_required
|
||||
from amo.utils import log_cef
|
||||
|
@ -70,7 +71,11 @@ def robots(request):
|
|||
def handler404(request):
|
||||
webapp = settings.APP_PREVIEW
|
||||
template = 'amo/404%s.html' % ('_apps' if webapp else '')
|
||||
return jingo.render(request, template, {'webapp': webapp}, status=404)
|
||||
if request.path_info.startswith('/api/'):
|
||||
# Pass over to handler404 view in api if api was targeted
|
||||
return api.views.handler404(request)
|
||||
else:
|
||||
return jingo.render(request, template, {'webapp': webapp}, status=404)
|
||||
|
||||
|
||||
def handler500(request):
|
||||
|
@ -79,7 +84,10 @@ def handler500(request):
|
|||
arecibo = getattr(settings, 'ARECIBO_SERVER_URL', '')
|
||||
if arecibo:
|
||||
post(request, 500)
|
||||
return jingo.render(request, template, {'webapp': webapp}, status=500)
|
||||
if request.path_info.startswith('/api/'):
|
||||
return api.views.handler500(request)
|
||||
else:
|
||||
return jingo.render(request, template, {'webapp': webapp}, status=500)
|
||||
|
||||
|
||||
def csrf_failure(request, reason=''):
|
||||
|
|
|
@ -19,6 +19,7 @@ import api.utils
|
|||
from amo import helpers
|
||||
from amo.tests import TestCase
|
||||
from amo.urlresolvers import reverse
|
||||
from amo.views import handler500
|
||||
from addons.cron import reset_featured_addons
|
||||
from addons.models import (Addon, AppSupport, CompatOverride,
|
||||
CompatOverrideRange, Feature, Preview)
|
||||
|
@ -192,6 +193,33 @@ class APITest(TestCase):
|
|||
|
||||
self.assertContains(response, 'Add-on not found!', status_code=404)
|
||||
|
||||
def test_handler404(self):
|
||||
"""
|
||||
Check separate handler404 response for API.
|
||||
"""
|
||||
response = self.client.get('/en-us/firefox/api/nonsense')
|
||||
doc = pq(response.content)
|
||||
eq_(response.status_code, 404)
|
||||
d = doc('error')
|
||||
self.assertTemplateUsed(response, 'api/message.xml')
|
||||
eq_(d.length, 1)
|
||||
eq_(d.text(), 'Not Found')
|
||||
|
||||
def test_handler500(self):
|
||||
"""
|
||||
Check separate handler500 response for API.
|
||||
"""
|
||||
req = self.client.get('/en-US/firefox/api/').context['request']
|
||||
try:
|
||||
raise NameError('an error')
|
||||
except NameError:
|
||||
r = handler500(req)
|
||||
eq_(r.status_code, 500)
|
||||
doc = pq(r.content)
|
||||
d = doc('error')
|
||||
eq_(d.length, 1)
|
||||
eq_(d.text(), 'Server Error')
|
||||
|
||||
def test_addon_detail_appid(self):
|
||||
"""
|
||||
Make sure we serve an appid. See
|
||||
|
|
|
@ -84,6 +84,16 @@ def render_xml(request, template, context={}, **kwargs):
|
|||
return HttpResponse(rendered, **kwargs)
|
||||
|
||||
|
||||
def handler404(request):
|
||||
context = {'error_level': ERROR, 'msg': 'Not Found'}
|
||||
return render_xml(request, 'api/message.xml', context, status=404)
|
||||
|
||||
|
||||
def handler500(request):
|
||||
context = {'error_level': ERROR, 'msg': 'Server Error'}
|
||||
return render_xml(request, 'api/message.xml', context, status=500)
|
||||
|
||||
|
||||
def validate_api_version(version):
|
||||
"""
|
||||
We want to be able to deprecate old versions of the API, therefore we check
|
||||
|
|
Загрузка…
Ссылка в новой задаче