Add simple error page and email alerts.

Add error testing page.
Fix email fallback.
Fix some routing issues.
This commit is contained in:
Sage Vaillancourt 2022-10-02 15:09:16 -04:00
parent fa11e38c6f
commit 0b6738aad1
7 changed files with 60 additions and 11 deletions

View File

@ -3,10 +3,12 @@
import os import os
import sys import sys
from flask import Flask from flask import Flask, Response, make_response, render_template
from flask_minify import Minify from flask_minify import Minify
from werkzeug.exceptions import HTTPException
import undercover.routes import undercover.routes
import undercover.email
def create_app(test_config=None) -> Flask: def create_app(test_config=None) -> Flask:
@ -28,6 +30,18 @@ def create_app(test_config=None) -> Flask:
os.makedirs(app.instance_path, exist_ok=True) os.makedirs(app.instance_path, exist_ok=True)
@app.errorhandler(Exception)
def internal_error(e) -> HTTPException | Response:
if isinstance(e, HTTPException):
return e
print(str(e))
email.exception_alert(e)
return make_response(render_template('error.jinja2', status=500, error_text='Internal error occurred.', extra_text="We're looking into it!"), 500)
app.register_blueprint(routes.writing_blueprint) app.register_blueprint(routes.writing_blueprint)
@app.errorhandler(404)
def not_found(e) -> Response:
return make_response(render_template('error.jinja2', status=404, error_text='Page not found!'), 404)
return app return app

View File

@ -1,3 +1,4 @@
import traceback
import os import os
import sys import sys
@ -44,5 +45,24 @@ Or copy and paste this link into your browser: {reset_link}
return 200 <= result.status_code <= 299 return 200 <= result.status_code <= 299
def exception_alert(e: Exception):
data = {
'Messages': [
{
"From": {
"Email": "ErrorReporting@undercover.cafe",
"Name": "UnderCover"
},
"To": [{"Email": "sage@sagev.space"}],
"Subject": "UnderCover Internal Error",
"TextPart": str(e) + '\n' + ''.join(traceback.format_exception(None, e, e.__traceback__)),
"CustomID": "InternalServerError"
}
]
}
mailjet.send.create(data=data)
if __name__ == "__main__": if __name__ == "__main__":
send_password_reset('sage@sagev.space', 'https://sagev.space/') send_password_reset('sage@sagev.space', 'https://sagev.space/')

View File

@ -31,5 +31,5 @@ class MockCreator:
mock_sender = types.SimpleNamespace() mock_sender = types.SimpleNamespace()
mock_sender.send = lambda self: MockCreator() mock_sender.send = MockCreator()

View File

@ -106,16 +106,19 @@ def render_index(
), status) ), status)
@writing_blueprint.route('/login', methods=['POST', 'GET']) @writing_blueprint.route('/login', methods=['POST'])
def login() -> Response | str: def login() -> Response | str:
if request.method == 'POST': username = request.form['login']
username = request.form['login'] if db.login(username, request.form['password']):
if db.login(username, request.form['password']): session['username'] = username
session['username'] = username return redirect('/')
return redirect('/')
return render_index(error="Invalid username or password", status=401)
return render_index(status=404) return render_index(error="Invalid username or password", status=401)
@writing_blueprint.route('/test_error', methods=['GET'])
def error_test() -> Response:
raise Exception("Test error")
@writing_blueprint.route('/create_account', methods=['GET']) @writing_blueprint.route('/create_account', methods=['GET'])

View File

@ -1,5 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
{% from "_formhelpers.jinja2" import modal %}
<head> <head>
<title>{% block title %}UnderCover{% endblock title %}</title> <title>{% block title %}UnderCover{% endblock title %}</title>

View File

@ -0,0 +1,11 @@
{% extends "base.jinja2" %}
{% from "_formhelpers.jinja2" import render_field, render_label, render_input %}
{% block title %}UnderCover - {{ status }}{% endblock %}
{% block content %}
<h1>{{ error_text }}</h1>
{% if extra_text %}
<h2>{{ extra_text }}</h2>
{% endif %}
{% endblock content %}

View File

@ -1,5 +1,5 @@
{% extends "base.jinja2" %} {% extends "base.jinja2" %}
{% from "_formhelpers.jinja2" import render_field, render_label, render_input, modal %} {% from "_formhelpers.jinja2" import render_field, render_label, render_input %}
{% block title %}UnderCover{% endblock %} {% block title %}UnderCover{% endblock %}