dispatchrun / dispatch-py

Python package to develop applications with Dispatch.
https://pypi.org/project/dispatch-py/
Apache License 2.0
56 stars 3 forks source link

Propagate error types when awaiting on durable coroutines #66

Closed achille-roussel closed 9 months ago

achille-roussel commented 9 months ago

Take this code snippet as example to reproduce the issue:

from fastapi import FastAPI
from dispatch.fastapi import Dispatch
import requests

app = FastAPI()
dispatch = Dispatch(app)

@dispatch.coroutine()
async def get(something):
    raise ValueError("This is an error")

@dispatch.coroutine()
async def publish():
    return await get.call('this')

@app.get('/')
def root():
    publish.dispatch()
    return "OK"

While the exception thrown by the call to get is propagated, it appears to be turned into a RuntimeError (originally a ValueError).

Preserving the error type is important because exception handling in Python is based on types, not values (e.g., with try/except).

This is the exception that gets raised when executing this code:

unexpected exception occurred during coroutine scheduling
Traceback (most recent call last):
  File "/Users/achilleroussel/go/src/github.com/stealthrocket/dispatch-sdk-python/src/dispatch/scheduler.py", line 162, in run
    return self._run(input)
           ^^^^^^^^^^^^^^^^
  File "/Users/achilleroussel/go/src/github.com/stealthrocket/dispatch-sdk-python/src/dispatch/scheduler.py", line 251, in _run
    coroutine_yield = coroutine.run()
                      ^^^^^^^^^^^^^^^
  File "/Users/achilleroussel/go/src/github.com/stealthrocket/dispatch-sdk-python/src/dispatch/scheduler.py", line 116, in run
    return self.coroutine.send(None)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/achilleroussel/go/src/github.com/stealthrocket/dispatch-sdk-python/src/dispatch/experimental/durable/function.py", line 218, in send
    return self.coroutine.send(send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/achilleroussel/go/src/github.com/stealthrocket/dispatch-sdk-python/main.py", line 10, in get
    raise ValueError("This is an error")
ValueError: This is an error
unexpected exception occurred during coroutine scheduling
Traceback (most recent call last):
  File "/Users/achilleroussel/go/src/github.com/stealthrocket/dispatch-sdk-python/src/dispatch/scheduler.py", line 162, in run
    return self._run(input)
           ^^^^^^^^^^^^^^^^
  File "/Users/achilleroussel/go/src/github.com/stealthrocket/dispatch-sdk-python/src/dispatch/scheduler.py", line 251, in _run
    coroutine_yield = coroutine.run()
                      ^^^^^^^^^^^^^^^
  File "/Users/achilleroussel/go/src/github.com/stealthrocket/dispatch-sdk-python/src/dispatch/scheduler.py", line 120, in run
    return self.coroutine.throw(error)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/achilleroussel/go/src/github.com/stealthrocket/dispatch-sdk-python/src/dispatch/experimental/durable/function.py", line 221, in throw
    return self.coroutine.throw(typ, val, tb)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/achilleroussel/go/src/github.com/stealthrocket/dispatch-sdk-python/main.py", line 15, in publish
    r1 = await get.call('this')
         ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/achilleroussel/go/src/github.com/stealthrocket/dispatch-sdk-python/src/dispatch/experimental/durable/function.py", line 285, in throw
    return self.generator.throw(typ, val, tb)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/achilleroussel/go/src/github.com/stealthrocket/dispatch-sdk-python/src/dispatch/function.py", line 92, in _call_async
    return await dispatch.coroutine.call(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/types.py", line 220, in throw
    return self.__wrapped.throw(tp, *rest)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/achilleroussel/go/src/github.com/stealthrocket/dispatch-sdk-python/src/dispatch/experimental/durable/function.py", line 285, in throw
    return self.generator.throw(typ, val, tb)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/achilleroussel/go/src/github.com/stealthrocket/dispatch-sdk-python/src/dispatch/coroutine.py", line 14, in call
    return (yield call)
            ^^^^^^^^^^
RuntimeError: This is an error
chriso commented 9 months ago

See: https://github.com/stealthrocket/dispatch-sdk-python/blob/b7889f3a1840fa56619049f25a6ee93d4a04dcc9/src/dispatch/proto.py#L318-L320