fal-ai / fal

⚡ Fastest way to serve open source ML models to millions
https://fal.ai/docs
Apache License 2.0
509 stars 44 forks source link

feat(api): improve handling of user exceptions #143

Closed efiop closed 5 months ago

efiop commented 5 months ago

This patch adds support for chained exceptions and utilizes those to provide a more seamless experience for both cli and api users by providing them with original exceptions and their tracebacks.

This likely makes isolate's stringized_traceback obsolete.

For example:

import fal

class FooException(Exception):
    pass

class BarException(Exception):
    pass

def bar():
    raise BarException("bar")

def foo():
    try:
        bar()
    except Exception as exc:
        raise FooException("foo") from exc

@fal.function()
def mytest():
    foo()
    print(str(2*2))

if __name__ == "__main__":
    mytest()

running it through API we'll get (note that not only traceback is present but also __cause__ is correctly set):

Traceback (most recent call last):
  File "/Users/efiop/git/efiop/ui-comp/fal/exc_chain.py", line 15, in foo
    bar()
  ^^^^^^^^
  File "/Users/efiop/git/efiop/ui-comp/fal/exc_chain.py", line 11, in bar
    raise BarException("bar")
      ^^^^^^^^^^^^^^^^^
BarException: bar

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/efiop/git/efiop/ui-comp/fal/exc_chain.py", line 25, in <module>
    mytest()
  File "/Users/efiop/git/efiop/fal/projects/fal/src/fal/api.py", line 978, in __call__
    raise cause
  File "/Users/efiop/git/efiop/ui-comp/fal/exc_chain.py", line 21, in mytest
    foo()
      ^^^^
  File "/Users/efiop/git/efiop/ui-comp/fal/exc_chain.py", line 17, in foo
    raise FooException("foo") from exc
  ^^^^^^^^^^^^^^^^^
FooException: foo

and with fal fn run (no --debug) we'll get

Screenshot 2024-03-27 at 17 25 11

NOTE: we now support chained exceptions, but their classes are recreated because we dill everything by value and not by reference. This is better than before, but not perfect yet. See #142

linear[bot] commented 5 months ago

FEA-2184 ui: reduce traceback to user code for user exceptions

efiop commented 5 months ago

TODO: