diff --git a/web/app/__init__.py b/web/app/__init__.py index c4e094aa..64674f34 100644 --- a/web/app/__init__.py +++ b/web/app/__init__.py @@ -38,6 +38,8 @@ def create_app(): app.register_blueprint(content_blueprint, url_prefix='/content') from .corpora import corpora as corpora_blueprint app.register_blueprint(corpora_blueprint, url_prefix='/corpora') + from .errors import errors as errors_blueprint + app.register_blueprint(errors_blueprint) from .jobs import jobs as jobs_blueprint app.register_blueprint(jobs_blueprint, url_prefix='/jobs') from .main import main as main_blueprint diff --git a/web/app/errors/__init__.py b/web/app/errors/__init__.py new file mode 100644 index 00000000..3087208b --- /dev/null +++ b/web/app/errors/__init__.py @@ -0,0 +1,5 @@ +from flask import Blueprint + + +errors = Blueprint('errors', __name__) +from app.errors import handlers diff --git a/web/app/errors/handlers.py b/web/app/errors/handlers.py new file mode 100644 index 00000000..c568fd8c --- /dev/null +++ b/web/app/errors/handlers.py @@ -0,0 +1,42 @@ +from flask import render_template, request, jsonify +from . import errors + + +@errors.app_errorhandler(403) +def forbidden(e): + if (request.accept_mimetypes.accept_json + and not request.accept_mimetypes.accept_html): + response = jsonify({'error': 'forbidden'}) + response.status_code = 403 + return response + return render_template('errors/403.html.j2', title='Forbidden'), 403 + + +@errors.app_errorhandler(404) +def not_found(e): + if (request.accept_mimetypes.accept_json + and not request.accept_mimetypes.accept_html): + response = jsonify({'error': 'not found'}) + response.status_code = 404 + return response + return render_template('errors/404.html.j2', title='Not Found'), 404 + + +@errors.app_errorhandler(413) +def payload_too_large(e): + if (request.accept_mimetypes.accept_json + and not request.accept_mimetypes.accept_html): + response = jsonify({'error': 'payload too large'}) + response.status_code = 413 + return response + return render_template('errors/413.html.j2', title='Payload Too Large'), 413 + + +@errors.app_errorhandler(500) +def internal_server_error(e): + if (request.accept_mimetypes.accept_json + and not request.accept_mimetypes.accept_html): + response = jsonify({'error': 'internal server error'}) + response.status_code = 500 + return response + return render_template('errors/500.html.j2', title='Internal Server Error'), 500 diff --git a/web/app/main/__init__.py b/web/app/main/__init__.py index e58f63b5..74430247 100644 --- a/web/app/main/__init__.py +++ b/web/app/main/__init__.py @@ -2,4 +2,4 @@ from flask import Blueprint main = Blueprint('main', __name__) -from . import errors, views # noqa +from . import views # noqa diff --git a/web/app/main/errors.py b/web/app/main/errors.py deleted file mode 100644 index 625555be..00000000 --- a/web/app/main/errors.py +++ /dev/null @@ -1,32 +0,0 @@ -from flask import render_template, request, jsonify -from . import main - - -@main.app_errorhandler(403) -def forbidden(e): - if request.accept_mimetypes.accept_json and \ - not request.accept_mimetypes.accept_html: - response = jsonify({'error': 'forbidden'}) - response.status_code = 403 - return response - return render_template('403.html.j2', title='Forbidden'), 403 - - -@main.app_errorhandler(404) -def page_not_found(e): - if request.accept_mimetypes.accept_json and \ - not request.accept_mimetypes.accept_html: - response = jsonify({'error': 'not found'}) - response.status_code = 404 - return response - return render_template('404.html.j2', title='Not Found'), 404 - - -@main.app_errorhandler(500) -def internal_server_error(e): - if request.accept_mimetypes.accept_json and \ - not request.accept_mimetypes.accept_html: - response = jsonify({'error': 'internal server error'}) - response.status_code = 500 - return response - return render_template('500.html.j2', title='Internal Server Error'), 500 diff --git a/web/app/templates/403.html.j2 b/web/app/templates/403.html.j2 deleted file mode 100644 index eba64fb3..00000000 --- a/web/app/templates/403.html.j2 +++ /dev/null @@ -1,7 +0,0 @@ -{% extends "nopaque.html.j2" %} - -{% block page_content %} -
-

This site is forbidden for you.

-
-{% endblock %} diff --git a/web/app/templates/404.html.j2 b/web/app/templates/404.html.j2 deleted file mode 100644 index b0fdc4fb..00000000 --- a/web/app/templates/404.html.j2 +++ /dev/null @@ -1,7 +0,0 @@ -{% extends "nopaque.html.j2" %} - -{% block page_content %} -
-

Site has not been found.

-
-{% endblock %} diff --git a/web/app/templates/500.html.j2 b/web/app/templates/500.html.j2 deleted file mode 100644 index 4c9c4a33..00000000 --- a/web/app/templates/500.html.j2 +++ /dev/null @@ -1,7 +0,0 @@ -{% extends "nopaque.html.j2" %} - -{% block page_content %} -
-

Internal Server Error. We are Sorry!

-
-{% endblock %} diff --git a/web/app/templates/errors/403.html.j2 b/web/app/templates/errors/403.html.j2 new file mode 100644 index 00000000..7a4eca4e --- /dev/null +++ b/web/app/templates/errors/403.html.j2 @@ -0,0 +1,19 @@ +{% extends 'nopaque.html.j2' %} + +{% block page_content %} +
+

{{ title }}

+

{{ request.path }}

+

Alternatively, you can visit the Main Page or read more information about this type of error.

+
+ + +{% endblock page_content %} diff --git a/web/app/templates/errors/404.html.j2 b/web/app/templates/errors/404.html.j2 new file mode 100644 index 00000000..a6bf7492 --- /dev/null +++ b/web/app/templates/errors/404.html.j2 @@ -0,0 +1,19 @@ +{% extends 'nopaque.html.j2' %} + +{% block page_content %} +
+

{{ title }}

+

{{ request.path }}

+

Alternatively, you can visit the Main Page or read more information about this type of error.

+
+ + +{% endblock page_content %} diff --git a/web/app/templates/errors/413.html.j2 b/web/app/templates/errors/413.html.j2 new file mode 100644 index 00000000..1be86042 --- /dev/null +++ b/web/app/templates/errors/413.html.j2 @@ -0,0 +1,19 @@ +{% extends 'nopaque.html.j2' %} + +{% block page_content %} +
+

{{ title }}

+

{{ request.path }}

+

Alternatively, you can visit the Main Page or read more information about this type of error.

+
+ + +{% endblock page_content %} diff --git a/web/app/templates/errors/500.html.j2 b/web/app/templates/errors/500.html.j2 new file mode 100644 index 00000000..1faaa42d --- /dev/null +++ b/web/app/templates/errors/500.html.j2 @@ -0,0 +1,19 @@ +{% extends 'nopaque.html.j2' %} + +{% block page_content %} +
+

{{ title }}

+

{{ request.path }}

+

Alternatively, you can visit the Main Page or read more information about this type of error.

+
+ + +{% endblock page_content %}