Skip to content

Commit

Permalink
fix: correctely address HTTP server errors (#1872)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsam committed Feb 13, 2021
1 parent 1abd4f6 commit 2fd5052
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
3 changes: 3 additions & 0 deletions renku/service/config.py
Expand Up @@ -32,6 +32,9 @@
INVALID_PARAMS_ERROR_CODE = -32602
INTERNAL_FAILURE_ERROR_CODE = -32603

HTTP_SERVER_ERROR = -32000


SERVICE_NAME = "Renku Service"
OPENAPI_VERSION = "2.0"
API_VERSION = "v1"
Expand Down
44 changes: 37 additions & 7 deletions renku/service/entrypoint.py
Expand Up @@ -28,15 +28,25 @@
from flask_apispec import FlaskApiSpec
from flask_swagger_ui import get_swaggerui_blueprint
from jwt import InvalidTokenError
from sentry_sdk import capture_exception
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from sentry_sdk.integrations.rq import RqIntegration

from renku.service.cache import cache
from renku.service.config import API_SPEC_URL, API_VERSION, CACHE_DIR, OPENAPI_VERSION, SERVICE_NAME, SWAGGER_URL
from renku.service.config import (
API_SPEC_URL,
API_VERSION,
CACHE_DIR,
HTTP_SERVER_ERROR,
OPENAPI_VERSION,
SERVICE_NAME,
SWAGGER_URL,
)
from renku.service.logger import service_log
from renku.service.serializers.headers import JWT_TOKEN_SECRET
from renku.service.utils.json_encoder import SvcJSONEncoder
from renku.service.views import error_response
from renku.service.views.cache import (
CACHE_BLUEPRINT_TAG,
cache_blueprint,
Expand Down Expand Up @@ -186,15 +196,35 @@ def after_request(response):

@app.errorhandler(Exception)
def exceptions(e):
"""App exception logger."""
"""This exceptions handler manages Flask/Werkzeug exceptions.
For Renku exception handlers check ``service/decorators.py``
"""

# NOTE: Capture werkzeug exceptions and propagate them to sentry.
capture_exception(e)

# NOTE: Capture traceback for dumping it to the log.
tb = traceback.format_exc()
service_log.error(
"{} {} {} {} 5xx INTERNAL SERVER ERROR\n{}".format(
request.remote_addr, request.method, request.scheme, request.full_path, tb

if hasattr(e, "code") and e.code == 404:
service_log.error(
"{} {} {} {} 404 NOT FOUND\n{}".format(
request.remote_addr, request.method, request.scheme, request.full_path, tb
)
)
)
return error_response(HTTP_SERVER_ERROR - e.code, e.name)

if hasattr(e, "code") and e.code >= 500:
service_log.error(
"{} {} {} {} 5xx INTERNAL SERVER ERROR\n{}".format(
request.remote_addr, request.method, request.scheme, request.full_path, tb
)
)
return error_response(HTTP_SERVER_ERROR - e.code, e.name)

return e.status_code
# NOTE: Werkzeug exceptions should be covered above, following line is for unexpected HTTP server errors.
return error_response(HTTP_SERVER_ERROR, str(e))


app.debug = os.environ.get("DEBUG_MODE", "false") == "true"
Expand Down

0 comments on commit 2fd5052

Please sign in to comment.