implement top-level error handling

This commit is contained in:
Jakob Ketterl 2021-04-27 18:23:59 +02:00
parent 118335b2b6
commit c6e01eed1a
2 changed files with 38 additions and 3 deletions

View File

@ -12,6 +12,7 @@
${header}
<div class="container">
${breadcrumb}
${error}
<div class="row">
<h1 class="col-12">${title}</h1>
</div>

View File

@ -6,6 +6,10 @@ from owrx.breadcrumb import Breadcrumb, BreadcrumbItem, BreadcrumbMixin
from abc import ABCMeta, abstractmethod
from urllib.parse import parse_qs
import logging
logger = logging.getLogger(__name__)
class Section(object):
def __init__(self, title, *inputs):
@ -55,6 +59,7 @@ class SettingsFormController(AuthorizationMixin, BreadcrumbMixin, WebpageControl
def __init__(self, handler, request, options):
super().__init__(handler, request, options)
self.errors = {}
self.globalError = None
@abstractmethod
def getSections(self):
@ -98,6 +103,7 @@ class SettingsFormController(AuthorizationMixin, BreadcrumbMixin, WebpageControl
variables["content"] = self.render_sections()
variables["title"] = self.getTitle()
variables["modal"] = self.buildModal()
variables["error"] = self.renderGlobalError()
return variables
def parseFormData(self):
@ -122,14 +128,26 @@ class SettingsFormController(AuthorizationMixin, BreadcrumbMixin, WebpageControl
return result
def processFormData(self):
data, errors = self.parseFormData()
data = None
errors = None
try:
data, errors = self.parseFormData()
except Exception as e:
logger.exception("Error while parsing form data")
self.globalError = str(e)
return self.indexAction()
if errors:
self.errors = self._mergeErrors(errors)
self.indexAction()
else:
return self.indexAction()
try:
self.processData(data)
self.store()
self.send_redirect(self.getSuccessfulRedirect())
except Exception as e:
logger.exception("Error while processing form data")
self.globalError = str(e)
return self.indexAction()
def processData(self, data):
config = self.getData()
@ -146,6 +164,22 @@ class SettingsFormController(AuthorizationMixin, BreadcrumbMixin, WebpageControl
def buildModal(self):
return ""
def renderGlobalError(self):
if self.globalError is None:
return ""
return """
<div class="card text-white bg-danger">
<div class="card-header">Error</div>
<div class="card-body">
<div>Your settings could not be saved due to an error:</div>
<div>{error}</div>
</div>
</div>
""".format(
error=self.globalError
)
class SettingsBreadcrumb(Breadcrumb):
def __init__(self):