Skip to content

Commit 3f49a50

Browse files
committed
check for gui browser
1 parent ebaa5ce commit 3f49a50

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

codeflash/code_utils/oauth_handler.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -639,21 +639,27 @@ def exchange_code_for_token(self, code: str, code_verifier: str, redirect_uri: s
639639
self.token_error = "No access token in response" # noqa: S105
640640
return None
641641

642-
except requests.exceptions.HTTPError as e:
643-
error_msg = f"HTTP {e.response.status_code}"
644-
try:
645-
error_data = e.response.json()
646-
error_msg = error_data.get("error_description", error_data.get("error", error_msg))
647-
except Exception:
648-
self.token_error = "Unauthorized" # noqa: S105
649-
return None
650-
except Exception:
642+
except requests.exceptions.HTTPError:
651643
self.token_error = "Unauthorized" # noqa: S105
652644
return None
653645
else:
654646
return api_key
655647

656648

649+
def _is_graphical_browser() -> bool:
650+
text_browsers = {"lynx", "links", "w3m", "elinks", "links2"}
651+
652+
try:
653+
# Get the default browser
654+
browser = webbrowser.get()
655+
browser_name = getattr(browser, "name", "").lower()
656+
657+
# Check if it's a known text browser
658+
return all(text_browser not in browser_name for text_browser in text_browsers)
659+
except Exception:
660+
return True
661+
662+
657663
def _wait_for_manual_code_input(oauth: OAuthHandler) -> None:
658664
"""Thread function to wait for manual code input."""
659665
try:
@@ -700,10 +706,11 @@ def perform_oauth_signin() -> str | None:
700706
click.echo("❌ Failed to start local server.")
701707
return None
702708

703-
# Try to open browser
704-
click.echo("🌐 Opening browser to sign in to CodeFlash…")
705-
with contextlib.suppress(Exception):
706-
webbrowser.open(local_auth_url)
709+
if _is_graphical_browser():
710+
# Try to open browser
711+
click.echo("🌐 Opening browser to sign in to CodeFlash…")
712+
with contextlib.suppress(Exception):
713+
webbrowser.open(local_auth_url)
707714

708715
# Show remote URL and start input thread
709716
click.echo("\n📋 If browser didn't open, visit this URL:")

0 commit comments

Comments
 (0)