Convert a low-level `KeyError` into a domain-specific configuration error without losing the original traceback cause.
Answer
Define a domain exception and catch only the low-level error you intend to translate. Raise the domain exception with `from exc` so Python records the original failure as `__cause__`. Callers then have a stable application-level exception to catch, while debugging still shows exactly which low-level key lookup failed.
Strong answer includes
- defines a domain-specific exception
- catches only the intended low-level exception
- uses raise ... from exc for explicit chaining
Practical example
Python
Result
Run the code to see the result.
The caller sees the domain exception while `__cause__` keeps the original KeyError explicitly attached.
Expected result: KeyError, then Missing required key: token.