diff --git a/swh/objstorage/api/server.py b/swh/objstorage/api/server.py --- a/swh/objstorage/api/server.py +++ b/swh/objstorage/api/server.py @@ -77,6 +77,11 @@ objstorage = None +@app.errorhandler(ObjNotFoundError) +def not_found_error_handler(exception): + return error_handler(exception, encode_data, status_code=404) + + @app.errorhandler(Error) def argument_error_handler(exception): return error_handler(exception, encode_data, status_code=400) diff --git a/swh/objstorage/tests/objstorage_testing.py b/swh/objstorage/tests/objstorage_testing.py --- a/swh/objstorage/tests/objstorage_testing.py +++ b/swh/objstorage/tests/objstorage_testing.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2020 The Software Heritage developers +# Copyright (C) 2015-2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -115,7 +115,7 @@ def test_check_missing(self): content, obj_id = self.hash_content(b"check_missing") - with self.assertRaises(exc.Error): + with self.assertRaises(exc.ObjNotFoundError): self.storage.check(obj_id) def test_check_present(self): diff --git a/swh/objstorage/tests/test_objstorage_api.py b/swh/objstorage/tests/test_objstorage_api.py --- a/swh/objstorage/tests/test_objstorage_api.py +++ b/swh/objstorage/tests/test_objstorage_api.py @@ -1,13 +1,15 @@ -# Copyright (C) 2015-2020 The Software Heritage developers +# Copyright (C) 2015-2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +from hashlib import sha1 import shutil import tempfile import unittest import pytest +import requests from swh.core.api.tests.server_testing import ServerTestFixture from swh.objstorage.api.server import app @@ -38,6 +40,13 @@ super().tearDown() shutil.rmtree(self.tmpdir) + def test_404_status_code_on_missing_object(self): + response = requests.post( + self.url() + "content/get", + json={"obj_id": sha1(b"missing").hexdigest()}, + ) + assert response.status_code == 404 + @pytest.mark.skip("makes no sense to test this for the remote api") def test_delete_not_allowed(self): pass