Fix request detail load failures
This commit is contained in:
@@ -118,6 +118,7 @@ def _cache_get(key: str) -> Optional[Dict[str, Any]]:
|
|||||||
|
|
||||||
def _cache_set(key: str, payload: Dict[str, Any]) -> None:
|
def _cache_set(key: str, payload: Dict[str, Any]) -> None:
|
||||||
_detail_cache[key] = (time.time() + CACHE_TTL_SECONDS, payload)
|
_detail_cache[key] = (time.time() + CACHE_TTL_SECONDS, payload)
|
||||||
|
_failed_detail_cache.pop(key, None)
|
||||||
|
|
||||||
|
|
||||||
def _status_label_with_jellyfin(current_status: Any, jellyfin_available: bool) -> str:
|
def _status_label_with_jellyfin(current_status: Any, jellyfin_available: bool) -> str:
|
||||||
@@ -169,7 +170,6 @@ async def _request_is_available_in_jellyfin(
|
|||||||
return True
|
return True
|
||||||
availability_cache[cache_key] = False
|
availability_cache[cache_key] = False
|
||||||
return False
|
return False
|
||||||
_failed_detail_cache.pop(key, None)
|
|
||||||
|
|
||||||
|
|
||||||
def _failure_cache_has(key: str) -> bool:
|
def _failure_cache_has(key: str) -> bool:
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ from backend.app.config import settings
|
|||||||
from backend.app.network_security import request_trusts_forwarded_headers, validate_notification_target_url
|
from backend.app.network_security import request_trusts_forwarded_headers, validate_notification_target_url
|
||||||
from backend.app.routers import auth as auth_router
|
from backend.app.routers import auth as auth_router
|
||||||
from backend.app.routers import portal as portal_router
|
from backend.app.routers import portal as portal_router
|
||||||
|
from backend.app.routers import requests as requests_router
|
||||||
from backend.app.routers import status as status_router
|
from backend.app.routers import status as status_router
|
||||||
from backend.app.security import PASSWORD_POLICY_MESSAGE, validate_password_policy
|
from backend.app.security import PASSWORD_POLICY_MESSAGE, validate_password_policy
|
||||||
from backend.app.services import password_reset
|
from backend.app.services import password_reset
|
||||||
@@ -117,6 +118,22 @@ class ServiceStatusTests(unittest.IsolatedAsyncioTestCase):
|
|||||||
self.assertIn("credentials", result["message"].lower())
|
self.assertIn("credentials", result["message"].lower())
|
||||||
|
|
||||||
|
|
||||||
|
class RequestCacheTests(unittest.TestCase):
|
||||||
|
def tearDown(self) -> None:
|
||||||
|
requests_router._detail_cache.clear()
|
||||||
|
requests_router._failed_detail_cache.clear()
|
||||||
|
|
||||||
|
def test_successful_detail_cache_write_clears_prior_failure(self) -> None:
|
||||||
|
key = "request:123"
|
||||||
|
requests_router._failure_cache_set(key)
|
||||||
|
self.assertTrue(requests_router._failure_cache_has(key))
|
||||||
|
|
||||||
|
requests_router._cache_set(key, {"id": 123})
|
||||||
|
|
||||||
|
self.assertFalse(requests_router._failure_cache_has(key))
|
||||||
|
self.assertEqual(requests_router._cache_get(key), {"id": 123})
|
||||||
|
|
||||||
|
|
||||||
class DatabaseEmailTests(TempDatabaseMixin, unittest.TestCase):
|
class DatabaseEmailTests(TempDatabaseMixin, unittest.TestCase):
|
||||||
def test_set_user_email_is_case_insensitive(self) -> None:
|
def test_set_user_email_is_case_insensitive(self) -> None:
|
||||||
created = db.create_user_if_missing(
|
created = db.create_user_if_missing(
|
||||||
|
|||||||
@@ -55,6 +55,44 @@ type ActionHistory = {
|
|||||||
created_at: string
|
created_at: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const readApiError = async (response: Response, fallback: string) => {
|
||||||
|
try {
|
||||||
|
const contentType = response.headers.get('content-type') ?? ''
|
||||||
|
if (contentType.includes('application/json')) {
|
||||||
|
const payload = await response.json()
|
||||||
|
if (typeof payload?.detail === 'string' && payload.detail.trim()) {
|
||||||
|
return payload.detail
|
||||||
|
}
|
||||||
|
if (typeof payload?.message === 'string' && payload.message.trim()) {
|
||||||
|
return payload.message
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const text = await response.text()
|
||||||
|
if (text.trim()) {
|
||||||
|
return text.trim()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
const isSnapshotPayload = (value: unknown): value is Snapshot => {
|
||||||
|
if (!value || typeof value !== 'object') {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
const snapshot = value as Partial<Snapshot>
|
||||||
|
return (
|
||||||
|
typeof snapshot.request_id === 'string' &&
|
||||||
|
typeof snapshot.title === 'string' &&
|
||||||
|
typeof snapshot.request_type === 'string' &&
|
||||||
|
typeof snapshot.state === 'string' &&
|
||||||
|
Array.isArray(snapshot.timeline) &&
|
||||||
|
Array.isArray(snapshot.actions)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const percentFromTorrent = (torrent: Record<string, any>) => {
|
const percentFromTorrent = (torrent: Record<string, any>) => {
|
||||||
const progress = Number(torrent.progress)
|
const progress = Number(torrent.progress)
|
||||||
if (!Number.isNaN(progress) && progress >= 0 && progress <= 1) {
|
if (!Number.isNaN(progress) && progress >= 0 && progress <= 1) {
|
||||||
@@ -201,6 +239,7 @@ export default function RequestTimelinePage() {
|
|||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const [snapshot, setSnapshot] = useState<Snapshot | null>(null)
|
const [snapshot, setSnapshot] = useState<Snapshot | null>(null)
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
|
const [loadError, setLoadError] = useState<string | null>(null)
|
||||||
const [showDetails, setShowDetails] = useState(false)
|
const [showDetails, setShowDetails] = useState(false)
|
||||||
const [actionMessage, setActionMessage] = useState<string | null>(null)
|
const [actionMessage, setActionMessage] = useState<string | null>(null)
|
||||||
const [releaseOptions, setReleaseOptions] = useState<ReleaseOption[]>([])
|
const [releaseOptions, setReleaseOptions] = useState<ReleaseOption[]>([])
|
||||||
@@ -214,6 +253,9 @@ export default function RequestTimelinePage() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
const load = async () => {
|
const load = async () => {
|
||||||
|
setLoading(true)
|
||||||
|
setLoadError(null)
|
||||||
|
setSnapshot(null)
|
||||||
try {
|
try {
|
||||||
if (!getToken()) {
|
if (!getToken()) {
|
||||||
router.push('/login')
|
router.push('/login')
|
||||||
@@ -226,12 +268,22 @@ export default function RequestTimelinePage() {
|
|||||||
authFetch(`${baseUrl}/requests/${requestId}/actions?limit=5`),
|
authFetch(`${baseUrl}/requests/${requestId}/actions?limit=5`),
|
||||||
])
|
])
|
||||||
|
|
||||||
if (snapshotResponse.status === 401) {
|
const authExpired = [snapshotResponse, historyResponse, actionsResponse].some(
|
||||||
|
(response) => response.status === 401
|
||||||
|
)
|
||||||
|
if (authExpired) {
|
||||||
clearToken()
|
clearToken()
|
||||||
router.push('/login')
|
router.push('/login')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (!snapshotResponse.ok) {
|
||||||
|
const message = await readApiError(snapshotResponse, 'Unable to load this request.')
|
||||||
|
throw new Error(message)
|
||||||
|
}
|
||||||
const snapshotData = await snapshotResponse.json()
|
const snapshotData = await snapshotResponse.json()
|
||||||
|
if (!isSnapshotPayload(snapshotData)) {
|
||||||
|
throw new Error('Unable to load this request.')
|
||||||
|
}
|
||||||
setSnapshot(snapshotData)
|
setSnapshot(snapshotData)
|
||||||
setReleaseOptions([])
|
setReleaseOptions([])
|
||||||
setSearchRan(false)
|
setSearchRan(false)
|
||||||
@@ -251,6 +303,9 @@ export default function RequestTimelinePage() {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
|
setLoadError(
|
||||||
|
error instanceof Error && error.message ? error.message : 'Unable to load this request.'
|
||||||
|
)
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
@@ -328,8 +383,12 @@ export default function RequestTimelinePage() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (loadError) {
|
||||||
|
return <main className="card">{loadError}</main>
|
||||||
|
}
|
||||||
|
|
||||||
if (!snapshot) {
|
if (!snapshot) {
|
||||||
return <main className="card">Could not load that request.</main>
|
return <main className="card">Unable to load this request.</main>
|
||||||
}
|
}
|
||||||
|
|
||||||
const summary =
|
const summary =
|
||||||
|
|||||||
Reference in New Issue
Block a user