Repository: dask/dask
File: dask/backends.py (Python)

Code before:
        )

    def register_inplace(
        self,
        backend: str,
        name: str | None = None,
    ) -> Callable[
        [Callable[BackendFuncParams, BackendFuncReturn]],
        Callable[BackendFuncParams, BackendFuncReturn],
    ]:
        """Register dispatchable function"""

        def decorator(
            fn: Callable[BackendFuncParams, BackendFuncReturn]
        ) -> Callable[BackendFuncParams, BackendFuncReturn]:
            dispatch_name = name or fn.__name__
            dispatcher = self.dispatch(backend)
            dispatcher.__setattr__(dispatch_name, fn)

            @wraps(fn)
            def wrapper(*args, **kwargs):
                func = getattr(self, dispatch_name)
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    if len(inspect.signature(e.__init__).parameters) <= 1:
                        # BotoCoreError doesn't have a message parameter
                        raise

                    raise type(e)(
                        f"An error occurred while calling the {funcname(func)} "
                        f"method registered to the {self.backend} backend.\n"
                        f"Original Message: {e}"
                    ) from e

            wrapper.__name__ = dispatch_name
            return wrapper

        return decorator

    def __getattr__(self, item: str):
        """
        Return the appropriate attribute for the current backend
        """
        backend = self.dispatch(self.backend)
        return getattr(backend, item)


Review comment (suggestion):
This feels very lossy, how about something like this instead?

```python
                except Exception as e:
                    try:
                        exc = type(e)(
                            f"An error occurred while calling the {funcname(func)} "
                            f"method registered to the {self.backend} backend.\n"
                            f"Original Message: {e}"
                        )
                    except TypeError:
                        raise e
                    else:
                        raise exc from e
```

Code after:

    def register_inplace(
        self,
        backend: str,
        name: str | None = None,
    ) -> Callable[
        [Callable[BackendFuncParams, BackendFuncReturn]],
        Callable[BackendFuncParams, BackendFuncReturn],
    ]:
        """Register dispatchable function"""

        def decorator(
            fn: Callable[BackendFuncParams, BackendFuncReturn]
        ) -> Callable[BackendFuncParams, BackendFuncReturn]:
            dispatch_name = name or fn.__name__
            dispatcher = self.dispatch(backend)
            dispatcher.__setattr__(dispatch_name, fn)

            @wraps(fn)
            def wrapper(*args, **kwargs):
                func = getattr(self, dispatch_name)
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    try:
                        exc = type(e)(
                            f"An error occurred while calling the {funcname(func)} "
                            f"method registered to the {self.backend} backend.\n"
                            f"Original Message: {e}"
                        )
                    except TypeError:
                        raise e
                    else:
                        raise exc from e

            wrapper.__name__ = dispatch_name
            return wrapper

        return decorator

    def __getattr__(self, item: str):
        """
        Return the appropriate attribute for the current backend
        """
        backend = self.dispatch(self.backend)
        return getattr(backend, item)