Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
Define a base exception for your package with specific subclasses that callers can catch selectively, carry useful context as attributes, and use raise NewError(...) from err to chain the original cause when translating low-level errors.
A base class such as class BillingError(Exception) lets callers catch everything from your package in one place, while subclasses like PaymentDeclined or InvoiceNotFound allow precise handling. Exceptions should carry structured context, such as an invoice id, rather than only a formatted message. When a low-level error like a database or HTTP exception is converted into a domain error, raise InvoiceNotFound(id) from err sets cause, so the traceback shows both errors. raise ... from None hides the original when it is just noise. Do not use exceptions for normal control flow in hot paths, and avoid leaking internal library exceptions through your public API.