fix(wallets): throw a typed FreighterNotInstalledError with the install URL - #834
Open
woahwhattheheck wants to merge 4 commits into
Open
Conversation
4 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #772
FreighterAdapternow fails with a typed, actionable error carrying the install URL instead of a raw crash, and the guard works in non-browser contexts.What was actually wrong
The adapter already guarded
if (!window.freighter)inconnect(),sign()andgetAddress(), so the plain "extension missing in a browser" case did already throw rather than reach the Freighter API. Two things were still missing against the acceptance criteria:No typed error and no install URL. All three sites threw a bare
Error("Freighter wallet not installed"). Callers had noinstanceofto branch on, no error code, and no way to tell the user where to get the extension.The guard itself crashed off the browser.
window.freighterevaluateswindowfirst. Wherewindowis undefined — SSR, Node, a worker — that access throwsTypeError: Cannot read properties of undefinedbefore the!check can run. That is the exact error named in the issue, and it came from the guard rather than from an API call.Change
src/errors.ts— addsFreighterNotInstalledError extends StellarSplitError, codeFREIGHTER_NOT_INSTALLED, a readonlyinstallUrl, and the URL in both the message andcontext. Follows the file's existing subclass convention, with the matchingisFreighterNotInstalledErrorguard and an exportedFREIGHTER_INSTALL_URLconstant so the URL is not repeated as a literal.src/wallets/adapters/FreighterAdapter.ts— two module-level helpers replace the three inline guards:typeof windowis safe on an undeclared or undefinedwindow, so the non-browser path now reaches the typed error instead of aTypeError.requireFreighter()wraps it and throws. The account-change poller uses the same accessor, so an extension disabled mid-session stops the poll quietly instead of tripping onwindow.freighterevery 2 seconds.src/index.ts— exports the error, its guard and the constant so consumers can catch it. One appended line.Behaviour when the extension is present
Unchanged. The adapter reads the API once into a local and calls exactly the same methods with exactly the same arguments; no call order, return value, polling interval or handler semantics moved. The existing
FreighterAdaptercases intest/walletSessionManager.test.tspass unmodified — that file is untouched here, and it asserts on adapter behaviour rather than on the old message string.WalletNotConnectedErroris deliberately left alone: it means something different (present but not connected) and callers may already depend on it.Tests
New
test/freighterAdapter.test.ts, 13 cases: the typed error from all four entry points, the URL/code/context/name payload,instanceof StellarSplitErrorso existing catch blocks still match, the exported type guard, thewindow === undefinedregression, the unchanged happy path forconnect/sign/getAddress, account-change notification, and the poller surviving both an extension that disappears and awindowthat goes away.CI
Fork run 34058011952 — every step green.
vitest run test/freighterAdapter.test.tsvitest run test/walletSessionManager.test.tstsc --noEmitmainOn the typecheck: the 210 are pre-existing repo-wide diagnostics, and this PR does not move that number. Broken out by touched file:
src/errors.ts,src/wallets/adapters/FreighterAdapter.ts,test/freighterAdapter.test.ts— zero errors.src/index.ts— carries pre-existingTS2305/TS2724missing-export errors at lines 411, 482–487, 690, 870, 873–874, 877, 1416 and 1417. They are unrelated to this change and are present without it. The line this PR adds is 1430, the last line of the file, and contributes none.Not merged, not awarded, nothing paid — submitted for review.