11//! Errors in leetcode-cli 
2+ use  colored:: Colorize ; 
23use  std:: error:: Error  as  StdError ; 
34use  std:: fmt; 
45
@@ -11,13 +12,13 @@ pub enum Error {
1112    ParseError ( String ) , 
1213    CacheError ( String ) , 
1314    FeatureError ( String ) , 
15+     ScriptError ( String ) , 
1416    CookieError , 
1517    DecryptError , 
1618} 
1719
1820impl  std:: fmt:: Debug  for  Error  { 
1921    fn  fmt ( & self ,  f :  & mut  fmt:: Formatter < ' _ > )  -> fmt:: Result  { 
20-         use  colored:: Colorize ; 
2122        let  e = "error:" . bold ( ) . red ( ) ; 
2223        match  self  { 
2324            Error :: CacheError ( s)  => write ! ( f,  "{} {}, please try again" ,  e,  s) , 
@@ -26,17 +27,20 @@ impl std::fmt::Debug for Error {
2627                "{} {}{}{}{}{}" , 
2728                e, 
2829                "Cannot get leetcode cookies from chrome, " , 
29-                 "please make sure you have logined in leetcode.com with chrome. " . bold( ) , 
30+                 "please make sure you have logined in leetcode.com with chrome. " 
31+                     . yellow( ) 
32+                     . bold( ) , 
3033                "Either you can handwrite your LEETCODE_SESSION and csrf to leetcode.toml, " , 
3134                "more info please checkout this: " , 
3235                "https://github.com/clearloop/leetcode-cli/blob/master/README.md#cookies" 
3336            ) , 
3437            Error :: DownloadError ( s)  => write ! ( f,  "{} Download {} failed, please try again" ,  e,  s) , 
3538            Error :: NetworkError ( s)  => write ! ( f,  "{} {}, please try again" ,  e,  s) , 
36-             Error :: ParseError ( s)  => write ! ( f,  "{} {}, please try again " ,  e,  s) , 
39+             Error :: ParseError ( s)  => write ! ( f,  "{} {}" ,  e,  s) , 
3740            Error :: FeatureError ( s)  => write ! ( f,  "{} {}" ,  e,  s) , 
3841            Error :: MatchError  => write ! ( f,  "{} Nothing matches" ,  e) , 
3942            Error :: DecryptError  => write ! ( f,  "{} openssl decrypt failed" ,  e) , 
43+             Error :: ScriptError ( s)  => write ! ( f,  "{} {}" ,  e,  s) , 
4044        } 
4145    } 
4246} 
@@ -72,7 +76,18 @@ impl std::convert::From<serde_json::error::Error> for Error {
7276// toml 
7377impl  std:: convert:: From < toml:: de:: Error >  for  Error  { 
7478    fn  from ( err :  toml:: de:: Error )  -> Self  { 
75-         Error :: ParseError ( err. description ( ) . to_string ( ) ) 
79+         Error :: ParseError ( format ! ( 
80+             "{}, {}{}{}{}{}{}{}{}" , 
81+             err. description( ) . to_string( ) , 
82+             "Parse config file failed, " , 
83+             "leetcode-cli has just generated a new leetcode.toml at " , 
84+             "~/.leetcode/leetcode_tmp.toml," . green( ) . bold( ) . underline( ) , 
85+             " the current one at " , 
86+             "~/.leetcode/leetcode.toml" . yellow( ) . bold( ) . underline( ) , 
87+             "seems missing some keys, please compare to " , 
88+             "the tmp one and add them up : )\n " , 
89+             "." , 
90+         ) ) 
7691    } 
7792} 
7893
@@ -92,7 +107,13 @@ impl std::convert::From<std::io::Error> for Error {
92107// options 
93108impl  std:: convert:: From < std:: option:: NoneError >  for  Error  { 
94109    fn  from ( _:  std:: option:: NoneError )  -> Self  { 
95-         Error :: ParseError ( "json from response" . to_string ( ) ) 
110+         Error :: ParseError ( format ! ( 
111+             "{}{}{}{}" , 
112+             "json from response parse failed, " , 
113+             "please open a new issue at: " , 
114+             "https://github.com/clearloop/leetcode-cli/" . underline( ) , 
115+             "." 
116+         ) ) 
96117    } 
97118} 
98119
@@ -102,3 +123,10 @@ impl std::convert::From<openssl::error::ErrorStack> for Error {
102123        Error :: DecryptError 
103124    } 
104125} 
126+ 
127+ // pyo3 
128+ impl  std:: convert:: From < pyo3:: PyErr >  for  Error  { 
129+     fn  from ( _:  pyo3:: PyErr )  -> Self  { 
130+         Error :: ScriptError ( "Python script went Error" . to_string ( ) ) 
131+     } 
132+ } 
0 commit comments