-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: implement more friendly updater errors #1770
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
nmggithub
wants to merge
3
commits into
pingdotgg:main
Choose a base branch
from
nmggithub:friendly-update-errors
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,39 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { normalizeDesktopUpdateError } from "./updateErrors"; | ||
|
|
||
| describe("normalizeDesktopUpdateError", () => { | ||
| it("maps network-style check errors to a friendly message", () => { | ||
| expect(normalizeDesktopUpdateError(new Error("net::ERR_CONNECTION_REFUSED"), "check")).toEqual({ | ||
| message: "Couldn't reach the update server. Check your connection and try again.", | ||
| rawMessage: "net::ERR_CONNECTION_REFUSED", | ||
| toastAction: null, | ||
| }); | ||
| }); | ||
|
|
||
| it("maps checksum download failures to a retryable message", () => { | ||
| expect( | ||
| normalizeDesktopUpdateError( | ||
| new Error("sha512 checksum mismatch, expected abc but got def"), | ||
| "download", | ||
| ), | ||
| ).toEqual({ | ||
| message: "The downloaded update could not be verified. Try downloading it again.", | ||
| rawMessage: "sha512 checksum mismatch, expected abc but got def", | ||
| toastAction: { | ||
| kind: "desktop-update.retry-download", | ||
| label: "Retry download", | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("falls back to the raw message when it is already useful", () => { | ||
| expect( | ||
| normalizeDesktopUpdateError(new Error("Release feed returned malformed JSON"), "check"), | ||
| ).toEqual({ | ||
| message: "Release feed returned malformed JSON", | ||
| rawMessage: "Release feed returned malformed JSON", | ||
| toastAction: null, | ||
| }); | ||
| }); | ||
| }); |
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,76 @@ | ||
| import type { DesktopToastAction, DesktopUpdateState } from "@t3tools/contracts"; | ||
|
|
||
| type DesktopUpdateErrorContext = DesktopUpdateState["errorContext"]; | ||
|
|
||
| const RETRY_DOWNLOAD_TOAST_ACTION: DesktopToastAction = { | ||
| kind: "desktop-update.retry-download", | ||
| label: "Retry download", | ||
| }; | ||
|
|
||
| const NETWORK_ERROR_PATTERN = | ||
| /\b(EAI_AGAIN|ECONNABORTED|ECONNREFUSED|ECONNRESET|ENETUNREACH|ENOTFOUND|ERR_CONNECTION_(?:CLOSED|REFUSED|RESET|TIMED_OUT)|ERR_INTERNET_DISCONNECTED|ERR_NAME_NOT_RESOLVED|ETIMEDOUT|socket hang up)\b/i; | ||
| const CHECKSUM_ERROR_PATTERN = | ||
| /\b(checksum|sha(?:256|512)?|hash)\b.*\b(mismatch|invalid|failed|different)\b|\b(mismatch|invalid|failed|different)\b.*\b(checksum|sha(?:256|512)?|hash)\b/i; | ||
|
|
||
| function formatDesktopUpdateRawError(error: unknown): string { | ||
| if (error instanceof Error) { | ||
| return error.message; | ||
| } | ||
| return String(error); | ||
| } | ||
|
|
||
| function getFallbackDesktopUpdateErrorMessage(context: DesktopUpdateErrorContext): string { | ||
| if (context === "check") return "Couldn't check for updates."; | ||
| if (context === "download") return "Couldn't download the update."; | ||
| if (context === "install") return "Couldn't install the update."; | ||
| return "Update failed."; | ||
| } | ||
|
|
||
| function isNetworkErrorMessage(message: string): boolean { | ||
| return NETWORK_ERROR_PATTERN.test(message); | ||
| } | ||
|
|
||
| function isChecksumErrorMessage(message: string): boolean { | ||
| return CHECKSUM_ERROR_PATTERN.test(message); | ||
| } | ||
|
|
||
| export function normalizeDesktopUpdateError( | ||
| error: unknown, | ||
| context: DesktopUpdateErrorContext, | ||
| ): { | ||
| message: string; | ||
| rawMessage: string; | ||
| toastAction: DesktopToastAction | null; | ||
| } { | ||
| const rawMessage = formatDesktopUpdateRawError(error).trim(); | ||
|
|
||
| if (context === "download" && isChecksumErrorMessage(rawMessage)) { | ||
| return { | ||
| message: "The downloaded update could not be verified. Try downloading it again.", | ||
| rawMessage, | ||
| toastAction: RETRY_DOWNLOAD_TOAST_ACTION, | ||
| }; | ||
| } | ||
|
|
||
| if (isNetworkErrorMessage(rawMessage)) { | ||
| if (context === "download") { | ||
| return { | ||
| message: | ||
| "Couldn't download the update because the update server is unavailable. Try again in a moment.", | ||
| rawMessage, | ||
| toastAction: null, | ||
| }; | ||
| } | ||
| return { | ||
| message: "Couldn't reach the update server. Check your connection and try again.", | ||
| rawMessage, | ||
| toastAction: null, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| message: rawMessage.length > 0 ? rawMessage : getFallbackDesktopUpdateErrorMessage(context), | ||
| rawMessage, | ||
| toastAction: null, | ||
| }; | ||
| } | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
This is a bit over-engineered, as upstream Electron Updater should only ever have one error message, but this does make our implementation (hopefully) future-proof if they change that error message. We may also be able to key off their
ERR_CHECKSUM_MISMATCHcode, if we desire.