Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/api/server.py
# 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 | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import logging | import logging | ||||
import os | import os | ||||
from typing import Any, Dict, Optional | from typing import Any, Dict, Optional | ||||
from psycopg2.errors import QueryCanceled | |||||
from swh.core import config | from swh.core import config | ||||
from swh.core.api import RPCServerApp | from swh.core.api import RPCServerApp | ||||
from swh.core.api import encode_data_server as encode_data | from swh.core.api import encode_data_server as encode_data | ||||
from swh.core.api import error_handler | from swh.core.api import error_handler, serializers | ||||
from swh.storage import get_storage as get_swhstorage | from swh.storage import get_storage as get_swhstorage | ||||
from ..exc import StorageArgumentException | from ..exc import StorageArgumentException | ||||
from ..interface import StorageInterface | from ..interface import StorageInterface | ||||
from ..metrics import send_metric, timed | from ..metrics import send_metric, timed | ||||
from .serializers import DECODERS, ENCODERS | from .serializers import DECODERS, ENCODERS | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | |||||
storage = None | storage = None | ||||
@app.errorhandler(StorageArgumentException) | @app.errorhandler(StorageArgumentException) | ||||
def argument_error_handler(exception): | def argument_error_handler(exception): | ||||
return error_handler(exception, encode_data, status_code=400) | return error_handler(exception, encode_data, status_code=400) | ||||
@app.errorhandler(QueryCanceled) | |||||
def querycanceled_error_handler(exception): | |||||
# Same as error_handler(exception, encode_data); but does not log or send to Sentry. | |||||
# These errors are noisy, and are better logged on the caller's side after it | |||||
# retried a few times | |||||
response = encode_data(serializers.exception_to_dict(exception)) | |||||
response.status_code = 500 | |||||
return response | |||||
@app.errorhandler(Exception) | @app.errorhandler(Exception) | ||||
def my_error_handler(exception): | def default_error_handler(exception): | ||||
return error_handler(exception, encode_data) | return error_handler(exception, encode_data) | ||||
@app.route("/") | @app.route("/") | ||||
@timed | @timed | ||||
def index(): | def index(): | ||||
return """<html> | return """<html> | ||||
<head><title>Software Heritage storage server</title></head> | <head><title>Software Heritage storage server</title></head> | ||||
▲ Show 20 Lines • Show All 73 Lines • Show Last 20 Lines |