Skip to content

Commit

Permalink
Fix status serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
pelletier committed Jan 30, 2024
1 parent c5efaf7 commit feb7001
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/dispatch/coroutine.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,10 @@ def error(cls, error: Error) -> Output:
"""Terminally exit the coroutine with the provided error."""
return Output(
coroutine_pb2.ExecuteResponse(
status=error.status._proto,
exit=coroutine_pb2.Exit(
result=coroutine_pb2.Result(error=error._as_proto())
)
),
)
)

Expand Down Expand Up @@ -298,6 +299,7 @@ def __init__(self, status: Status, type: str | None, message: str | None):

self.type = type
self.message = message
self.status = status

@classmethod
def from_exception(cls, ex: Exception, status: Status | None = None) -> Error:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def mycoro(input: Input) -> Output:
resp = self.execute(mycoro)
self.assertEqual("ZeroDivisionError", resp.exit.result.error.type)
self.assertEqual("division by zero", resp.exit.result.error.message)
self.assertEqual(Status.TEMPORARY_ERROR, resp.status)

def test_coroutine_unexpected_exception(self):
@self.app.dispatch_coroutine()
Expand All @@ -279,3 +280,14 @@ def mycoro(input: Input) -> Output:
resp = self.execute(mycoro)
self.assertEqual("ZeroDivisionError", resp.exit.result.error.type)
self.assertEqual("division by zero", resp.exit.result.error.message)
self.assertEqual(Status.TEMPORARY_ERROR, resp.status)

def test_specific_status(self):
@self.app.dispatch_coroutine()
def mycoro(input: Input) -> Output:
return Output.error(Error(Status.THROTTLED, "foo", "bar"))

resp = self.execute(mycoro)
self.assertEqual("foo", resp.exit.result.error.type)
self.assertEqual("bar", resp.exit.result.error.message)
self.assertEqual(Status.THROTTLED, resp.status)

0 comments on commit feb7001

Please sign in to comment.