Skip to content

Commit

Permalink
Merge pull request #321 from periodo/more-useful-404
Browse files Browse the repository at this point in the history
More useful 404 message
  • Loading branch information
rybesh authored Sep 26, 2023
2 parents ccb31f0 + aee6c9b commit f066b6f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion periodo/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f066b6f

Please sign in to comment.