diff --git a/Makefile b/Makefile index bbf5e80..e633005 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ PYTEST := $(VENV_DIR)/bin/pytest FLASK := $(VENV_DIR)/bin/flask DB := ./db.sqlite SERVER_VERSION := $(shell git describe | cut -c 2-) +SKIP_TRANSLATION ?= false .PHONY: all all: $(DB) @@ -54,7 +55,7 @@ clean: .PHONY: test test: | $(PYTHON3) - TESTING=1 $(PYTEST) test -x + TESTING=1 SKIP_TRANSLATION=$(SKIP_TRANSLATION) $(PYTEST) test -x .PHONY: run run: test diff --git a/periodo/__init__.py b/periodo/__init__.py index a663994..8af35f5 100644 --- a/periodo/__init__.py +++ b/periodo/__init__.py @@ -1,11 +1,13 @@ import os +import re import json import rdflib 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 @@ -149,6 +151,25 @@ 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(_): + sanitized_path = re.sub(r"[^./a-z0-9]", r"", request.path[1:], flags=re.IGNORECASE) + message = { + "code": 404, + "status": "Not Found", + "message": f"{sanitized_path} 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), + 404, + {"Content-Type": "application/json"}, + ) + else: + return make_response(periodo.highlight.as_json(message), 404) @app.errorhandler(periodo.auth.AuthenticationFailed)