Added `MyFriendlyError` for the CLI.

This commit is contained in:
Mahesh Bansod 2021-10-20 21:13:45 +05:30
parent 56d0a7a3a6
commit dc2eff4312
2 changed files with 51 additions and 5 deletions

39
cli/src/error.rs Normal file
View File

@ -0,0 +1,39 @@
use socialvoid_rawclient::ErrorKind;
pub struct MyFriendlyError(socialvoid_rawclient::Error);
impl std::convert::From<socialvoid_rawclient::Error> for MyFriendlyError {
fn from(err: socialvoid_rawclient::Error) -> Self {
Self(err)
}
}
impl std::fmt::Display for MyFriendlyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.0.kind {
ErrorKind::Authentication(err) => {
write!(f, "This method needs you to log in.
Authentication Error: {:#?}\nIf you are already logged in, then try logging out and logging in again.
To log in:
socialvoid-cli login
To log out:
socialvoid-cli logout", err)
}
ErrorKind::Cdn(err) => {
write!(
f,
"There was a problem while uploading/downloading the file from CDN.
CDN Error: {:#?}\nIf it was an authentication error, try logging in.
To log in:
socialvoid-cli login
To log out:
socialvoid-cli logout",
err
)
}
_ => {
write!(f, "{:#?}", self.0)
}
}
}
}

View File

@ -2,8 +2,12 @@ use socialvoid as sv_client;
use socialvoid::session::RegisterRequest;
use structopt::StructOpt;
mod error;
mod utils;
use crate::utils::*;
use error::MyFriendlyError;
// TODO: maybe try to remove ALL the expect calls and use only MyFriendlyError everywhere. + improve the MyFriendlyError
#[tokio::main]
async fn main() {
@ -36,7 +40,10 @@ async fn main() {
//TODO: add OTP support
match sv.authenticate_user(sk, username, password, None).await {
Err(err) => {
println!("Couldn't authenticate the user.\n{:#?}", err);
println!(
"Couldn't authenticate the user.\n{}",
MyFriendlyError::from(err)
);
}
Ok(_) => {
println!("Successfully logged in.");
@ -89,7 +96,7 @@ async fn main() {
.await
{
Ok(peer) => println!("Registered.\n{:?}", peer),
Err(err) => println!("Couldn't register.\n{:?}", err),
Err(err) => println!("Couldn't register.\n{}", MyFriendlyError::from(err)),
}
} else {
println!("You need to accept the terms of service to register to SocialVoid");
@ -104,7 +111,7 @@ async fn main() {
match sv.get_me(current_session).await {
Ok(response) => println!("{:?}", response),
Err(err) => println!("{:?}", err),
Err(err) => println!("{}", MyFriendlyError::from(err)),
}
}
Cli::SetProfile { field, value } => {
@ -121,8 +128,8 @@ async fn main() {
}
Err(err) => {
println!(
"An error occurred while setting the profile picture.\n{:?}",
err
"An error occurred while setting the profile picture.\n{}",
MyFriendlyError::from(err)
);
}
}