Skip to content

Commit

Permalink
Merge pull request #83 from PurpleMyst/master
Browse files Browse the repository at this point in the history
Return an error if roux::Reddit::create_client is called without credentials set
  • Loading branch information
beanpuppy authored Feb 21, 2024
2 parents 57a29bf + d6cf8c4 commit b18db0b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ impl Reddit {
let url = &url::build_url("api/v1/access_token")[..];
let form = [
("grant_type", "password"),
("username", &self.config.username.to_owned().unwrap()),
("password", &self.config.password.to_owned().unwrap()),
("username", &self.config.username.to_owned().ok_or(util::RouxError::CredentialsNotSet)?),
("password", &self.config.password.to_owned().ok_or(util::RouxError::CredentialsNotSet)?),
];

let request = self
Expand Down
4 changes: 4 additions & 0 deletions src/util/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub enum RouxError {
Parse(serde_json::Error),
/// Occurs if there is a grant error.
Auth(String),
/// Occurs if [`Reddit::create_client`] is called before [`Reddit::username`] and [`Reddit::password`].
CredentialsNotSet,
}

impl From<client::Error> for RouxError {
Expand All @@ -38,6 +40,7 @@ impl fmt::Display for RouxError {
RouxError::Network(ref err) => err.fmt(f),
RouxError::Parse(ref err) => err.fmt(f),
RouxError::Auth(ref err) => write!(f, "Auth error: {}", err),
RouxError::CredentialsNotSet => write!(f, "Must set username and password before calling create_client"),
}
}
}
Expand All @@ -47,6 +50,7 @@ impl error::Error for RouxError {
match *self {
RouxError::Status(_) => None,
RouxError::Auth(_) => None,
RouxError::CredentialsNotSet => None,
RouxError::Network(ref err) => Some(err),
RouxError::Parse(ref err) => Some(err),
}
Expand Down

0 comments on commit b18db0b

Please sign in to comment.