comparison 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
comparison
equal deleted inserted replaced
1381:a53e22400dad 1382:21d30f5d582a
1735 log.error("Can't renderAndUpdate, html was: {html}".format(html=html)) 1735 log.error("Can't renderAndUpdate, html was: {html}".format(html=html))
1736 raise e 1736 raise e
1737 1737
1738 async def renderPage(self, request, skip_parse_url=False): 1738 async def renderPage(self, request, skip_parse_url=False):
1739 """Main method to handle the workflow of a LiberviaPage""" 1739 """Main method to handle the workflow of a LiberviaPage"""
1740
1741 # template_data are the variables passed to template 1740 # template_data are the variables passed to template
1742 if not hasattr(request, "template_data"): 1741 if not hasattr(request, "template_data"):
1743 # if template_data doesn't exist, it's the beginning of the request workflow 1742 # if template_data doesn't exist, it's the beginning of the request workflow
1744 # so we fill essential data 1743 # so we fill essential data
1745 session_data = self.host.getSessionData(request, session_iface.ISATSession) 1744 session_data = self.host.getSessionData(request, session_iface.ISATSession)
1849 ) 1848 )
1850 1849
1851 self.writeData(rendered, request) 1850 self.writeData(rendered, request)
1852 1851
1853 except failure.Failure as f: 1852 except failure.Failure as f:
1854 # we have to unpack to Failure to catch the right Exception 1853 # we have to unpack the Failure to catch the right Exception
1855 raise f.value 1854 raise f.value
1856 1855
1857 except exceptions.CancelError: 1856 except exceptions.CancelError:
1858 pass 1857 pass
1859 except BridgeException as e: 1858 except BridgeException as e:
1860 if e.condition == 'not-allowed': 1859 if e.condition == 'not-allowed':
1861 log.warning("not allowed exception catched") 1860 log.warning("not allowed exception catched")
1862 self.pageError(request, C.HTTP_FORBIDDEN) 1861 self.pageError(request, C.HTTP_FORBIDDEN)
1863 elif e.condition == 'item-not-found': 1862 elif e.condition == 'item-not-found' or e.classname == 'NotFound':
1864 self.pageError(request, C.HTTP_NOT_FOUND) 1863 self.pageError(request, C.HTTP_NOT_FOUND)
1865 elif e.condition == 'remote-server-not-found': 1864 elif e.condition == 'remote-server-not-found':
1866 self.pageError(request, C.HTTP_NOT_FOUND) 1865 self.pageError(request, C.HTTP_NOT_FOUND)
1867 else: 1866 elif e.condition == 'forbidden':
1868 log.error(_("Uncatched bridge exception for HTTP request on {url}: {e}") 1867 if self.getProfile(request) is None:
1869 .format(url=request.URLPath(), e=e)) 1868 log.debug("access forbidden, we're redirecting to log-in page")
1869 self.HTTPRedirect(request, self.getPageRedirectURL(request))
1870 else:
1871 self.pageError(request, C.HTTP_FORBIDDEN)
1872 else:
1873 log.error(
1874 _("Uncatched bridge exception for HTTP request on {url}: {e}\n"
1875 "page name: {name}\npath: {path}\nURL: {full_url}\n{tb}")
1876 .format(
1877 url=self.url,
1878 e=e,
1879 name=self.name or "",
1880 path=self.root_dir,
1881 full_url=request.URLPath(),
1882 tb=traceback.format_exc(),
1883 )
1884 )
1870 try: 1885 try:
1871 self.pageError(request, C.HTTP_INTERNAL_ERROR) 1886 self.pageError(request, C.HTTP_INTERNAL_ERROR)
1872 except exceptions.CancelError: 1887 except exceptions.CancelError:
1873 pass 1888 pass
1874 except Exception as e: 1889 except Exception as e:
1875 tb = traceback.format_exc()
1876 log.error( 1890 log.error(
1877 _("Uncatched error for HTTP request on {url}:\n{tb}") 1891 _("Uncatched error for HTTP request on {url}: {e}\npage name: "
1892 "{name}\npath: {path}\nURL: {full_url}\n{tb}")
1878 .format( 1893 .format(
1879 url=request.URLPath(), 1894 url=self.url,
1880 e_name=e.__class__.__name__,
1881 e=e, 1895 e=e,
1882 tb=tb, 1896 name=self.name or "",
1897 path=self.root_dir,
1898 full_url=request.URLPath(),
1899 tb=traceback.format_exc(),
1883 ) 1900 )
1884 ) 1901 )
1885 try: 1902 try:
1886 self.pageError(request, C.HTTP_INTERNAL_ERROR) 1903 self.pageError(request, C.HTTP_INTERNAL_ERROR)
1887 except exceptions.CancelError: 1904 except exceptions.CancelError: