leetcode_cli/
err.rs

1//! Errors in leetcode-cli
2use anyhow::anyhow;
3use colored::Colorize;
4
5#[cfg(debug_assertions)]
6const CONFIG: &str = "~/.leetcode/leetcode.tmp.toml";
7#[cfg(not(debug_assertions))]
8const CONFIG: &str = "~/.leetcode/leetcode_tmp.toml";
9
10/// Leetcode result.
11pub type Result<T> = std::result::Result<T, Error>;
12
13/// Leetcode cli errors
14#[derive(thiserror::Error, Debug)]
15pub enum Error {
16    #[error("Nothing matched")]
17    MatchError,
18    #[error("Download {0} failed, please try again")]
19    DownloadError(String),
20    #[error(transparent)]
21    Reqwest(#[from] reqwest::Error),
22    #[error(transparent)]
23    HeaderName(#[from] reqwest::header::InvalidHeaderName),
24    #[error(transparent)]
25    HeaderValue(#[from] reqwest::header::InvalidHeaderValue),
26    #[error(
27        "Your leetcode cookies seems expired, \
28         {} \
29         Either you can handwrite your `LEETCODE_SESSION` and `csrf` into `leetcode.toml`, \
30         more info please checkout this: \
31         https://github.com/clearloop/leetcode-cli/blob/master/README.md#cookies",
32        "please make sure you have logined in leetcode.com with chrome. ".yellow().bold()
33    )]
34    CookieError,
35    #[error(
36        "Your leetcode account lacks a premium subscription, which the given problem requires.\n \
37         If this looks like a mistake, please open a new issue at: {}",
38        "https://github.com/clearloop/leetcode-cli/".underline()
39    )]
40    PremiumError,
41    #[error(transparent)]
42    Utf8(#[from] std::string::FromUtf8Error),
43    #[error(
44        "json from response parse failed, please open a new issue at: {}.",
45        "https://github.com/clearloop/leetcode-cli/".underline()
46    )]
47    NoneError,
48    #[error(
49        "Parse config file failed, \
50         leetcode-cli has just generated a new leetcode.toml at {}, \
51         the current one at {} seems missing some keys, Please compare \
52         the new file and add the missing keys.\n",
53        CONFIG,
54        "~/.leetcode/leetcode.toml".yellow().bold().underline(),
55    )]
56    Config(#[from] toml::de::Error),
57    #[error("Maybe you not login on the Chrome, you can login and retry")]
58    ChromeNotLogin,
59    #[error(transparent)]
60    ParseInt(#[from] std::num::ParseIntError),
61    #[error(transparent)]
62    Json(#[from] serde_json::Error),
63    #[error(transparent)]
64    Toml(#[from] toml::ser::Error),
65    #[error(transparent)]
66    Io(#[from] std::io::Error),
67    #[error(transparent)]
68    Anyhow(#[from] anyhow::Error),
69    #[error(transparent)]
70    Keyring(#[from] keyring::Error),
71    #[error(transparent)]
72    OpenSSL(#[from] openssl::error::ErrorStack),
73    #[cfg(feature = "pym")]
74    #[error(transparent)]
75    Pyo3(#[from] pyo3::PyErr),
76}
77
78impl std::convert::From<diesel::result::Error> for Error {
79    fn from(err: diesel::result::Error) -> Self {
80        match err {
81            diesel::result::Error::NotFound => Error::Anyhow(anyhow!(
82                "NotFound, you may update cache with `leetcode data -u`, and try it again\r\n"
83            )),
84            _ => Error::Anyhow(anyhow!("{err}")),
85        }
86    }
87}