leetcode_cli/
err.rs

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