Skip to content

Commit

Permalink
add custom 404 handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rybesh committed Sep 26, 2023
1 parent 864630e commit a495be9
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion periodo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import logging
from uuid import UUID
from logging.config import dictConfig
from flask import Flask, make_response, g
from flask_principal import Principal, identity_loaded
from flask import Flask, make_response, g, request
from werkzeug.exceptions import NotFound
from werkzeug.http import http_date
from werkzeug.middleware.proxy_fix import ProxyFix
from werkzeug.routing import BaseConverter
Expand Down Expand Up @@ -149,6 +150,24 @@ def add_server_version_header(response):

import periodo.auth # noqa: E402
import periodo.database # noqa: E402
import periodo.highlight # noqa: E402


@app.errorhandler(NotFound)
def handle_not_found_error(_):
message = {
"code": 404,
"status": "Not Found",
"message": f"{request.path[1:]} is not a valid PeriodO identifier. Perhaps you followed a broken link?",
}
if request.accept_mimetypes.best == "application/json":
return make_response(
json.dumps(message),

Check warning

Code scanning / CodeQL

Reflected server-side cross-site scripting Medium

Cross-site scripting vulnerability due to a
user-provided value
.
404,
{"Content-Type": "application/json"},
)
else:
return make_response(periodo.highlight.as_json(message), 404)


@app.errorhandler(periodo.auth.AuthenticationFailed)
Expand Down

0 comments on commit a495be9

Please sign in to comment.