GitHub user gidhubuser255 edited a discussion: Graceful Error Handling (2 
feature requests)

### (1) Error Origin
My current understanding of the GracefulErrorAdapter is that short circuiting 
exception raising nodes and their downstream nodes is tied solely to the type 
of exception, and not where the exception originated.
Take the following example:
```
from hamilton.lifecycle.default import GracefulErrorAdapter, 
accept_error_sentinels

def A(B: int, C: int) -> int:
    return B + C

@accept_error_sentinels
def B(D: int) -> int:
    if D is None:
        return 999
    else:
        return D

def C() -> int:
    raise ValueError()

def D() -> int:
    raise ValueError()


dr = (
    driver.Builder()
    .with_modules(__import__('__main__'))
    .with_adapters(
        GracefulErrorAdapter(
            error_to_catch=ValueError,
            sentinel_value=None,
        )
    )
    .build()
)

dr.execute('A')
```
In this case I'd only want errors to be handled gracefully in node B 
originating in node D or any upstream of D. As that's the only place that has 
the error handling logic. However, errors from C will cause short circuiting 
all the way to the root since C errors have no error handling logic downstream 
of them. This makes for unexpected short circuited results, which would be 
better handled as an "unhandled" error, raising the exception as expected.

**Request:** Would it be possible to add a mode to GracefulErrorAdapter that 
only short circuits up to a function/node that is decorated with 
`@accept_error_sentinels`, and if such a node does not exist downstream of the 
erroring node it will raise the exception up?

### (2) Error Sentinel Type
To do more thorough error handling it would be useful to get the actual 
exception object as the sentinel value, this would allow you to do something 
like this:

```
@accept_error_sentinels
def B(D: int) -> int:
    match D:
        case FileNotFoundError():
            ...
        case ZeroDivisionError():
            ...
        case Exception():
            ...
        case _:
            return D
```
I see that I can do something like this:
```
GracefulErrorAdapter(
    error_to_catch=ValueError,
    sentinel_value=ValueError,
)
```
But I can only have 1 adapter here, so I can't have all exception types handled 
this way, and it would not contain the original exception information either, 
which could be useful for logging or parsing the exception message.

**Request:** Would it be possible to provide something like 
`GracefulErrorAdapter(exception_as_sentinel_value=True)`?


GitHub link: https://github.com/apache/hamilton/discussions/1399

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: 
[email protected]

Reply via email to