Skip to content

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

Closed
@cwognum

Description

@cwognum

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew features or improvements

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions