Skip to content

Commit

Permalink
fix exc not showing up in run
Browse files Browse the repository at this point in the history
  • Loading branch information
nggit committed Dec 2, 2024
1 parent b839047 commit abf20e5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
10 changes: 9 additions & 1 deletion examples/exc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@

from httpout import run


print('Hi')

raise ValueError

async def main():
raise ValueError


run(main())
28 changes: 1 addition & 27 deletions httpout/httpout.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import os
import sys

from traceback import TracebackException
from types import ModuleType

from awaiter import MultiThreadExecutor
Expand Down Expand Up @@ -277,32 +276,7 @@ async def _on_request(self, **server):
ctx.module_path = module_path
except BaseException as exc:
await server['response'].join()

if not response.headers_sent():
response.set_status(500, b'Internal Server Error')
response.set_content_type(b'text/html; charset=utf-8')
request.http_keepalive = False

if isinstance(exc, Exception):
if request.protocol.options['debug']:
te = TracebackException.from_exception(exc)
await response.write(
b'<ul><li>%s</li></ul>\n' % b'</li><li>'.join(
html_escape(line)
.encode() for line in te.format()
)
)
else:
await 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 response.write(str(exc.code).encode())
else:
request.protocol.print_exception(exc)
await server['response'].handle_exception(exc)
finally:
await g.executor.submit(
cleanup_modules, server['modules'], excludes
Expand Down
34 changes: 33 additions & 1 deletion httpout/lib/http_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import asyncio
import concurrent.futures

from traceback import TracebackException
from tremolo.utils import html_escape


class HTTPResponse:
def __init__(self, response):
Expand All @@ -24,6 +27,33 @@ async def join(self):
while self.tasks:
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()
)
)
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)

def run_coroutine(self, coro):
fut = concurrent.futures.Future()

Expand All @@ -35,7 +65,9 @@ async def callback():
fut.set_result(result)
except BaseException as exc:
if not fut.done():
fut.set_exception(exc)
fut.set_result(None)

await self.handle_exception(exc)

self.loop.call_soon_threadsafe(self.create_task, callback())
return fut
Expand Down

0 comments on commit abf20e5

Please sign in to comment.