TypeScript
fix(web): explain pull request diff load failures (#19945)
Jake Leventhal09e54fd990eb
diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsxindex d3feb01b4e8..399c3c5f97d 100644--- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx+++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx@@ -102,6 +102,7 @@ import {type DiffFoldOverride,} from "./pullRequestDiff.logic";import { PullRequestDiffStat, PullRequestMetaLine } from "./pullRequestPresentation";+import { PullRequestDiffUnavailableState } from "./PullRequestDiffUnavailableState";import { usePullRequestFilesViewed } from "./usePullRequestFilesViewed";import {nextPendingReviewCommentId,@@ -1307,7 +1308,12 @@ function PullRequestCodeTab({if (diffQuery.error && loadedSlices.length === 0) {return withToolbar(- <p className="px-4 py-5 text-sm text-muted-foreground">{diffQuery.error}</p>,+ <PullRequestDiffUnavailableState+ error={diffQuery.error}+ provider={detail.provider}+ url={detail.url}+ onRetry={() => diffQuery.refresh()}+ />,);}diff --git a/apps/web/src/components/pullRequest/PullRequestDiffUnavailableState.tsx b/apps/web/src/components/pullRequest/PullRequestDiffUnavailableState.tsxnew file mode 100644index 00000000000..6d837500fc8--- /dev/null+++ b/apps/web/src/components/pullRequest/PullRequestDiffUnavailableState.tsx@@ -0,0 +1,58 @@+import type { SourceControlProviderKind } from "@t3tools/contracts";+import { ExternalLinkIcon, TriangleAlertIcon } from "lucide-react";++import { getSourceControlPresentationForKind } from "~/sourceControlPresentation";++import { Button } from "../ui/button";+import { Empty, EmptyDescription, EmptyHeader, EmptyTitle } from "../ui/empty";+import { RefreshIcon } from "../ui/refresh-icon";+import { describePullRequestDiffError } from "./pullRequestDiffError";++export function PullRequestDiffUnavailableState({+ error,+ provider,+ url,+ onRetry,+}: {+ error: string;+ provider: SourceControlProviderKind;+ url: string;+ onRetry: () => void;+}) {+ const { providerName } = getSourceControlPresentationForKind(provider);+ const { title, description } = describePullRequestDiffError(error, providerName);++ return (+ <Empty size="compact" className="min-h-full" role="status">+ <div className="flex size-11 items-center justify-center rounded-full bg-warning-surface text-warning">+ <TriangleAlertIcon aria-hidden className="size-5" />+ </div>+ <EmptyHeader>+ <EmptyTitle>{title}</EmptyTitle>+ <EmptyDescription>{description}</EmptyDescription>+ </EmptyHeader>+ <div className="flex flex-wrap justify-center gap-2">+ <Button size="sm" variant="default" onClick={onRetry}>+ <RefreshIcon />+ Try again+ </Button>+ <Button+ size="sm"+ variant="outline"+ render={<a href={url} target="_blank" rel="noopener noreferrer" />}+ >+ <ExternalLinkIcon aria-hidden />+ Open on {providerName}+ </Button>+ </div>+ <details className="w-full max-w-lg text-xs text-muted-foreground">+ <summary className="mx-auto w-fit cursor-pointer rounded-sm -outline-offset-2 hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring">+ Technical details+ </summary>+ <pre className="scrollbar-gutter-both mt-3 max-h-40 overflow-auto whitespace-pre-wrap break-words rounded-lg border border-border bg-muted/40 p-3 text-left font-mono text-xs">+ {error}+ </pre>+ </details>+ </Empty>+ );+}diff --git a/apps/web/src/components/pullRequest/pullRequestDiffError.test.ts b/apps/web/src/components/pullRequest/pullRequestDiffError.test.tsnew file mode 100644index 00000000000..26a4423e003--- /dev/null+++ b/apps/web/src/components/pullRequest/pullRequestDiffError.test.ts@@ -0,0 +1,42 @@+import { describe, expect, it } from "vite-plus/test";++import { describePullRequestDiffError } from "./pullRequestDiffError";++describe("describePullRequestDiffError", () => {+ it("explains the rate limit inside an HTTP transport error without exposing the endpoint", () => {+ const error =+ "Failed to fetch remote environment endpoint http://127.0.0.1:49477/api/pull-requests/diff (PullRequestOperationError: Pull request operation diff failed: github requests to github.com are paused until the rate limit resets.).";+ const result = describePullRequestDiffError(error, "GitHub");++ expect(result.title).toBe("GitHub rate limit reached");+ expect(result.description).toContain("after the limit resets");+ expect(result.description).not.toContain("127.0.0.1");+ expect(result.description).not.toContain("PullRequestOperationError");+ });++ it("recognizes a provider's API quota error", () => {+ expect(describePullRequestDiffError("GitHub API rate limit exceeded.", "GitHub").title).toBe(+ "GitHub rate limit reached",+ );+ });++ it("uses the actual host's name for a rate limit", () => {+ const result = describePullRequestDiffError("Too many requests", "GitLab");++ expect(result.title).toBe("GitLab rate limit reached");+ expect(result.description).toContain("on GitLab");+ expect(result.description).not.toContain("GitHub");+ });++ it("does not blame a rate limit for an unknown connection failure", () => {+ const result = describePullRequestDiffError(+ "Failed to fetch remote environment endpoint http://localhost:49477/api/pull-requests/diff (TypeError: Failed to fetch).",+ "GitHub",+ );++ expect(result.title).toBe("Couldn't load the changes");+ expect(result.description).toContain("Try again");+ expect(result.description).not.toContain("localhost");+ expect(result.description).not.toContain("limit");+ });+});diff --git a/apps/web/src/components/pullRequest/pullRequestDiffError.ts b/apps/web/src/components/pullRequest/pullRequestDiffError.tsnew file mode 100644index 00000000000..5703023bbc9--- /dev/null+++ b/apps/web/src/components/pullRequest/pullRequestDiffError.ts@@ -0,0 +1,14 @@+/** Diff failures currently include transport wrappers, so recognize the reason inside them. */+export function describePullRequestDiffError(error: string, providerName: string) {+ if (/\brate limit\b|\brate[-_]limited\b|\btoo many requests\b/i.test(error)) {+ return {+ title: `${providerName} rate limit reached`,+ description: `${providerName} is temporarily limiting requests, so this diff couldn't be loaded. Try again after the limit resets, or view the changes on ${providerName}.`,+ };+ }++ return {+ title: "Couldn't load the changes",+ description: `The environment couldn't load this pull request's diff. Try again, or view the changes on ${providerName}.`,+ };+}