diff --git a/httpout/__init__.py b/httpout/__init__.py
index 7c3f082..e3b83f1 100644
--- a/httpout/__init__.py
+++ b/httpout/__init__.py
@@ -1,6 +1,6 @@
# Copyright (c) 2024 nggit
-__version__ = '0.0.64'
+__version__ = '0.0.65'
__all__ = ('HTTPOut',)
from .httpout import HTTPOut # noqa: E402
diff --git a/httpout/lib/http_response.py b/httpout/lib/http_response.py
index b6b29df..58f0822 100644
--- a/httpout/lib/http_response.py
+++ b/httpout/lib/http_response.py
@@ -28,31 +28,34 @@ async def join(self):
await self.tasks.pop()
async def handle_exception(self, exc):
- if not self.response.headers_sent():
- self.response.set_status(500, b'Internal Server Error')
- self.response.set_content_type(b'text/html; charset=utf-8')
- self.response.request.http_keepalive = False
-
- if isinstance(exc, Exception):
- if self.response.request.protocol.options['debug']:
- te = TracebackException.from_exception(exc)
- await self.response.write(
- b'
\n' % b''.join(
- html_escape(line)
- .encode() for line in te.format()
+ if self.response.request.upgraded:
+ await self.response.request.protocol.handle_exception(exc)
+ else:
+ if not self.response.headers_sent():
+ self.response.set_status(500, b'Internal Server Error')
+ self.response.set_content_type(b'text/html; charset=utf-8')
+ self.response.request.http_keepalive = False
+
+ if isinstance(exc, Exception):
+ if self.response.request.protocol.options['debug']:
+ te = TracebackException.from_exception(exc)
+ await self.response.write(
+ b'\n' % b''.join(
+ html_escape(line)
+ .encode() for line in te.format()
+ )
+ )
+ else:
+ await self.response.write(
+ f'- {exc.__class__.__name__}: '
+ f'{html_escape(str(exc))}
\n'
+ .encode()
)
- )
+ elif isinstance(exc, SystemExit):
+ if exc.code:
+ await self.response.write(str(exc.code).encode())
else:
- await self.response.write(
- f'- {exc.__class__.__name__}: '
- f'{html_escape(str(exc))}
\n'
- .encode()
- )
- elif isinstance(exc, SystemExit):
- if exc.code:
- await self.response.write(str(exc.code).encode())
- else:
- self.response.request.protocol.print_exception(exc)
+ self.response.request.protocol.print_exception(exc)
def run_coroutine(self, coro):
fut = concurrent.futures.Future()
@@ -124,16 +127,19 @@ async def _run_middleware(self):
middlewares = g.options['_middlewares']['response']
i = len(middlewares)
- while i > 0:
- i -= 1
-
- if await middlewares[i][1](globals=g,
- context=ctx,
- loop=self.loop,
- logger=self.logger,
- request=self.response.request,
- response=self.response):
- break
+ try:
+ while i > 0:
+ i -= 1
+
+ if await middlewares[i][1](globals=g,
+ context=ctx,
+ loop=self.loop,
+ logger=self.logger,
+ request=self.response.request,
+ response=self.response):
+ break
+ except Exception as exc:
+ await self.response.request.protocol.handle_exception(exc)
async def write(self, data, **kwargs):
if not self.response.headers_sent():