From c793230582d44f82e804fe885c4b93fa864f6d7b Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Mon, 16 Nov 2026 19:58:29 +0100 Subject: [PATCH] fix(cli): report missing cookies clearly Rename ChromeNotLogin to CookieNotFound, skip cookies DBs that do not exist instead of failing with a raw Io(NotFound), and print errors with Display on stderr with a non-zero exit code. --- src/bin/lc.rs | 3 ++- src/err.rs | 8 ++++++-- src/plugins/chrome.rs | 19 +++++++++++-------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/bin/lc.rs b/src/bin/lc.rs index 4364cb7..814f6fb 100644 --- a/src/bin/lc.rs +++ b/src/bin/lc.rs @@ -8,6 +8,7 @@ fn main() { .expect("Build tokio runtime failed") .block_on(cli::main()) { - println!("{:?}", err); + eprintln!("{err}"); + std::process::exit(1); } } diff --git a/src/err.rs b/src/err.rs index a89ecd6..1601bd1 100644 --- a/src/err.rs +++ b/src/err.rs @@ -54,8 +54,12 @@ pub enum Error { "~/.leetcode/leetcode.toml".yellow().bold().underline(), )] Config(#[from] toml::de::Error), - #[error("Maybe you not login on the Chrome, you can login and retry")] - ChromeNotLogin, + #[error( + "No LeetCode cookies found in Chrome. \ + Please log in at https://leetcode.com in Chrome, \ + or set `csrf` and `session` in `~/.leetcode/leetcode.toml`." + )] + CookieNotFound, #[error(transparent)] ParseInt(#[from] std::num::ParseIntError), #[error(transparent)] diff --git a/src/plugins/chrome.rs b/src/plugins/chrome.rs index d948a13..6276d24 100644 --- a/src/plugins/chrome.rs +++ b/src/plugins/chrome.rs @@ -4,6 +4,7 @@ //! (`LEETCODE_SESSION`, `csrftoken`) from one browser, so this reuses the SQLite //! stack already pulled in by `diesel` to read the cookie DB and implements just //! Chrome's value decryption — no second SQLite client and no dbus linkage. +#![allow(clippy::redundant_field_names)] // diesel derive trips this in generated code use crate::{Error, Result}; use cbc::cipher::{BlockDecryptMut, KeyIvInit, block_padding::Pkcs7}; use diesel::{prelude::*, sql_query, sql_types, sqlite::SqliteConnection}; @@ -42,11 +43,11 @@ pub fn cookies() -> Result { Ok(Ident { csrf: jar .get("csrftoken") - .ok_or(Error::ChromeNotLogin)? + .ok_or(Error::CookieNotFound)? .to_string(), session: jar .get("LEETCODE_SESSION") - .ok_or(Error::ChromeNotLogin)? + .ok_or(Error::CookieNotFound)? .to_string(), }) } @@ -61,7 +62,7 @@ struct CookieRow { /// Read Chrome's cookie DB for `domain` and decrypt the matching values. fn chrome_cookies(domain: &str) -> Result> { - let db = cookie_db_path().ok_or(Error::ChromeNotLogin)?; + let db = cookie_db_path().ok_or(Error::CookieNotFound)?; // Chrome keeps the DB locked while running; read from a snapshot copy. let tmp = std::env::temp_dir().join(format!("leetcode-cli-{}.sqlite", std::process::id())); @@ -123,7 +124,8 @@ fn derive(password: &[u8], iterations: u32) -> Vec { #[cfg(target_os = "macos")] fn cookie_db_path() -> Option { - Some(dirs::home_dir()?.join("Library/Application Support/Google/Chrome/Default/Cookies")) + let p = dirs::home_dir()?.join("Library/Application Support/Google/Chrome/Default/Cookies"); + p.exists().then_some(p) } #[cfg(target_os = "macos")] @@ -141,7 +143,7 @@ fn encryption_key() -> Result> { let pw = String::from_utf8_lossy(&out.stdout); let pw = pw.trim(); if pw.is_empty() { - return Err(Error::ChromeNotLogin); + return Err(Error::CookieNotFound); } Ok(derive(pw.as_bytes(), 1003)) } @@ -170,7 +172,8 @@ fn encryption_key() -> Result> { #[cfg(target_os = "windows")] fn cookie_db_path() -> Option { - Some(dirs::data_local_dir()?.join("Google/Chrome/User Data/Default/Network/Cookies")) + let p = dirs::data_local_dir()?.join("Google/Chrome/User Data/Default/Network/Cookies"); + p.exists().then_some(p) } #[cfg(target_os = "windows")] @@ -178,7 +181,7 @@ fn encryption_key() -> Result> { // Windows uses DPAPI + AES-256-GCM (and app-bound encryption on Chrome // 127+), a separate code path that isn't implemented yet. Until then, // Windows users should set `csrf`/`session` in the config manually. - Err(Error::ChromeNotLogin) + Err(Error::CookieNotFound) } // for headless installations stub functions are defined @@ -190,5 +193,5 @@ fn cookie_db_path() -> Option { #[cfg(target_os = "android")] fn encryption_key() -> Result> { - Err(Error::ChromeNotLogin) + Err(Error::CookieNotFound) }