Skip to content

Commit 0ca4fda

Browse files
committed
feat(session): derive SessionError from CacheXError
SessionError derived from Exception while StateError derived from CacheXError, so except CacheXError caught state errors but not session errors. SessionError still is an Exception, so existing handlers keep working. Closes #162
1 parent 1574819 commit 0ca4fda

5 files changed

Lines changed: 29 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ Note that 0.3.3 was never released; 0.3.4 follows 0.3.2.
4444
summaries with their issue links, grouped as in the changelog, and links to
4545
the full entries on the documentation site. The changelog itself keeps the
4646
details.
47+
- **`SessionError` derives from `CacheXError`.** It derived from `Exception`,
48+
while `StateError` already derived from `CacheXError`, so `except CacheXError`
49+
caught state errors but not session errors. Handlers for `SessionError` or
50+
`Exception` keep working. A `try` block that lists `except CacheXError`
51+
before `except SessionError` now takes the `CacheXError` branch for session
52+
errors. ([#162](https://github.com/allen0099/FastAPI-CacheX/issues/162))
4753

4854
### Deprecated
4955

‎docs/SESSION.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,8 @@ be sent back to the client. `get_session()` raises a `SessionError` subclass on
645645
failure: `SessionTokenError` (malformed token), `SessionSecurityError` (bad
646646
signature or binding mismatch), `SessionNotFoundError`, `SessionInvalidError`
647647
(session not active) or `SessionExpiredError` (TTL or absolute timeout exceeded).
648+
Since 0.3.8, `SessionError` derives from `CacheXError`, so `except CacheXError`
649+
catches session errors too.
648650

649651
Every method with its signature is in the generated
650652
[Session API reference](api/session.md).

‎fastapi_cachex/session/exceptions.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
"""Session-related exceptions."""
22

3+
from fastapi_cachex.exceptions import CacheXError
34

4-
class SessionError(Exception):
5-
"""Base exception for session errors."""
5+
6+
class SessionError(CacheXError):
7+
"""Base exception for session errors.
8+
9+
Derives from ``CacheXError`` since 0.3.8, like ``StateError``, so
10+
``except CacheXError`` also catches session errors.
11+
"""
612

713

814
class SessionNotFoundError(SessionError):

‎i18n/zh-TW/docs/SESSION.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ session, new_token = await manager.regenerate_session_id(session)
534534

535535
## SessionManager 概覽 {#sessionmanager-at-a-glance}
536536

537-
`SessionManager(backend, config, token_serializer=None)` 處理整個生命週期:`create_session()`/`create_anonymous_session()` 回傳 `(session, token)`;`get_session()` 回傳 `(session, renewed_token)`,其中 `renewed_token` 只有在滑動過期更新了權杖時才會有值,並應傳回給用戶端。`get_session()` 失敗時會拋出 `SessionError` 的子類別:`SessionTokenError`(權杖格式錯誤)、`SessionSecurityError`(簽章錯誤或綁定不符)、`SessionNotFoundError`、`SessionInvalidError`(Session 不是啟用狀態)或 `SessionExpiredError`(超過 TTL 或絕對逾時)。
537+
`SessionManager(backend, config, token_serializer=None)` 處理整個生命週期:`create_session()`/`create_anonymous_session()` 回傳 `(session, token)`;`get_session()` 回傳 `(session, renewed_token)`,其中 `renewed_token` 只有在滑動過期更新了權杖時才會有值,並應傳回給用戶端。`get_session()` 失敗時會拋出 `SessionError` 的子類別:`SessionTokenError`(權杖格式錯誤)、`SessionSecurityError`(簽章錯誤或綁定不符)、`SessionNotFoundError`、`SessionInvalidError`(Session 不是啟用狀態)或 `SessionExpiredError`(超過 TTL 或絕對逾時)。從 0.3.8 起,`SessionError` 繼承自 `CacheXError`,因此 `except CacheXError` 也會捕捉 Session 錯誤。
538538

539539
每個方法及其簽名請見自動產生的 [Session API 參考](https://fastapi-cachex.readthedocs.io/en/latest/api/session/)(英文)。
540540

‎tests/session/test_manager.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pydantic import SecretStr
1212

1313
from fastapi_cachex.backends.memory import MemoryBackend
14+
from fastapi_cachex.exceptions import CacheXError
1415
from fastapi_cachex.session.config import SessionConfig
1516
from fastapi_cachex.session.exceptions import SessionExpiredError
1617
from fastapi_cachex.session.exceptions import SessionInvalidError
@@ -120,6 +121,17 @@ async def test_get_invalid_token(manager: SessionManager) -> None:
120121
await manager.get_session("invalid-token")
121122

122123

124+
@pytest.mark.asyncio
125+
async def test_session_errors_are_caught_as_cachex_errors(
126+
manager: SessionManager,
127+
) -> None:
128+
"""Test a session error is caught by ``except CacheXError`` (#162)."""
129+
with pytest.raises(CacheXError) as exc_info:
130+
await manager.get_session("invalid-token")
131+
132+
assert isinstance(exc_info.value, SessionTokenError)
133+
134+
123135
@pytest.mark.asyncio
124136
async def test_get_nonexistent_session(manager: SessionManager) -> None:
125137
"""Test getting nonexistent session."""

0 commit comments

Comments
 (0)