fix: Catch any expection and show proper error page

- Also, show title & message if it is defined in the exception class
This commit is contained in:
Suraj Shetty 2022-06-01 08:08:52 +05:30
parent f8f29e284f
commit 1431cb26d3
5 changed files with 21 additions and 19 deletions

View file

@ -26,7 +26,7 @@
{% endif %}
</div>
<div class="page_content">
<div class="page-content">
{%- block page_content -%}{%- endblock -%}
</div>

View file

@ -5,7 +5,13 @@ class ErrorPage(TemplatePage):
def __init__(self, path=None, http_status_code=None, exception=None):
path = "error"
super().__init__(path=path, http_status_code=http_status_code)
self.http_status_code = getattr(exception, "http_status_code", None) or http_status_code or 500
self.exception = exception
def can_render(self):
return True
def init_context(self):
super().init_context()
self.context.http_status_code = getattr(self.exception, "http_status_code", None) or 500
self.context.error_title = getattr(self.exception, "title", None)
self.context.error_message = getattr(self.exception, "message", None)

View file

@ -212,19 +212,13 @@ class TemplatePage(BaseTemplatePage):
def run_pymodule_method(self, method_name):
if hasattr(self.pymodule, method_name):
try:
import inspect
import inspect
method = getattr(self.pymodule, method_name)
if inspect.getfullargspec(method).args:
return method(self.context)
else:
return method()
except (frappe.PermissionError, frappe.DoesNotExistError, frappe.Redirect):
raise
except Exception:
if not frappe.flags.in_migrate:
frappe.errprint(frappe.utils.get_traceback())
method = getattr(self.pymodule, method_name)
if inspect.getfullargspec(method).args:
return method(self.context)
else:
return method()
def render_template(self):
if self.template_path.endswith("min.js"):

View file

@ -23,15 +23,15 @@
<script></script>
<div class="page-card">
<div class="page-card-head">
<span class="indicator red">{{_("Uncaught Server Exception")}}</span>
<span class="indicator red">{{ error_title }}</span>
</div>
<p>{{_("There was an error building this page")}}</p>
<p>{{ error_message }}</p>
<div>
<a href="/" class="btn btn-primary btn-sm">{{ _("Home") }}</a>
</div>
</div>
<p class="text-muted text-center small" style="margin-top: -20px;">
{{ _("Error Code: {0}").format('500') }}
{{ _("Error Code: {0}").format(http_status_code) }}
</p>
<div class="text-center mt-3">
<button class="btn btn-xs btn-link text-muted small view-error" >{{ _("Show Traceback") if not dev_server else _("Hide Traceback") }}</a>

View file

@ -1,6 +1,7 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
import frappe
from frappe import _
no_cache = 1
@ -8,7 +9,8 @@ no_cache = 1
def get_context(context):
if frappe.flags.in_migrate:
return
context.http_status_code = 500
print(frappe.get_traceback())
context.error_title = context.error_title or _("Uncaught Server Exception")
context.error_message = context.error_message or _("There was an error building this page")
return {"error": frappe.get_traceback().replace("<", "&lt;").replace(">", "&gt;")}