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} ${header}
<div class="container"> <div class="container">
${breadcrumb} ${breadcrumb}
${error}
<div class="row"> <div class="row">
<h1 class="col-12">${title}</h1> <h1 class="col-12">${title}</h1>
</div> </div>

View File

@ -6,6 +6,10 @@ from owrx.breadcrumb import Breadcrumb, BreadcrumbItem, BreadcrumbMixin
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from urllib.parse import parse_qs from urllib.parse import parse_qs
import logging
logger = logging.getLogger(__name__)
class Section(object): class Section(object):
def __init__(self, title, *inputs): def __init__(self, title, *inputs):
@ -55,6 +59,7 @@ class SettingsFormController(AuthorizationMixin, BreadcrumbMixin, WebpageControl
def __init__(self, handler, request, options): def __init__(self, handler, request, options):
super().__init__(handler, request, options) super().__init__(handler, request, options)
self.errors = {} self.errors = {}
self.globalError = None
@abstractmethod @abstractmethod
def getSections(self): def getSections(self):
@ -98,6 +103,7 @@ class SettingsFormController(AuthorizationMixin, BreadcrumbMixin, WebpageControl
variables["content"] = self.render_sections() variables["content"] = self.render_sections()
variables["title"] = self.getTitle() variables["title"] = self.getTitle()
variables["modal"] = self.buildModal() variables["modal"] = self.buildModal()
variables["error"] = self.renderGlobalError()
return variables return variables
def parseFormData(self): def parseFormData(self):
@ -122,14 +128,26 @@ class SettingsFormController(AuthorizationMixin, BreadcrumbMixin, WebpageControl
return result return result
def processFormData(self): 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: if errors:
self.errors = self._mergeErrors(errors) self.errors = self._mergeErrors(errors)
self.indexAction() return self.indexAction()
else: try:
self.processData(data) self.processData(data)
self.store() self.store()
self.send_redirect(self.getSuccessfulRedirect()) 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): def processData(self, data):
config = self.getData() config = self.getData()
@ -146,6 +164,22 @@ class SettingsFormController(AuthorizationMixin, BreadcrumbMixin, WebpageControl
def buildModal(self): def buildModal(self):
return "" 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): class SettingsBreadcrumb(Breadcrumb):
def __init__(self): def __init__(self):