-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(extensions): handle marketplace outages safely #884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AniketDeshmane
wants to merge
1
commit into
webadderallorg:main
Choose a base branch
from
AniketDeshmane:fix/marketplace-error-response
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+135
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { formatMarketplaceHttpError } from "./errorUtils"; | ||
|
|
||
| describe("formatMarketplaceHttpError", () => { | ||
| it("hides upstream HTML when the marketplace is unavailable", () => { | ||
| const html = "<!DOCTYPE html><html><body>SSL handshake failed</body></html>"; | ||
|
|
||
| const message = formatMarketplaceHttpError({ | ||
| status: 525, | ||
| contentType: "text/html; charset=UTF-8", | ||
| body: html, | ||
| }); | ||
|
|
||
| expect(message).toBe( | ||
| "Marketplace is temporarily unavailable (HTTP 525). Please try again later.", | ||
| ); | ||
| expect(message).not.toContain(html); | ||
| }); | ||
|
|
||
| it("keeps a short JSON error for client-side request failures", () => { | ||
| expect( | ||
| formatMarketplaceHttpError({ | ||
| status: 400, | ||
| contentType: "application/json", | ||
| body: JSON.stringify({ error: "Invalid search query" }), | ||
| }), | ||
| ).toBe("Marketplace request failed (HTTP 400): Invalid search query"); | ||
| }); | ||
|
|
||
| it("uses a JSON message when an error field is absent", () => { | ||
| expect( | ||
| formatMarketplaceHttpError({ | ||
| status: 409, | ||
| contentType: "application/json", | ||
| body: JSON.stringify({ message: "Extension version already exists" }), | ||
| }), | ||
| ).toBe("Marketplace request failed (HTTP 409): Extension version already exists"); | ||
| }); | ||
|
|
||
| it("prefers a string error when both JSON detail fields are present", () => { | ||
| expect( | ||
| formatMarketplaceHttpError({ | ||
| status: 400, | ||
| contentType: "application/json", | ||
| body: JSON.stringify({ error: "Primary detail", message: "Secondary detail" }), | ||
| }), | ||
| ).toBe("Marketplace request failed (HTTP 400): Primary detail"); | ||
| }); | ||
|
|
||
| it("hides malformed JSON bodies", () => { | ||
| const body = '{"error":"internal route details"'; | ||
| const message = formatMarketplaceHttpError({ | ||
| status: 400, | ||
| contentType: "application/json", | ||
| body, | ||
| }); | ||
|
|
||
| expect(message).toBe("Marketplace request failed (HTTP 400)."); | ||
| expect(message).not.toContain(body); | ||
| }); | ||
|
|
||
| it("bounds long JSON details and marks truncation without splitting Unicode", () => { | ||
| const detail = `🚀${"x".repeat(200)}`; | ||
| const message = formatMarketplaceHttpError({ | ||
| status: 400, | ||
| contentType: "application/problem+json", | ||
| body: JSON.stringify({ error: detail }), | ||
| }); | ||
|
|
||
| expect(message).toBe(`Marketplace request failed (HTTP 400): 🚀${"x".repeat(198)}…`); | ||
| expect(Array.from(message.split(": ")[1])).toHaveLength(200); | ||
| }); | ||
|
|
||
| it("does not expose non-JSON response bodies", () => { | ||
| expect( | ||
| formatMarketplaceHttpError({ | ||
| status: 404, | ||
| contentType: "text/plain", | ||
| body: "internal route details", | ||
| }), | ||
| ).toBe("Marketplace request failed (HTTP 404)."); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,43 @@ | ||
| export function getErrorMessage(error: unknown): string { | ||
| return error instanceof Error ? error.message : String(error); | ||
| } | ||
| export function getErrorMessage(error: unknown): string { | ||
| return error instanceof Error ? error.message : String(error); | ||
| } | ||
|
|
||
| const MAX_MARKETPLACE_ERROR_DETAIL_LENGTH = 200; | ||
|
|
||
| export function formatMarketplaceHttpError({ | ||
| status, | ||
| contentType, | ||
| body, | ||
| }: { | ||
| status: number; | ||
| contentType: string | null; | ||
| body: string; | ||
| }): string { | ||
| if (status >= 500) { | ||
| return `Marketplace is temporarily unavailable (HTTP ${status}). Please try again later.`; | ||
| } | ||
|
|
||
| let detail: string | null = null; | ||
| if (contentType?.toLowerCase().includes("json")) { | ||
| try { | ||
| const payload: unknown = JSON.parse(body); | ||
| if (payload && typeof payload === "object") { | ||
| const { error, message } = payload as { error?: unknown; message?: unknown }; | ||
| const value = typeof error === "string" ? error : message; | ||
| if (typeof value === "string" && value.trim()) { | ||
| const normalized = value.trim().replace(/\s+/g, " "); | ||
| const codePoints = Array.from(normalized); | ||
| detail = | ||
| codePoints.length > MAX_MARKETPLACE_ERROR_DETAIL_LENGTH | ||
| ? `${codePoints.slice(0, MAX_MARKETPLACE_ERROR_DETAIL_LENGTH - 1).join("")}…` | ||
| : normalized; | ||
| } | ||
| } | ||
| } catch { | ||
| // Malformed or non-API responses are intentionally not exposed to the renderer. | ||
| } | ||
| } | ||
|
|
||
| const summary = `Marketplace request failed (HTTP ${status})`; | ||
| return detail ? `${summary}: ${detail}` : `${summary}.`; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Use
messagewhenerroris blank.For a 4xx payload such as
{ "error": "", "message": "Invalid query" }, Line 26 selects the blankerror. The later check suppresses the validmessage, somarketplaceFetchshows only the generic status summary. Select the first non-empty string and add this case to the formatter tests.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents