diff --git a/undercover/__init__.py b/undercover/__init__.py index 5908d2a..ebfe38e 100644 --- a/undercover/__init__.py +++ b/undercover/__init__.py @@ -3,10 +3,12 @@ import os import sys -from flask import Flask +from flask import Flask, Response, make_response, render_template from flask_minify import Minify +from werkzeug.exceptions import HTTPException import undercover.routes +import undercover.email 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) + @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.errorhandler(404) + def not_found(e) -> Response: + return make_response(render_template('error.jinja2', status=404, error_text='Page not found!'), 404) + return app diff --git a/undercover/email.py b/undercover/email.py index d362e6a..b48061f 100644 --- a/undercover/email.py +++ b/undercover/email.py @@ -1,3 +1,4 @@ +import traceback import os import sys @@ -44,5 +45,24 @@ Or copy and paste this link into your browser: {reset_link} 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__": send_password_reset('sage@sagev.space', 'https://sagev.space/') diff --git a/undercover/fallback.py b/undercover/fallback.py index f3324ff..536b105 100644 --- a/undercover/fallback.py +++ b/undercover/fallback.py @@ -31,5 +31,5 @@ class MockCreator: mock_sender = types.SimpleNamespace() -mock_sender.send = lambda self: MockCreator() +mock_sender.send = MockCreator() diff --git a/undercover/routes.py b/undercover/routes.py index 2cc9604..eac3053 100644 --- a/undercover/routes.py +++ b/undercover/routes.py @@ -106,16 +106,19 @@ def render_index( ), status) -@writing_blueprint.route('/login', methods=['POST', 'GET']) +@writing_blueprint.route('/login', methods=['POST']) def login() -> Response | str: - if request.method == 'POST': - username = request.form['login'] - if db.login(username, request.form['password']): - session['username'] = username - return redirect('/') - return render_index(error="Invalid username or password", status=401) + username = request.form['login'] + if db.login(username, request.form['password']): + session['username'] = username + return redirect('/') - 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']) diff --git a/undercover/templates/base.jinja2 b/undercover/templates/base.jinja2 index 84a0d03..ef62256 100644 --- a/undercover/templates/base.jinja2 +++ b/undercover/templates/base.jinja2 @@ -1,5 +1,6 @@ +{% from "_formhelpers.jinja2" import modal %} {% block title %}UnderCover{% endblock title %} diff --git a/undercover/templates/error.jinja2 b/undercover/templates/error.jinja2 new file mode 100644 index 0000000..5f6b276 --- /dev/null +++ b/undercover/templates/error.jinja2 @@ -0,0 +1,11 @@ +{% extends "base.jinja2" %} +{% from "_formhelpers.jinja2" import render_field, render_label, render_input %} + +{% block title %}UnderCover - {{ status }}{% endblock %} + +{% block content %} +

{{ error_text }}

+ {% if extra_text %} +

{{ extra_text }}

+ {% endif %} +{% endblock content %} diff --git a/undercover/templates/writing.jinja2 b/undercover/templates/writing.jinja2 index dd4ad6b..a9e590e 100644 --- a/undercover/templates/writing.jinja2 +++ b/undercover/templates/writing.jinja2 @@ -1,5 +1,5 @@ {% 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 %}