Skip to content
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
3 changes: 2 additions & 1 deletion packages/asyncfast/src/asyncfast/_asyncfast.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from amgi_types import Scope
from asyncfast._asyncapi import get_asyncapi
from asyncfast._channel import Router
from asyncfast.middleware.errors import ServerErrorMiddleware

P = ParamSpec("P")
DecoratedCallable = TypeVar("DecoratedCallable", bound=Callable[..., Any])
Expand Down Expand Up @@ -123,7 +124,7 @@ def build_middleware_stack(self) -> AMGIApplication:
app = self._app
for cls, args, kwargs in self._middleware:
app = cls(app, *args, **kwargs)
return app
return ServerErrorMiddleware(app)

def add_middleware(
self,
Expand Down
19 changes: 0 additions & 19 deletions packages/asyncfast/src/asyncfast/_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

from amgi_types import AMGIReceiveCallable
from amgi_types import AMGISendCallable
from amgi_types import MessageAckEvent
from amgi_types import MessageNackEvent
from amgi_types import MessageScope
from amgi_types import MessageSendEvent
from asyncfast._utils import get_address_parameters
Expand Down Expand Up @@ -617,23 +615,6 @@ def add_channel(self, address: str, func: Callable[..., Any]) -> None:

async def __call__(
self, scope: MessageScope, receive: AMGIReceiveCallable, send: AMGISendCallable
) -> None:
try:
await self.call_channel(scope, receive, send)

message_ack_event: MessageAckEvent = {
"type": "message.ack",
}
await send(message_ack_event)
except Exception as e:
message_nack_event: MessageNackEvent = {
"type": "message.nack",
"message": str(e),
}
await send(message_nack_event)

async def call_channel(
self, scope: MessageScope, receive: AMGIReceiveCallable, send: AMGISendCallable
) -> None:
address = scope["address"]
for channel in self.channels:
Expand Down
Empty file.
32 changes: 32 additions & 0 deletions packages/asyncfast/src/asyncfast/middleware/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from amgi_types import AMGIApplication
from amgi_types import AMGIReceiveCallable
from amgi_types import AMGISendCallable
from amgi_types import MessageAckEvent
from amgi_types import MessageNackEvent
from amgi_types import Scope


class ServerErrorMiddleware:
def __init__(self, app: AMGIApplication) -> None:
self.app = app

async def __call__(
self, scope: Scope, receive: AMGIReceiveCallable, send: AMGISendCallable
) -> None:
if scope["type"] != "message":
await self.app(scope, receive, send)
return

try:
await self.app(scope, receive, send)

message_ack_event: MessageAckEvent = {
"type": "message.ack",
}
await send(message_ack_event)
except Exception as e:
message_nack_event: MessageNackEvent = {
"type": "message.nack",
"message": str(e),
}
await send(message_nack_event)