diff --git a/swh/storage/api/client.py b/swh/storage/api/client.py index 222076f7..125f132c 100644 --- a/swh/storage/api/client.py +++ b/swh/storage/api/client.py @@ -1,24 +1,41 @@ -# Copyright (C) 2015-2017 The Software Heritage developers +# Copyright (C) 2015-2020 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 swh.core.api import RPCClient +from swh.core.api import RPCClient, RemoteException -from ..exc import StorageAPIError +from .. import HashCollision +from ..exc import StorageAPIError, StorageArgumentException from ..interface import StorageInterface class RemoteStorage(RPCClient): """Proxy to a remote storage API""" api_exception = StorageAPIError backend_class = StorageInterface + reraise_exceptions = [ + StorageArgumentException, + ] + + def raise_for_status(self, response) -> None: + try: + super().raise_for_status(response) + except RemoteException as e: + if e.response is not None and e.response.status_code == 500 \ + and e.args and e.args[0].get('type') == 'HashCollision': + # XXX: workaround until we fix these HashCollisions happening + # when they shouldn't + raise HashCollision( + *e.args[0]['args']) + else: + raise def reset(self): return self.post('reset', {}) def stat_counters(self): return self.get('stat/counters') def refresh_stat_counters(self): return self.get('stat/refresh') diff --git a/swh/storage/api/server.py b/swh/storage/api/server.py index 05cf12e0..dd49c447 100644 --- a/swh/storage/api/server.py +++ b/swh/storage/api/server.py @@ -1,118 +1,124 @@ # Copyright (C) 2015-2019 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 import os import logging from swh.core import config from swh.storage import get_storage as get_swhstorage from swh.core.api import (RPCServerApp, error_handler, encode_data_server as encode_data) from ..interface import StorageInterface from ..metrics import timed +from ..exc import StorageArgumentException def get_storage(): global storage if not storage: storage = get_swhstorage(**app.config['storage']) return storage app = RPCServerApp(__name__, backend_class=StorageInterface, backend_factory=get_storage) storage = None +@app.errorhandler(StorageArgumentException) +def argument_error_handler(exception): + return error_handler(exception, encode_data, status_code=400) + + @app.errorhandler(Exception) def my_error_handler(exception): return error_handler(exception, encode_data) @app.route('/') @timed def index(): return '''
You have reached the
Software Heritage
storage server.
See its
documentation
and API for more information