comparison src/server/server.py @ 937:00236973e138

server (pages): an HTTP internal error is raised if an error is uncatched during page workflow
author Goffi <goffi@goffi.org>
date Thu, 27 Apr 2017 01:13:50 +0200
parents 78692d47340d
children 67a59552f3e3
comparison
equal deleted inserted replaced
936:78692d47340d 937:00236973e138
1480 if parse_url is not None and not callable(parse_url): 1480 if parse_url is not None and not callable(parse_url):
1481 log.error(_(u"parse_url must be a callable")) 1481 log.error(_(u"parse_url must be a callable"))
1482 1482
1483 @classmethod 1483 @classmethod
1484 def importPages(cls, host, parent=None, path=None): 1484 def importPages(cls, host, parent=None, path=None):
1485 """Recursively import Libervia pages"""
1485 if path is None: 1486 if path is None:
1486 path = [] 1487 path = []
1487 if parent is None: 1488 if parent is None:
1488 root_dir = os.path.join(os.path.dirname(libervia.__file__), C.PAGES_DIR) 1489 root_dir = os.path.join(os.path.dirname(libervia.__file__), C.PAGES_DIR)
1489 parent = host 1490 parent = host
1640 1641
1641 self.writeData(rendered, request) 1642 self.writeData(rendered, request)
1642 raise failure.Failure(exceptions.CancelError(u'error page is used')) 1643 raise failure.Failure(exceptions.CancelError(u'error page is used'))
1643 1644
1644 def writeData(self, data, request): 1645 def writeData(self, data, request):
1646 """write data to transport and finish the request"""
1645 if data is None: 1647 if data is None:
1646 self.pageError(request) 1648 self.pageError(request)
1647 request.write(data.encode('utf-8')) 1649 request.write(data.encode('utf-8'))
1648 request.finish() 1650 request.finish()
1649 1651
1679 1681
1680 def _renderEb(self, failure_, request): 1682 def _renderEb(self, failure_, request):
1681 """don't raise error on CancelError""" 1683 """don't raise error on CancelError"""
1682 failure_.trap(exceptions.CancelError) 1684 failure_.trap(exceptions.CancelError)
1683 1685
1686 def _internalError(self, failure_, request):
1687 """called if an error is not catched"""
1688 log.error(_(u"Uncatched error for HTTP request on {url}: {msg}").format(
1689 url = request.URLPath(),
1690 msg = failure_))
1691 self.pageError(request, C.HTTP_INTERNAL_ERROR)
1692
1684 def _on_data_post(self, dummy, request): 1693 def _on_data_post(self, dummy, request):
1685 return defer.maybeDeferred(self.on_data_post, self, request) 1694 return defer.maybeDeferred(self.on_data_post, self, request)
1686 1695
1687 def getPostedData(self, request, keys, multiple=False): 1696 def getPostedData(self, request, keys, multiple=False):
1688 """get data from a POST request and decode it 1697 """get data from a POST request and decode it
1748 self.pageError(request, C.HTTP_UNAUTHORIZED) 1757 self.pageError(request, C.HTTP_UNAUTHORIZED)
1749 1758
1750 return data 1759 return data
1751 1760
1752 def renderPage(self, request, skip_parse_url=False): 1761 def renderPage(self, request, skip_parse_url=False):
1762 """Main method to handle the workflow of a LiberviaPage"""
1753 # template_data are the variables passed to template 1763 # template_data are the variables passed to template
1754 if not hasattr(request, 'template_data'): 1764 if not hasattr(request, 'template_data'):
1755 request.template_data = {} 1765 request.template_data = {}
1756 1766
1757 # XXX: here is the code which need to be executed once 1767 # XXX: here is the code which need to be executed once
1794 elif self.render_method: 1804 elif self.render_method:
1795 d.addCallback(self._render_method, request) 1805 d.addCallback(self._render_method, request)
1796 1806
1797 d.addCallback(self.writeData, request) 1807 d.addCallback(self.writeData, request)
1798 d.addErrback(self._renderEb, request) 1808 d.addErrback(self._renderEb, request)
1809 d.addErrback(self._internalError, request)
1799 d.callback(self) 1810 d.callback(self)
1800 return server.NOT_DONE_YET 1811 return server.NOT_DONE_YET
1801 1812
1802 def render_GET(self, request): 1813 def render_GET(self, request):
1803 return self.renderPage(request) 1814 return self.renderPage(request)