Skip to content

Commit

Permalink
Display WsErrors nicely instead of unwrapping them
Browse files Browse the repository at this point in the history
This improves the user experience with wrong tokens.
  • Loading branch information
mkroening committed Jun 23, 2021
1 parent deed0cb commit 69936d0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion edu-sync-cli/src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Subcommand {
.parse()?
};

let account_config = AccountConfig::new(self.url, token, self.path, self.lang).await;
let account_config = AccountConfig::new(self.url, token, self.path, self.lang).await?;
let account = Account::new(account_config.id.clone(), token);
account.save_token().unwrap();
let mut config = config_task.await??;
Expand Down
16 changes: 12 additions & 4 deletions edu-sync/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,35 @@ impl Display for AccountConfig {
}

impl AccountConfig {
pub async fn new(site_url: Url, token: Token, path: PathBuf, lang: Option<String>) -> Self {
pub async fn new(
site_url: Url,
token: Token,
path: PathBuf,
lang: Option<String>,
) -> Result<Self, ws::Error> {
let ws_client = ws::Client::new(util::shared_http(), &site_url, token, lang.clone());
let Info {
site_url,
user_id,
full_name,
site_name,
..
} = ws_client.get_info().await.unwrap();
} = ws_client.get_info().await.map_err(|err| match err {
ws::RequestError::WsError(err) => err,
ws::RequestError::HttpError(err) => Err(err).unwrap(),
})?;
let id = Id {
site_url,
user_id,
lang,
};
Self {
Ok(Self {
user: full_name,
site: site_name,
id,
path,
courses: CourseConfigs(BTreeMap::new()),
}
})
}
}

Expand Down
7 changes: 5 additions & 2 deletions edu-ws/src/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ use crate::{
#[serde(tag = "errorcode")]
pub enum Error {
#[error("{message}")]
#[serde(rename = "invalidtoken")]
InvalidToken { message: String },
#[serde(rename = "accessexception")]
AccessException { message: String },
#[error("{message}")]
#[serde(rename = "errorcoursecontextnotvalid")]
CourseContextNotValid { message: String },
#[error("{message}")]
#[serde(rename = "invalidtoken")]
InvalidToken { message: String },
}

#[derive(Error, Debug)]
Expand Down

0 comments on commit 69936d0

Please sign in to comment.