MarseyWorld/files/routes/errors.py

62 lines
2.0 KiB
Python
Raw Normal View History

2022-05-04 23:09:46 +00:00
from files.helpers.wrappers import *
from flask import *
from urllib.parse import quote, urlencode
import time
from files.__main__ import app, limiter
# If you're adding an error, go here:
# https://github.com/pallets/werkzeug/blob/main/src/werkzeug/exceptions.py
# and copy the description for the error code you're adding and add it to
# the constant WERKZEUG_ERROR_DESCRIPTIONS so that the default error message
# doesn't show up on the message. Be exact or it won't work properly.
2022-05-04 23:09:46 +00:00
@app.errorhandler(400)
2022-10-11 10:22:28 +00:00
@app.errorhandler(403)
@app.errorhandler(404)
@app.errorhandler(405)
@app.errorhandler(406)
@app.errorhandler(409)
2022-10-11 10:22:28 +00:00
@app.errorhandler(413)
@app.errorhandler(414)
@app.errorhandler(415)
@app.errorhandler(417)
@app.errorhandler(418)
@app.errorhandler(429)
def error(e):
title = ERROR_TITLES.get(e.code, str(e.code))
msg = ERROR_MSGS.get(e.code, str(e.code))
details = e.description
2022-10-11 10:22:28 +00:00
if WERKZEUG_ERROR_DESCRIPTIONS.get(e.code, None) == details:
details = None
if request.headers.get("Authorization") or request.headers.get("xhr"):
return {"error": title, "code": e.code, "description": msg, "details": details}
2022-10-11 10:22:28 +00:00
img = ERROR_MARSEYS.get(e.code, 'marseyl')
return render_template('errors/error.html', err=True, title=title, msg=msg, details=details, img=img), e.code
2022-05-04 23:09:46 +00:00
@app.errorhandler(401)
def error_401(e):
if request.headers.get("Authorization") or request.headers.get("xhr"): return error(e)
2022-05-04 23:09:46 +00:00
else:
path = request.path
qs = urlencode(dict(request.values))
2022-09-01 19:24:06 +00:00
argval = quote(f"{path}?{qs}", safe='').replace('/logged_out','')
if not argval: argval = '/'
if session.get("history"): return redirect(f"/login?redirect={argval}")
else: return redirect(f"/signup?redirect={argval}")
2022-05-04 23:09:46 +00:00
@app.errorhandler(500)
def error_500(e):
2022-10-12 00:00:04 +00:00
if not g: print("500: not g")
if not g.db: print("500: not g.db")
2022-05-04 23:09:46 +00:00
g.db.rollback()
return error(e)
2022-05-04 23:09:46 +00:00
@app.post("/allow_nsfw")
def allow_nsfw():
session["over_18"] = int(time.time()) + 3600
redir = request.values.get("redir")
2022-07-10 14:21:16 +00:00
if is_site_url(redir): return redirect(redir)
return redirect('/')