feat: Add error message to header for HTML error responses

This commit is contained in:
2025-10-11 18:55:24 +11:00
parent bfc1f0839a
commit 8d6acca5e9
2 changed files with 31 additions and 19 deletions

View File

@@ -149,6 +149,18 @@ def javascript_get(name):
return error_response(request)
return send_from_directory("templates/assets/js", request.path.split("/")[-1])
@app.route("/download/<path:path>")
def download_get(path):
if path not in DOWNLOAD_ROUTES:
return error_response(request, message="Invalid download")
# Check if file exists
path = DOWNLOAD_ROUTES[path]
if os.path.isfile(path):
return send_file(path)
return error_response(request, message="File not found")
# endregion
# region PWA routes
@@ -583,18 +595,6 @@ def supersecretpath_get():
return render_template("ascii.html", ascii_art=ascii_art_html)
@app.route("/download/<path:path>")
def download_get(path):
if path not in DOWNLOAD_ROUTES:
return error_response(request, message="Invalid download")
# Check if file exists
path = DOWNLOAD_ROUTES[path]
if os.path.isfile(path):
return send_file(path)
return error_response(request, message="File not found")
@app.route("/hosting/send-enquiry", methods=["POST"])
def hosting_post():
global EMAIL_REQUEST_COUNT
@@ -756,11 +756,13 @@ def catch_all_get(path: str):
return error_response(request)
@app.errorhandler(404)
def not_found(e):
return error_response(request)
# endregion
if __name__ == "__main__":
app.run(debug=True, port=5000, host="127.0.0.1")

View File

@@ -1,7 +1,8 @@
from flask import Request, render_template, jsonify
from flask import Request, render_template, jsonify, make_response
import os
from functools import cache
def getClientIP(request):
x_forwarded_for = request.headers.get("X-Forwarded-For")
if x_forwarded_for:
@@ -10,6 +11,7 @@ def getClientIP(request):
ip = request.remote_addr
return ip
def getGitCommit():
# if .git exists, get the latest commit hash
if os.path.isdir(".git"):
@@ -47,6 +49,7 @@ def isCurl(request: Request) -> bool:
return True
return False
def isCrawler(request: Request) -> bool:
"""
Check if the request is from a web crawler (e.g., Googlebot, Bingbot)
@@ -79,6 +82,7 @@ def getFilePath(name, path):
if name in files:
return os.path.join(root, name)
def json_response(request: Request, message: str = "404 Not Found", code: int = 404):
return jsonify(
{
@@ -94,6 +98,12 @@ def error_response(request: Request, message: str = "404 Not Found", code: int =
return json_response(request, message, code)
# Check if <error code>.html exists in templates
response = make_response(render_template(
"404.html", code=code, message=message), code)
if os.path.isfile(f"templates/{code}.html"):
return render_template(f"{code}.html"), code
return render_template("404.html"), code
response = make_response(render_template(
f"{code}.html", code=code, message=message), code)
# Add message to response headers
response.headers["X-Error-Message"] = message
return response