Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More useful 404 message #321

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Fixed Show fixed Hide fixed
404,
{"Content-Type": "application/json"},
)
else:
return make_response(periodo.highlight.as_json(message), 404)


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