Skip to content

Commit

Permalink
Merge pull request #149 from springload/feature/500
Browse files Browse the repository at this point in the history
Custom 500 error handling with context for messages and email excepti…
  • Loading branch information
kesara authored Nov 8, 2021
2 parents 505d1e0 + 89f87ec commit e3bd076
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 63 deletions.
61 changes: 47 additions & 14 deletions ietf/forms/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
from logging import Logger

from django.contrib import messages
from django.views.defaults import server_error
from modelcluster.fields import ParentalKey
from wagtail.admin.edit_handlers import (
FieldPanel, InlinePanel, MultiFieldPanel
)
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel
from wagtail.contrib.forms.models import AbstractEmailForm, AbstractFormField
from wagtail.core.fields import RichTextField

from ietf.views import server_error

logger = Logger(__name__)


class EmailException(Exception):
def __init__(self, message="Error sending email", code=500, params=None):
super().__init__(message, code, params)
self.message = message
self.code = code


class FormField(AbstractFormField):
page = ParentalKey('FormPage', related_name='form_fields')
page = ParentalKey("FormPage", related_name="form_fields")

@classmethod
def _migrate_legacy_clean_name(cls):
Expand All @@ -18,14 +31,34 @@ class FormPage(AbstractEmailForm):
intro = RichTextField(blank=True)
thank_you_text = RichTextField(blank=True)

def send_mail(self, form):
try:
super().send_mail(form)
except Exception as ex:
logger.error("Failed to send email with exception: {}".format(ex))
raise EmailException

def serve(self, request, *args, **kwargs):
try:
return super().serve(request, *args, **kwargs)
except EmailException as Ex:
messages.add_message(
request, messages.ERROR, message="Failed to send email"
)
raise EmailException


FormPage.content_panels = [
FieldPanel('title', classname="full title"),
FieldPanel('intro', classname="full"),
InlinePanel('form_fields', label="Form fields"),
FieldPanel('thank_you_text', classname="full"),
MultiFieldPanel([
FieldPanel('to_address', classname="full"),
FieldPanel('from_address', classname="full"),
FieldPanel('subject', classname="full"),
], "Email")
FieldPanel("title", classname="full title"),
FieldPanel("intro", classname="full"),
InlinePanel("form_fields", label="Form fields"),
FieldPanel("thank_you_text", classname="full"),
MultiFieldPanel(
[
FieldPanel("to_address", classname="full"),
FieldPanel("from_address", classname="full"),
FieldPanel("subject", classname="full"),
],
"Email",
),
]
31 changes: 15 additions & 16 deletions ietf/forms/tests.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
from django.test import TestCase
from wagtail.core.models import Page, Site

from .models import FormPage
from ..home.models import HomePage
from .models import FormPage

from wagtail.core.models import Page, Site

class FormPageTests(TestCase):

def test_form_page(self):

root = Page.get_first_root_node()

home = HomePage(
slug = 'homepageslug',
title = 'home page title',
heading = 'home page heading',
introduction = 'home page introduction',
request_for_comments_section_body = 'rfc section body',
working_groups_section_body = 'wg section body',
slug="homepageslug",
title="home page title",
heading="home page heading",
introduction="home page introduction",
request_for_comments_section_body="rfc section body",
working_groups_section_body="wg section body",
)

root.add_child(instance=home)

Site.objects.all().delete()

Site.objects.create(
hostname='localhost',
root_page = home,
hostname="localhost",
root_page=home,
is_default_site=True,
site_name='testingsitename',
site_name="testingsitename",
)

form = FormPage(
slug = 'form',
title = 'form title',
intro = 'form introduction',
slug="form",
title="form title",
intro="form introduction",
)
home.add_child(instance = form)
home.add_child(instance=form)

r = self.client.get(path=form.url)
self.assertEqual(r.status_code, 200)
Expand Down
75 changes: 58 additions & 17 deletions ietf/templates/500.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,58 @@
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Internal server error</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<h1>Internal server error</h1>

<h2>Sorry, there seems to be an error. Please try again soon.</h2>
</body>
</html>
{% extends "base.html" %}

{% block body_class %}template-500{% endblock %}

{% block title %}- 500 - Internal server error{% endblock %}

{% block main_content %}
<main id="content">
<div class="bg-white border-bottom">
<div class="container pb-3">

<nav aria-label="breadcrumbs">
<ol class="breadcrumb bg-transparent pt-4 mb-0">
<li class="breadcrumb-item h5">
<a class="text-dark" href="/" aria-label="Home">
<span class="icon ion-android-home" aria-hidden="true"></span>
</a>
</li>
</ol>
</nav>


<div class="u-max-text-width">
<h1>HTTP Status Code: 500 Internal Server Error</h1>

<p>Ooops, something went wrong. We are looking into it, please try again soon.</p>

{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
</div>

<div class="row mb-4 mt-4">
<div class="col-md-8 col-lg-6">
<form class="input-group" action="{% url 'search' %}" method="GET" name="search">
<input id="search-box2" class="form-control" type="text" placeholder="Search" aria-label="Search website" name="query" />
<button class="btn btn-primary" type="submit" aria-label="Submit website search">Go</button>
</form>
</div>
</div>

</div>
</div>

<div class="container py-4 py-lg-5">
<div class="u-max-text-width">
<h2>Report problems</h2>
<p>If the matter is urgent, please email <a href="mailto:support@ietf.org">support@ietf.org</a> .</p>
</div>
</div>

</main>

{% endblock %}
32 changes: 16 additions & 16 deletions ietf/urls.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
from django.conf.urls import include, url
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin

from wagtail.admin import urls as wagtailadmin_urls
from wagtail.documents import urls as wagtaildocs_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls

from ietf.bibliography import urls as bibliography_urls
from ietf.blog.feeds import BlogFeed
from ietf.search.views import search
from ietf.snippets import urls as snippet_urls
from ietf.blog.feeds import BlogFeed

urlpatterns = [
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^bibliography/', include(bibliography_urls)),
url(r'^django-admin/', admin.site.urls),
url(r'^blog/feed/$', BlogFeed(), name='blog_feed'),
handler500 = "ietf.views.server_error"

url(r'^admin/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),

url(r'^search/$', search, name='search'),
urlpatterns = [
url(r"^admin/doc/", include("django.contrib.admindocs.urls")),
url(r"^bibliography/", include(bibliography_urls)),
url(r"^django-admin/", admin.site.urls),
url(r"^blog/feed/$", BlogFeed(), name="blog_feed"),
url(r"^admin/", include(wagtailadmin_urls)),
url(r"^documents/", include(wagtaildocs_urls)),
url(r"^search/$", search, name="search"),
]


Expand All @@ -35,12 +35,12 @@

# Add views for testing 404 and 500 templates
urlpatterns += [
url(r'^test404/$', TemplateView.as_view(template_name='404.html')),
url(r'^test500/$', TemplateView.as_view(template_name='500.html')),
url(r"^test404/$", TemplateView.as_view(template_name="404.html")),
url(r"^test500/$", TemplateView.as_view(template_name="500.html")),
]


urlpatterns += [
url(r'^misc/', include(snippet_urls)),
url(r'', include(wagtail_urls)),
url(r"^misc/", include(snippet_urls)),
url(r"", include(wagtail_urls)),
]
7 changes: 7 additions & 0 deletions ietf/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.http import HttpResponseServerError
from django.template.loader import get_template


def server_error(request, template_name="500.html"):
t = get_template(template_name)
return HttpResponseServerError(t.render(locals(), request))

0 comments on commit e3bd076

Please sign in to comment.