Exceptions#

The ploomber_core.exceptions module implements some exceptions that customize the error message and add our community link at the end; the objective is to incentivize users to reach out to us when they have problems so we can assist them.

PloomberValueError#

Added in version 0.1: Ensure you pin this version in the setup.py file (ploomber-core>=0.1.*)

A subclass of the built-in ValueError, use it when the value is unexpected:

from ploomber_core import exceptions


def call_person(name="Bob"):
    if name not in {"Bob", "Alice"}:
        raise exceptions.PloomberValueError("name must be 'Bob' or 'Alice'")

    print(f"Calling {name}")
call_person()
Calling Bob
call_person("Alice")
Calling Alice
call_person("John")
---------------------------------------------------------------------------
PloomberValueError                        Traceback (most recent call last)
Cell In[4], line 1
----> 1 call_person("John")

Cell In[1], line 6, in call_person(name)
      4 def call_person(name="Bob"):
      5     if name not in {"Bob", "Alice"}:
----> 6         raise exceptions.PloomberValueError("name must be 'Bob' or 'Alice'")
      8     print(f"Calling {name}")

PloomberValueError: name must be 'Bob' or 'Alice'. 
If you need help solving this issue, send us a message: https://ploomber.io/community

PloomberTypeError#

Added in version 0.1: Ensure you pin this version in the setup.py file (ploomber-core>=0.1.*)

A subclass of the built-in TypeError, use it when the type is unexpected:

from ploomber_core import exceptions  # noqa


def add_one(a):
    if not isinstance(a, (int, float)):
        raise exceptions.PloomberTypeError("a must be int or float")

    return a + 1
add_one(1)
2
add_one(41.0)
42.0
add_one("hello")
---------------------------------------------------------------------------
PloomberTypeError                         Traceback (most recent call last)
Cell In[8], line 1
----> 1 add_one("hello")

Cell In[5], line 6, in add_one(a)
      4 def add_one(a):
      5     if not isinstance(a, (int, float)):
----> 6         raise exceptions.PloomberTypeError("a must be int or float")
      8     return a + 1

PloomberTypeError: a must be int or float. 
If you need help solving this issue, send us a message: https://ploomber.io/community

PloomberKeyError#

Added in version 0.1: Ensure you pin this version in the setup.py file (ploomber-core>=0.1.*)

A subclass of the built-in KeyError, use it when a key is missing:

from ploomber_core import exceptions  # noqa


class MyCollection:
    def __init__(self, data):
        self._data = data

    def __getitem__(self, key):
        if key not in self._data:
            raise exceptions.PloomberKeyError(f"Key {key!r} not in collection")

        return self._data[key]
collection = MyCollection({"a": 1})
collection["a"]
1
collection["b"]
---------------------------------------------------------------------------
PloomberKeyError                          Traceback (most recent call last)
Cell In[11], line 1
----> 1 collection["b"]

Cell In[9], line 10, in MyCollection.__getitem__(self, key)
      8 def __getitem__(self, key):
      9     if key not in self._data:
---> 10         raise exceptions.PloomberKeyError(f"Key {key!r} not in collection")
     12     return self._data[key]

PloomberKeyError: "Key 'b' not in collection. If you need help solving this issue, send us a message: https://ploomber.io/community"

Catching generic exceptions#

Added in version 0.1.1.

To catch and modify exceptions raised by third-party packages:

from ploomber_core.exceptions import modify_exceptions
def do_stuff():
    raise ValueError("some error")


@modify_exceptions
def some_function():
    do_stuff()
some_function()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[14], line 1
----> 1 some_function()

File ~/checkouts/readthedocs.org/user_builds/ploomber-core/checkouts/latest/src/ploomber_core/exceptions.py:128, in modify_exceptions.<locals>.wrapper(*args, **kwargs)
    125 @wraps(fn)
    126 def wrapper(*args, **kwargs):
    127     try:
--> 128         return fn(*args, **kwargs)
    129     except (ValueError, TypeError) as e:
    130         _add_community_link(e)

Cell In[13], line 7, in some_function()
      5 @modify_exceptions
      6 def some_function():
----> 7     do_stuff()

Cell In[13], line 2, in do_stuff()
      1 def do_stuff():
----> 2     raise ValueError("some error")

ValueError: some error
If you need help solving this issue, send us a message: https://ploomber.io/community

Note that @modify_exceptions will only capture ValueError and TypError, if you want other exceptions to work, you can either subclass them (preferred method) or add the modify_exception attribute at runtime (this is applicable when exceptions come from a third-party package)

class SomeThirdPartyException(Exception):
    pass


def some_third_party_call():
    raise SomeThirdPartyException("some error")


@modify_exceptions
def some_function():
    try:
        some_third_party_call()
    except Exception as e:
        e.modify_exception = True
        raise
some_function()
---------------------------------------------------------------------------
SomeThirdPartyException                   Traceback (most recent call last)
Cell In[16], line 1
----> 1 some_function()

File ~/checkouts/readthedocs.org/user_builds/ploomber-core/checkouts/latest/src/ploomber_core/exceptions.py:128, in modify_exceptions.<locals>.wrapper(*args, **kwargs)
    125 @wraps(fn)
    126 def wrapper(*args, **kwargs):
    127     try:
--> 128         return fn(*args, **kwargs)
    129     except (ValueError, TypeError) as e:
    130         _add_community_link(e)

Cell In[15], line 12, in some_function()
      9 @modify_exceptions
     10 def some_function():
     11     try:
---> 12         some_third_party_call()
     13     except Exception as e:
     14         e.modify_exception = True

Cell In[15], line 6, in some_third_party_call()
      5 def some_third_party_call():
----> 6     raise SomeThirdPartyException("some error")

SomeThirdPartyException: some error
If you need help solving this issue, send us a message: https://ploomber.io/community