Skip to content

fix: graceful degradation when API calls fail in root layout#2184

Open
tessaherself wants to merge 2 commits intohuggingface:mainfrom
tessaherself:fix/resilient-layout-loading
Open

fix: graceful degradation when API calls fail in root layout#2184
tessaherself wants to merge 2 commits intohuggingface:mainfrom
tessaherself:fix/resilient-layout-loading

Conversation

@tessaherself
Copy link
Copy Markdown

Summary

  • Wrap auth-dependent API calls in withFallback() so the root layout doesn't crash on session expiry or API failures
  • Keep public-config as a hard requirement (needed to render anything)
  • Log swallowed errors via console.warn for debugging
  • Fixes Internal Error when expired session causes 302/401 on /api/v2/models

Problem

When a user's session expires mid-navigation, API calls in +layout.ts fail with 302/401. Since all calls are in a Promise.all with no error handling, a single failure crashes the entire app shell with an unhelpful "Internal Error" page.

Changes

  • src/routes/+layout.ts: Add withFallback<T>() helper, wrap auth-dependent calls with safe defaults

Test plan

  • Clear cookies while app is open, navigate — should show logged-out state, not crash
  • Normal authenticated flow still works
  • Check browser console for warning when fallback is used

🤖 Generated with Claude Code

The root layout currently crashes the entire app shell if any API call
fails (e.g., expired session returns 302/401). This replaces the hard
crash with graceful fallbacks for auth-dependent calls (settings,
models, user, feature-flags, conversations) while keeping public-config
as a hard requirement since it's needed to render anything.

Each swallowed error is logged via console.warn for debugging.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0e12a7cd2f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/routes/+layout.ts
Comment on lines +55 to +59
withFallback(
client.user.settings.get().then(handleResponse),
{
welcomeModalSeen: false,
welcomeModalSeenAt: null,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid persisting synthetic settings after read failures

If client.user.settings.get() fails transiently (for example a timeout or 5xx while the user is still authenticated), this fallback injects a complete default settings object and the app continues; later, any settings write via setSettings/instantSet posts the entire store (src/lib/stores/settings.ts), which can silently overwrite real server-side preferences (including sharing opt-in, active model, and custom prompts) with these defaults. This introduces a user-data regression that did not exist when the load failed fast.

Useful? React with 👍 / 👎.

When settings API call fails transiently, mark the resulting defaults
as fallback and skip all server POST calls so they never overwrite
real user preferences.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant