| 848 | return tob(template(ERROR_PAGE_TEMPLATE, e=res)) |
| 849 | |
| 850 | def _handle(self, environ): |
| 851 | path = environ['bottle.raw_path'] = environ['PATH_INFO'] |
| 852 | if py3k: |
| 853 | try: |
| 854 | environ['PATH_INFO'] = path.encode('latin1').decode('utf8') |
| 855 | except UnicodeError: |
| 856 | return HTTPError(400, 'Invalid path string. Expected UTF-8') |
| 857 | |
| 858 | try: |
| 859 | environ['bottle.app'] = self |
| 860 | request.bind(environ) |
| 861 | response.bind() |
| 862 | try: |
| 863 | self.trigger_hook('before_request') |
| 864 | route, args = self.router.match(environ) |
| 865 | environ['route.handle'] = route |
| 866 | environ['bottle.route'] = route |
| 867 | environ['route.url_args'] = args |
| 868 | return route.call(**args) |
| 869 | finally: |
| 870 | self.trigger_hook('after_request') |
| 871 | |
| 872 | except HTTPResponse: |
| 873 | return _e() |
| 874 | except RouteReset: |
| 875 | route.reset() |
| 876 | return self._handle(environ) |
| 877 | except (KeyboardInterrupt, SystemExit, MemoryError): |
| 878 | raise |
| 879 | except Exception: |
| 880 | if not self.catchall: raise |
| 881 | stacktrace = format_exc() |
| 882 | environ['wsgi.errors'].write(stacktrace) |
| 883 | return HTTPError(500, "Internal Server Error", _e(), stacktrace) |
| 884 | |
| 885 | def _cast(self, out, peek=None): |
| 886 | """ Try to convert the parameter into something WSGI compatible and set |