Skip to content

Commit

Permalink
Rework errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bytedream committed Aug 13, 2023
1 parent 7304612 commit efd0823
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 213 deletions.
8 changes: 4 additions & 4 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ impl Account {
if !profile_update.is_empty() {
self.executor
.patch(profile_endpoint)
.json(&serde_json::to_value(profile_update)?)
.json(&Value::Object(profile_update))
.request::<EmptyJsonProxy>()
.await?;
}
if !notification_update.is_empty() {
self.executor
.patch(notification_endpoint)
.json(&serde_json::to_value(notification_update)?)
.json(&Value::Object(notification_update))
.request::<EmptyJsonProxy>()
.await?;
}
Expand Down Expand Up @@ -218,7 +218,7 @@ impl Crunchyroll {
/// Return information about the current account. [`Account`] can be used to modify account
/// settings like the email or web interface language.
pub async fn account(&self) -> Result<Account> {
let mut result: HashMap<String, Value> = HashMap::new();
let mut result: serde_json::Map<String, Value> = serde_json::Map::new();

let me_endpoint = "https://www.crunchyroll.com/accounts/v1/me";
result.extend(
Expand All @@ -236,7 +236,7 @@ impl Crunchyroll {
.await?,
);

let mut account: Account = serde_json::from_value(serde_json::to_value(result)?)?;
let mut account: Account = serde_json::from_value(Value::Object(result))?;
account.executor = self.executor.clone();

Ok(account)
Expand Down
22 changes: 8 additions & 14 deletions src/crunchyroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,9 +687,10 @@ mod auth {
policy: index.cms_web.policy,
key_pair_id: index.cms_web.key_pair_id,
account_id: login_response.account_id.ok_or_else(|| {
Error::Authentication(
"Login with a user account to use this function".into(),
)
Error::Authentication {
message: "Login with a user account to use this function"
.to_string(),
}
}),
},
fixes: self.fixes,
Expand Down Expand Up @@ -721,17 +722,10 @@ mod auth {
let value = serde_json::Value::deserialize(serde::de::value::MapDeserializer::new(
cleaned.into_iter(),
))?;
serde_json::from_value(value.clone()).map_err(|e| {
Error::Decode(
crate::error::ErrorContext::new(format!(
"{} at {}:{}",
e,
e.line(),
e.column()
))
.with_url(url)
.with_value(value.to_string().as_bytes()),
)
serde_json::from_value(value.clone()).map_err(|e| Error::Decode {
message: format!("{} at {}:{}", e, e.line(), e.column()),
content: value.to_string().into_bytes(),
url,
})
}
}
Expand Down
Loading

0 comments on commit efd0823

Please sign in to comment.