Better error handling#6
Open
yhurynovich wants to merge 1 commit into
Open
Conversation
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.
Summary
Two bugs in the browser-based Z.ai auth flow, both causing the script to fail without a usable error message.
The ESM "am I the entry point?" guard compared import.meta.url against a naive string concat of process.argv[1]:
jsif (import.meta.url ===
file://${process.argv[1]}) { ... }import.meta.url is always an absolute, normalized file:// URL, but process.argv[1] is whatever was typed on the command line. Any relative invocation (node scripts/zai_browser_auth.js, as used by the npm run auth:browser script) — or any Windows path (backslashes, drive-letter encoding) — fails this comparison. main() was never called, and the process exited cleanly with zero output: no browser window, no error, nothing.
Fixed by comparing against pathToFileURL(process.argv[1]).href instead of string concatenation. Also added an opt-in ZAI_AUTH_DEBUG_ENTRY=1 env var that prints the actual vs. expected URLs if this check ever fails again, to make this class of bug diagnosable next time.
The polling loop calls page.evaluate(...) (via readToken) every 2s to check localStorage for a token. The moment the user finishes logging in, z.ai navigates/redirects, which can destroy the page's JS execution context mid-evaluate. Puppeteer throws Execution context was destroyed, most likely because of a navigation, which was unhandled and killed the whole process right as auth was about to succeed.
Fixed by catching this (and a few sibling Puppeteer errors — target/session closed, detached frame) around the readToken call and treating them as "retry next tick" rather than fatal, since the next iteration gets a fresh valid context.