Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ Improvements
Import errors caused by optional dependencies (ZFPY, MsgPack, CRC32C, and PCodec)
are still silently caught.
By :user:`David Stansby <dstansby>`, :issue:`550`.
* Raise a custom `UnknownCodecError` when trying to retrieve an unavailable codec.
By :user:`Cas Wognum <cwognum>`.

.. _release_0.14.1:

Expand Down
26 changes: 26 additions & 0 deletions numcodecs/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
This module defines custom exceptions that are raised in the `numcodecs` codebase.
"""


class UnknownCodecError(ValueError):
"""
An exception that is raised when trying to receive a codec that has not been registered.

Parameters
----------
codec_id : str
Codec identifier.

Examples
----------
>>> import numcodecs
>>> numcodecs.get_codec({"codec_id": "unknown"})
Traceback (most recent call last):
...
UnknownCodecError: codec not available: 'unknown'
"""

def __init__(self, codec_id: str):
self.codec_id = codec_id
super().__init__(f"codec not available: '{codec_id}'")
3 changes: 2 additions & 1 deletion numcodecs/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from importlib.metadata import EntryPoints, entry_points

from numcodecs.abc import Codec
from numcodecs.errors import UnknownCodecError

logger = logging.getLogger("numcodecs")
codec_registry: dict[str, Codec] = {}
Expand Down Expand Up @@ -50,7 +51,7 @@ def get_codec(config):
register_codec(cls, codec_id=codec_id)
if cls:
return cls.from_config(config)
raise ValueError(f'codec not available: {codec_id!r}')
raise UnknownCodecError(f"{codec_id!r}")


def register_codec(cls, codec_id=None):
Expand Down
3 changes: 2 additions & 1 deletion numcodecs/tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import pytest

import numcodecs
from numcodecs.errors import UnknownCodecError
from numcodecs.registry import get_codec


def test_registry_errors():
with pytest.raises(ValueError):
with pytest.raises(UnknownCodecError, match='foo'):
get_codec({'id': 'foo'})


Expand Down
Loading