Skip to content

Commit

Permalink
release 0.0.65 (#57)
Browse files Browse the repository at this point in the history
* optimize handle_exception

---------

Co-authored-by: nggit <[email protected]>
  • Loading branch information
nggit and nggit authored Dec 2, 2024
1 parent 2129c64 commit 5e48890
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
2 changes: 1 addition & 1 deletion httpout/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) 2024 nggit

__version__ = '0.0.64'
__version__ = '0.0.65'
__all__ = ('HTTPOut',)

from .httpout import HTTPOut # noqa: E402
72 changes: 39 additions & 33 deletions httpout/lib/http_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'<ul><li>%s</li></ul>\n' % b'</li><li>'.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'<ul><li>%s</li></ul>\n' % b'</li><li>'.join(
html_escape(line)
.encode() for line in te.format()
)
)
else:
await self.response.write(
f'<ul><li>{exc.__class__.__name__}: '
f'{html_escape(str(exc))}</li></ul>\n'
.encode()
)
)
elif isinstance(exc, SystemExit):
if exc.code:
await self.response.write(str(exc.code).encode())
else:
await self.response.write(
f'<ul><li>{exc.__class__.__name__}: '
f'{html_escape(str(exc))}</li></ul>\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()
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 5e48890

Please sign in to comment.