diff libervia/server/pages.py @ 1382:21d30f5d582a

pages: better generic errors handling: - catch `NotFound` BridgeException - if a `forbidden` BridgeException is received and the user is not logged, redirect to logging page - more debug info when an uncatched error is logged
author Goffi <goffi@goffi.org>
date Sat, 20 Feb 2021 13:54:13 +0100
parents a53e22400dad
children 3e482795630c
line wrap: on
line diff
--- a/libervia/server/pages.py	Sat Feb 20 13:51:55 2021 +0100
+++ b/libervia/server/pages.py	Sat Feb 20 13:54:13 2021 +0100
@@ -1737,7 +1737,6 @@
 
     async def renderPage(self, request, skip_parse_url=False):
         """Main method to handle the workflow of a LiberviaPage"""
-
         # template_data are the variables passed to template
         if not hasattr(request, "template_data"):
             # if template_data doesn't exist, it's the beginning of the request workflow
@@ -1851,7 +1850,7 @@
                 self.writeData(rendered, request)
 
             except failure.Failure as f:
-                # we have to unpack to Failure to catch the right Exception
+                # we have to unpack the Failure to catch the right Exception
                 raise f.value
 
         except exceptions.CancelError:
@@ -1860,26 +1859,44 @@
             if e.condition == 'not-allowed':
                 log.warning("not allowed exception catched")
                 self.pageError(request, C.HTTP_FORBIDDEN)
-            elif e.condition == 'item-not-found':
+            elif e.condition == 'item-not-found' or e.classname == 'NotFound':
                 self.pageError(request, C.HTTP_NOT_FOUND)
             elif e.condition == 'remote-server-not-found':
                 self.pageError(request, C.HTTP_NOT_FOUND)
+            elif e.condition == 'forbidden':
+                if self.getProfile(request) is None:
+                    log.debug("access forbidden, we're redirecting to log-in page")
+                    self.HTTPRedirect(request, self.getPageRedirectURL(request))
+                else:
+                    self.pageError(request, C.HTTP_FORBIDDEN)
             else:
-                log.error(_("Uncatched bridge exception for HTTP request on {url}: {e}")
-                    .format(url=request.URLPath(), e=e))
+                log.error(
+                    _("Uncatched bridge exception for HTTP request on {url}: {e}\n"
+                      "page name: {name}\npath: {path}\nURL: {full_url}\n{tb}")
+                    .format(
+                        url=self.url,
+                        e=e,
+                        name=self.name or "",
+                        path=self.root_dir,
+                        full_url=request.URLPath(),
+                        tb=traceback.format_exc(),
+                    )
+                )
                 try:
                     self.pageError(request, C.HTTP_INTERNAL_ERROR)
                 except exceptions.CancelError:
                     pass
         except Exception as e:
-            tb = traceback.format_exc()
             log.error(
-                _("Uncatched error for HTTP request on {url}:\n{tb}")
+                _("Uncatched error for HTTP request on {url}: {e}\npage name: "
+                  "{name}\npath: {path}\nURL: {full_url}\n{tb}")
                 .format(
-                    url=request.URLPath(),
-                    e_name=e.__class__.__name__,
+                    url=self.url,
                     e=e,
-                    tb=tb,
+                    name=self.name or "",
+                    path=self.root_dir,
+                    full_url=request.URLPath(),
+                    tb=traceback.format_exc(),
                 )
             )
             try: