Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Files
F7123252
D2219.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Subscribers
None
D2219.diff
View Options
diff --git a/swh/web/api/apiresponse.py b/swh/web/api/apiresponse.py
--- a/swh/web/api/apiresponse.py
+++ b/swh/web/api/apiresponse.py
@@ -13,7 +13,7 @@
from swh.storage.exc import StorageDBError, StorageAPIError
from swh.web.api import utils
-from swh.web.common.exc import NotFoundExc, ForbiddenExc
+from swh.web.common.exc import NotFoundExc, ForbiddenExc, BadInputExc
from swh.web.common.utils import shorten_path, gen_path_info
from swh.web.config import get_config
@@ -164,8 +164,10 @@
doc_data: documentation data for HTML response
"""
- error_code = 400
- if isinstance(error, NotFoundExc):
+ error_code = 500
+ if isinstance(error, BadInputExc):
+ error_code = 400
+ elif isinstance(error, NotFoundExc):
error_code = 404
elif isinstance(error, ForbiddenExc):
error_code = 403
diff --git a/swh/web/tests/api/test_apidoc.py b/swh/web/tests/api/test_apidoc.py
--- a/swh/web/tests/api/test_apidoc.py
+++ b/swh/web/tests/api/test_apidoc.py
@@ -6,8 +6,11 @@
from rest_framework.test import APITestCase
from rest_framework.response import Response
+from swh.storage.exc import StorageDBError, StorageAPIError
+
from swh.web.api.apidoc import api_doc, _parse_httpdomain_doc
from swh.web.api.apiurls import api_route
+from swh.web.common.exc import BadInputExc, ForbiddenExc, NotFoundExc
from swh.web.tests.testcase import WebTestCase
# flake8: noqa
@@ -58,6 +61,15 @@
class APIDocTestCase(WebTestCase, APITestCase):
+ exception_http_code = {
+ BadInputExc: 400,
+ ForbiddenExc: 403,
+ NotFoundExc: 404,
+ Exception: 500,
+ StorageAPIError: 503,
+ StorageDBError: 503,
+ }
+
def test_apidoc_nodoc_failure(self):
with self.assertRaises(Exception):
@api_doc('/my/nodoc/url/')
@@ -90,6 +102,26 @@
# then
self.assertEqual(rv.status_code, 200, rv.data)
+ @staticmethod
+ @api_route(r'/test/error/(?P<exc_name>.+)/',
+ 'test-error')
+ @api_doc('/test/error/')
+ def apidoc_test_error_route(request, exc_name):
+ """
+ Sample doc
+ """
+ for e in APIDocTestCase.exception_http_code.keys():
+ if e.__name__ == exc_name:
+ raise e('Error')
+
+ def test_apidoc_error(self):
+ for exc, code in self.exception_http_code.items():
+ # when
+ rv = self.client.get('/api/1/test/error/%s/' % exc.__name__)
+
+ # then
+ self.assertEqual(rv.status_code, code)
+
@staticmethod
@api_route(r'/some/full/(?P<myarg>[0-9]+)/(?P<myotherarg>[0-9]+)/',
'some-complete-doc-route')
diff --git a/swh/web/tests/api/views/test_origin.py b/swh/web/tests/api/views/test_origin.py
--- a/swh/web/tests/api/views/test_origin.py
+++ b/swh/web/tests/api/views/test_origin.py
@@ -12,6 +12,7 @@
from swh.storage.exc import StorageDBError, StorageAPIError
+from swh.web.common.exc import BadInputExc
from swh.web.common.utils import reverse
from swh.web.common.origin_visits import get_origin_visits
from swh.web.tests.strategies import (
@@ -55,7 +56,7 @@
err_msg = 'voluntary error to check the bad request middleware.'
- mock_get_origin_visits.side_effect = ValueError(err_msg)
+ mock_get_origin_visits.side_effect = BadInputExc(err_msg)
url = reverse(
'api-1-origin-visits', url_args={'origin_url': 'http://foo'})
@@ -64,7 +65,7 @@
self.assertEqual(rv.status_code, 400, rv.data)
self.assertEqual(rv['Content-Type'], 'application/json')
self.assertEqual(rv.data, {
- 'exception': 'ValueError',
+ 'exception': 'BadInputExc',
'reason': err_msg})
@patch('swh.web.api.views.origin.get_origin_visits')
diff --git a/swh/web/tests/api/views/test_stat.py b/swh/web/tests/api/views/test_stat.py
--- a/swh/web/tests/api/views/test_stat.py
+++ b/swh/web/tests/api/views/test_stat.py
@@ -8,6 +8,7 @@
from swh.storage.exc import StorageDBError, StorageAPIError
+from swh.web.common.exc import BadInputExc
from swh.web.common.utils import reverse
from swh.web.tests.testcase import WebTestCase
@@ -16,7 +17,7 @@
@patch('swh.web.api.views.stat.service')
def test_api_1_stat_counters_raise_error(self, mock_service):
- mock_service.stat_counters.side_effect = ValueError(
+ mock_service.stat_counters.side_effect = BadInputExc(
'voluntary error to check the bad request middleware.')
url = reverse('api-1-stat-counters')
@@ -25,7 +26,7 @@
self.assertEqual(rv.status_code, 400, rv.data)
self.assertEqual(rv['Content-Type'], 'application/json')
self.assertEqual(rv.data, {
- 'exception': 'ValueError',
+ 'exception': 'BadInputExc',
'reason': 'voluntary error to check the bad request middleware.'})
@patch('swh.web.api.views.stat.service')
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Dec 18, 5:53 AM (2 d, 1 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3216540
Attached To
D2219: Fix HTTP status code for internal server errors
Event Timeline
Log In to Comment