Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize handle_exception #57

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading