mirror of
https://github.com/librespot-org/librespot.git
synced 2025-10-04 10:19:27 +02:00
fix: map authentication errors to a custom error type
This commit is contained in:
parent
06f5aa9c90
commit
71e9295ee8
4 changed files with 51 additions and 14 deletions
|
@ -13,6 +13,7 @@ use std::ops::FnOnce;
|
|||
use std::path::Path;
|
||||
|
||||
use crate::protocol::authentication::AuthenticationType;
|
||||
use crate::protocol::keyexchange::{APLoginFailed, ErrorCode};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Credentials {
|
||||
|
@ -191,3 +192,37 @@ pub fn get_credentials<F: FnOnce(&String) -> String>(
|
|||
(None, _, None) => None,
|
||||
}
|
||||
}
|
||||
|
||||
error_chain! {
|
||||
types {
|
||||
AuthenticationError, AuthenticationErrorKind, AuthenticationResultExt, AuthenticationResult;
|
||||
}
|
||||
|
||||
foreign_links {
|
||||
Io(::std::io::Error);
|
||||
}
|
||||
|
||||
errors {
|
||||
BadCredentials {
|
||||
description("Bad credentials")
|
||||
display("Authentication failed with error: Bad credentials")
|
||||
}
|
||||
PremiumAccountRequired {
|
||||
description("Premium account required")
|
||||
display("Authentication failed with error: Premium account required")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<APLoginFailed> for AuthenticationError {
|
||||
fn from(login_failure: APLoginFailed) -> Self {
|
||||
let error_code = login_failure.get_error_code();
|
||||
match error_code {
|
||||
ErrorCode::BadCredentials => Self::from_kind(AuthenticationErrorKind::BadCredentials),
|
||||
ErrorCode::PremiumAccountRequired => {
|
||||
Self::from_kind(AuthenticationErrorKind::PremiumAccountRequired)
|
||||
}
|
||||
_ => format!("Authentication failed with error: {:?}", error_code).into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue