Skip to content

fix(history-import): explain rejected and unreachable import sources - #1332

Closed
Quick104 wants to merge 1 commit into
mainfrom
fix/history-import-start-errors
Closed

Quick104 wants to merge 1 commit into
mainfrom
fix/history-import-start-errors

Conversation

@Quick104

Copy link
Copy Markdown
Contributor

Problem

Related issue: #1243

When a personal history import could not start, the web page showed a toast that did not say why:

Case Toast before
The source server refused the username or password "The request did not pass validation; see errors."
A Jellyfin address without http:// or https:// "An unexpected error occurred."
The source server was unreachable (DNS, refused connection, TLS, timeout) "An unexpected error occurred."

The v2 problem for a refused login carried the reason only in errors[0].detail, which the web toast does not show. Transport failures and historyimport.ErrInvalidInput had no mapping at all, so they became a 500 and an error log line. The old string check in looksLikeValidationError no longer matches the current Jellyfin message.

Approach

  • Map ErrInvalidInput wraps to 400 with their message. Every wrap site uses a fixed string. A bare ErrInvalidInput marks an internal invariant in the personal queue, so it stays a logged 500.
  • Map HTTP client transport failures to the existing upstream 502 with "Couldn't reach that server. Check the address and make sure Silo can connect to it." The check requires the *url.Error that net/http returns, so a Silo database connection failure, which pgx also reports as *net.OpError, stays a logged 500. The v2 surface already turns upstream 5xx into 503 dependency_unavailable with Retry-After: 30.
  • Validate personal Jellyfin and Plex addresses with the rule admin sources already use, extracted as validBaseURL. Before this, a malformed Plex address surfaced as "Personal imports are unavailable."
  • In v2, the validation_failed problem detail now repeats the message in errors[0].detail. Fixing this on the server covers every client that shows the problem detail, so the web, Apple, and Android clients need no change. Changing only the web toast to read errors[] would fix one client.

After this change the same three cases show:

Case Toast after
Refused login "Couldn't connect to that server. Check the URL, username, and password and try again."
Address without a scheme "Enter the Jellyfin address with http:// or https:// at the start."
Unreachable server "Couldn't reach that server. Check the address and make sure Silo can connect to it."

docs/admin-api.md documents these responses for POST /api/v2/history-imports/runs.

Validation

  • New handler tests cover a wrapped ErrInvalidInput, a bare sentinel (stays 500), an unreachable source through *url.Error (502, upstream), and a pgx-shaped *net.OpError without *url.Error (stays 500). The v2 tests now assert that detail matches errors[0].detail.
  • Checked by hand in the web settings page against a local Silo server and a Jellyfin 12.1.0 container. All three "before" toasts and the refused-login "after" toast were observed there. The other two "after" rows changed after that check (the address message was reworded, and the reachability check gained the *url.Error guard from review); the handler tests cover their final text and status.
  • The full gate from CONTRIBUTING.md ran on this change combined with fix(historyimport): import Jellyfin favorites and support Jellyfin 12 #1331, before the two were split and before the review fixes below: build, gofmt, go vet, make lint-changed, the Postgres-backed history-import cancellation tests, the web checks, and every verify-* target passed.
    • make test-go failed only in TestAdminResourceCapabilitiesAndScope, which fails the same way on main on macOS, and in internal/policy 25 ms evaluation timeouts under full-suite load. internal/policy passes when run alone.
    • make verify-local-paths crashed with a bus error under the local GNU Make 3.81. The script passes when run directly and through make with a clean environment.
  • After the review fixes and the split, this branch builds, make lint-changed reports 0 issues, and go test ./internal/historyimport/ ./internal/api/handlers/ passes. go test ./internal/apiv2/ fails only in TestAdminResourceCapabilitiesAndScope.

Risks

  • The mapping is shared with the frozen /api/v1 history-import routes. There, requests that returned 500 now return 400 (invalid input) or 502 (unreachable source) with the same error body shape. Everything else on v1 is unchanged. This is a fix that keeps the bridge usable, which the v1 freeze allows.
  • An unreachable source now logs a warning instead of an error.
  • Emby Connect and plex.tv failures use the same "Check the address" wording, although the user did not type an address in those flows.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.

AI Disclosure

  • Harness: T3 Code running Claude Code
  • Tool(s): Claude Code, including Claude Code subagents for the audit and review
  • Model(s): claude-opus-5-5[1m] (main agent); claude-opus-5-5 (subagents)
  • Involvement: Fully AI-generated; human review pending
  • Adversarial review: An independent Claude Opus 5.5 subagent reviewed the combined diff of this PR and fix(historyimport): import Jellyfin favorites and support Jellyfin 12 #1331, read-only. It traced every caller of historyImportAPIError, including v1 and admin routes, probed error classification with real pgx connection errors, statement timeouts, and deadline errors, and checked the v2 fixtures and committed OpenAPI artifact. Findings for this PR:
    • A Silo database outage would have been reported as "Couldn't reach that server" and no longer logged. Fixed by requiring *url.Error, with a regression test.
    • A bare ErrInvalidInput from internal invariants would have become a silent 400. It stays a logged 500.
    • A malformed personal Plex address still got the wrong message. Validation added.
    • Capitalizing the first byte broke multibyte text, and a double wrap lost the message. Fixed with rune-aware capitalization and a substring cut.
    • The v1 status change is now stated under Risks.
    • Not changed: the "Check the address" wording for Emby Connect and plex.tv failures.

🤖 Generated with Claude Code

Starting a personal import showed a toast that did not say what went
wrong. A source that refused the credentials produced a v2 problem whose
detail was "The request did not pass validation; see errors." with the
reason only in errors[0].detail. An unreachable server, a TLS failure,
or a Jellyfin address without a scheme became a 500 "An unexpected
error occurred", because ErrInvalidInput and transport failures had no
mapping.

Map ErrInvalidInput wraps to 400 with their fixed message, and HTTP
client transport failures to the upstream 502 with a reachability
message. A bare ErrInvalidInput and database connection errors stay
internal errors. Validate personal Jellyfin and Plex addresses with the
rule admin sources already use, and repeat the member message in the v2
problem detail so clients that show only the detail explain the failure.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Quick104 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@coderabbitai

coderabbitai Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 24 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 8f892272-0e75-415e-862e-c9a9c716b13a

📥 Commits

Reviewing files that changed from the base of the PR and between 8e5ae2f and a992778.

📒 Files selected for processing (7)
  • docs/admin-api.md
  • internal/api/handlers/history_import.go
  • internal/api/handlers/history_import_test.go
  • internal/apiv2/history_imports.go
  • internal/apiv2/history_imports_test.go
  • internal/historyimport/editor_concurrency.go
  • internal/historyimport/service_personal_auth.go

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-23T01:32:51.897471Z a992778 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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

Copy link
Copy Markdown

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: a992778e5d

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +108 to 111
if !validBaseURL(strings.TrimSpace(input.PlexBaseURL)) {
return out, fmt.Errorf("%w: enter the Plex address with http:// or https:// at the start", ErrInvalidInput)
}
out.Credentials = personalRunCredentials{BaseURL: input.PlexBaseURL, ServerToken: input.PlexToken, AccountToken: firstNonEmpty(input.PlexAccountToken, input.PlexToken)}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Normalize the Plex URL before storing it

When a custom Plex address has leading or trailing whitespace, this validates the trimmed value but stores the original value in out.Credentials. enqueuePersonalRun later passes that untrimmed address to validateSource, which rejects it and returns ErrPersonalCredentialsUnavailable, so a valid copy-pasted URL produces a misleading 503 instead of starting the import. Store the trimmed URL or validate the exact value that will be stored.

AGENTS.md reference: AGENTS.md:L7-L9

Useful? React with 👍 / 👎.

Comment on lines +72 to +73
if !validBaseURL(strings.TrimSpace(input.JellyfinBaseURL)) {
return out, fmt.Errorf("%w: enter the Jellyfin address with http:// or https:// at the start", ErrInvalidInput)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Describe every rejected URL condition accurately

For an address such as https://server.example?token=x, or one containing user info or a fragment, validBaseURL rejects the input even though it already starts with https://; this response then tells the user to add a scheme, which cannot fix the request. The same misleading message is used for custom Plex addresses. Distinguish a missing/invalid scheme from the other forbidden components, or report the complete accepted URL rule.

AGENTS.md reference: AGENTS.md:L7-L9

Useful? React with 👍 / 👎.

Comment on lines +322 to +324
if historyImportSourceUnreachable(err) {
slog.Warn("history import: source server unreachable", "error", err)
return HistoryImportUnreachableAPIError()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid duplicate error logs for mapped outages

When Emby Connect or plex.tv is unreachable, LoginEmbyConnect and CreatePlexPin log the failure at error level before calling this mapper, after which this new branch logs the same failure again at warning level. Those expected upstream outages therefore still trigger error-level alerts and now create duplicate records, contrary to the mapper's warning treatment; centralize the log here or remove the callers' unconditional error logs.

Useful? React with 👍 / 👎.

@Quick104 Quick104 closed this Sep 24, 2026
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