diff --git a/backend/app/routes/channels.py b/backend/app/routes/channels.py index f21ed47..d996047 100644 --- a/backend/app/routes/channels.py +++ b/backend/app/routes/channels.py @@ -313,7 +313,17 @@ def subscribe( if channel.details_synced_at is None: apply_channel_details(db, yt.get_channels([channel_id])) except YouTubeError as exc: - raise HTTPException(status_code=502, detail=f"YouTube subscribe failed: {exc}") + # Already following on YouTube but not recorded here (a desync — e.g. a stale import + # dropped the local row, so it resurfaced in discovery). That's not a failure: the end + # state the user wanted already holds, so record it locally and move on. The next + # subscription resync fills in the resource id. Any other YouTube error is a real + # problem — surface it with a clear message (422, not a 502 that the client would + # mistake for a transient "connection lost"). + msg = str(exc).lower() + if "already exists" in msg or "duplicate" in msg: + yt_sub_id = "" + else: + raise HTTPException(status_code=422, detail=f"YouTube couldn't subscribe you: {exc}") db.add( Subscription( user_id=user.id, @@ -349,7 +359,9 @@ def unsubscribe( with quota.attribute(user.id, "unsubscribe"), YouTubeClient(db, user) as yt: yt.delete_subscription(sub.yt_subscription_id) except YouTubeError as exc: - raise HTTPException(status_code=502, detail=f"YouTube unsubscribe failed: {exc}") + # 422 (a real, explainable error → speaking modal), not 502 which the client treats as + # a transient "connection lost" gateway blip. + raise HTTPException(status_code=422, detail=f"YouTube couldn't unsubscribe you: {exc}") db.delete(sub) db.commit() return {"unsubscribed": channel_id}