Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor UX improvement: Custom error for missing codec. #2508

Open
cwognum opened this issue Nov 19, 2024 · 1 comment
Open

Minor UX improvement: Custom error for missing codec. #2508

cwognum opened this issue Nov 19, 2024 · 1 comment
Labels
enhancement New features or improvements

Comments

@cwognum
Copy link

cwognum commented Nov 19, 2024

I want to preface this by saying that this would be a minor UX improvement. Not blocking at all!

Context

Zarr is extensible with custom codecs, e.g. imagecodecs. Naturally, this can lead to situations where the environment in which a Zarr archive was written is not the same as the environment in which a Zarr archive is read. This can lead to missing codecs when trying to load data from a Zarr archive.

Description

If you try to access a Zarr array that uses a codec which is not registered locally, a generic ValueError is thrown.

ValueError: codec not available: 'imagecodecs_jpeg2k'

In downstream libraries that use Zarr under the hood, this makes it harder to catch this specific error and to suggest an appropriate solution.

Instead, it would be great if a custom error is thrown that holds some additional information. For example

class InvalidCodecError(Exception):
    """Raised when an expected codec is not registered."""

    def __init__(self, codec_name: str):
        self.codec_name = codec_name
        super().__init__(f"Codec not available: '{codec_name}'")

This would enable something like:

try: 
    arr = root["array"]
except InvalidCodecError as error:
    if error.codec_name == "imagecodecs_jpeg2k":
        raise RuntimeError("This Zarr archive requires `imagecodecs`, to install it use ...") from error
@dstansby
Copy link
Contributor

dstansby commented Dec 6, 2024

👍 this sounds like a great idea - would you be able to open a PR to add this?

@dstansby dstansby added the enhancement New features or improvements label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New features or improvements
Projects
None yet
Development

No branches or pull requests

2 participants