Skip to content

Commit

Permalink
Merge pull request #160 from AberystwythSystemsBiology/fix/better-erro
Browse files Browse the repository at this point in the history
Fix/better errors
  • Loading branch information
KeironO authored Feb 7, 2022
2 parents 017e469 + 6697767 commit 4f15e75
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 26 deletions.
53 changes: 33 additions & 20 deletions services/web/app/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@

from binascii import hexlify
from functools import wraps
from http.client import INTERNAL_SERVER_ERROR
from logging import error
import traceback
from os import urandom

from flask import render_template
from werkzeug.exceptions import *
import os
from random import choice

import sys

error_handlers = []

Expand Down Expand Up @@ -75,54 +78,64 @@ def wrapped(*args, **kwargs):
return decorator


def handle_error(e, code, json=False):
def handle_error(code, description, traceback, json=False):
if json:
return {"message": e}, code
return {"message": description, "traceback": traceback}, code
return (
render_template(
"error.html", e=code, smiley=choice(sad), text=e, title="{}".format(code)
"error.html",
code=code,
smiley=choice(sad),
text=description,
traceback=traceback,
),
code,
)


@errorhandler(Unauthorized.code)
def unauthorised(e="401: Unauthorised", json=False):
return handle_error(e, Unauthorized.code, json)
return handle_error(
Unauthorized.code, Unauthorized.description, traceback.format_exc(), json
)


@errorhandler(NotFound.code)
def not_found(e="404: Not Found", json=False):
def not_found(e="404: Page Not Found", json=False):

return handle_error(
e if "check your spelling" not in "{}".format(e) else "404: Not Found",
NotFound.code,
json,
NotFound.code, NotFound.description, traceback.format_exc(), json
)


@errorhandler(Forbidden.code)
def forbidden(e="403: Forbidden", json=False):
return handle_error(e, Forbidden.code, json)
return handle_error(
Forbidden.code, Forbidden.description, traceback.format_exc(), json
)


@errorhandler(MethodNotAllowed.code)
def method_not_allowed(e="405: Method Not Allowed", json=False):
return handle_error(e, MethodNotAllowed.code, json)
return handle_error(
MethodNotAllowed.code,
MethodNotAllowed.description,
traceback.format_exc(),
json,
)


@errorhandler(Gone.code)
def gone(e="410: Gone", json=False):
return handle_error(e, Gone.code, json)
return handle_error(Gone.code, Gone.descrition, traceback.format_exc(), json)


@errorhandler(Exception)
@errorhandler(InternalServerError.code)
def internal_error(exce):
if os.environ["FLASK_CONFIG"] == "development":
raise exce

code = hexlify(urandom(4)).decode()
error(Exception("Code: {}".format(code), exce), exc_info=True)
text = "500: Something bad has happened\n{}".format(code)

return handle_error(text, InternalServerError.code)
def internal_error(e="500: Encountered a bigly error", json=False):
return handle_error(
InternalServerError.code,
InternalServerError.description,
traceback.format_exc(),
json,
)
65 changes: 59 additions & 6 deletions services/web/app/templates/error.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,68 @@
{%- extends "template.html" -%}


{% block title %}{{text}}{% endblock %}
{% block title %}{{ code }}{% endblock %}

{% block body %}

<div class="jumbotron">
<div class="container">
<h1>Error Code: {{ code }} {{ smiley }}</h1>
<h2>{{ text }}</h2>

</div>


</div>
</div>

<div class="container">
<div class="exception-error">
<p class="exception-smiley">{{ smiley }}</p>
<h1>{{ text }}</h1>
<h2>Test {{ e }}</h2>


<p>Part of being free and open is our willingness to participate in public bug tracking. We make use of GitHub to help orchestrate development of LImBuS.</p>

<p>If you would like to submit a bug report then you are required to sign up, but don't worry - it's completely free to use.</p>

<p>We understand that many of our users are going to be new to to this, and understand that it may be difficult to submit your first bug. We advise that you follow this simple step-by-step guide to get started:</p>

<ol>
<li>Visit the <a href="https://github.com/AberystwythSystemsBiology/limbus/issues" _target="blank">Issues</a> page on our GitHub.</li>
<li>Search the issue list to ensure that your report hasn't been filed already. If your report has already been filed by someone else, comment that you are experiencing the same problem - so we know that you're also affected.</li>
<li>If you believe your bug is a new one, then feel free to create a "New Issue". Type in a complete summary and description of your issue and submit it.</li>
</ol>

<h2>🤓 Stuff For Our Geeks</h2>

<p>We might ask you for this when trying to help. Just press the blue button on the bottom right and paste it into the Issue to let us help you quicker.</p>

<div class="card" style="margin-bottom: 2rem;">
<div class="card-body">
<code>
<pre id="exception">{{ traceback }}</pre>
</code>
</div>
</div>


<div class='btn btn-primary float-right' id="copy-to-clipboard"><i class="fa fa-clipboard"></i> Copy Error To Clipboard</div>

</div>

{%- endblock %}
{%- endblock %}

{% block javascript %}

<script>

$("#copy-to-clipboard").click(function(e) {
var markdown = "'''" + $("#exception").html() + "'''"
var $temp = $("<input>");
$("body").append($temp);
$temp.val(markdown).select();
document.execCommand("copy");
$temp.remove();
alert("Error Copied");
});
</script>

{% endblock %}

0 comments on commit 4f15e75

Please sign in to comment.