Repository navigation
feat: one exception family for an invalid ttl (#337) - #463
Merged
Merged
Conversation
An invalid ttl raised CacheXError in @cache but TypeError or ValueError in the backends, CacheManager and CacheLock, so code validating a user-supplied ttl had to catch all three. Add InvalidTTLError (a CacheXError) with two subclasses that keep the built-in bases: TTLTypeError (also a TypeError) and TTLValueError (also a ValueError). validate_ttl, @cache's ttl/stale_ttl check and the Memcached 2038 expiry check raise them, so every entry point raises the same classes and every existing except clause keeps catching them. Exported from fastapi_cachex and documented under TTL values. Closes #337
This was referenced Oct 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #337
Summary
An invalid
ttlraisedCacheXErrorin@cache, butTypeErrororValueErrorin the backends,CacheManagerandCacheLock. Code validating a user-supplied TTL had to catch all three.This PR follows the issue's proposal: a
CacheXErrorsubclass that keeps the built-in bases, so no existingexceptclause breaks.These now raise the new classes:
validate_ttl(), and therefore every backend,CacheManager,CacheLockand@cached@cache's check onttlandstale_ttlOld
exceptclauses keep working:except TypeErrorandexcept ValueErroraround backend, manager or lock calls still catch.except CacheXErroraround@cachestill catches.except ValueErroraround@cache(ttl=-1)now also catches. Nothing changes otherwise.Other changes:
fastapi_cachexand documented by the API reference (::: fastapi_cachex.exceptions).BACKENDS.mdand thettl=0note inHTTP_CACHING.mdare updated in English and zh-TW, with anexcept InvalidTTLErrorexample.validate_delta(the counter delta, not a TTL) is unchanged.The docstrings and docs say "before 0.5.1", assuming this ships in the next patch release. If you release it as 0.6.0, they need changing.
Checklist
tests/cover the change.tests/test_ttl_errors.pychecks each entry point (backendset,CacheManagerand itsset,CacheLock,@cached,@cachettl/stale_ttl, the Memcached 2038 check) for the exact class and both bases. Reverting one raise toTypeErrorfails 12 tests, and reverting@cache's toCacheXErrorfails 2.changelog.d/337.added.mduv run pre-commit run --all-filesanduv run pytest --covpass: 1243 passed, 192 skipped, 92.90% coverage. So do mypy strict,mypy tests,mypy scriptsand bothzensical build --strictruns.Generated by Claude Code