Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(installation)!: Return Result instead of panicking #687

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl std::error::Error for UriParseError {}
/// An error that could have occurred while using [`crate::Octocrab`].
#[derive(Snafu, Debug)]
#[snafu(visibility(pub))]
#[non_exhaustive]
pub enum Error {
GitHub {
source: GitHubError,
Expand All @@ -35,7 +36,8 @@ pub enum Error {
source: InvalidUri,
backtrace: Backtrace,
},

#[snafu(display("Installation Error: Github App authorization is required to target an installation.\n\nFound at {}", backtrace))]
Installation { backtrace: Backtrace },
InvalidHeaderValue {
source: http::header::InvalidHeaderValue,
backtrace: Backtrace,
Expand Down
16 changes: 10 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,20 +1008,22 @@ impl Octocrab {
/// then obtain an installation ID, and then pass that here to
/// obtain a new `Octocrab` with which you can make API calls
/// with the permissions of that installation.
pub fn installation(&self, id: InstallationId) -> Octocrab {
pub fn installation(&self, id: InstallationId) -> Result<Octocrab> {
let app_auth = if let AuthState::App(ref app_auth) = self.auth_state {
app_auth.clone()
} else {
panic!("Github App authorization is required to target an installation");
return Err(Error::Installation {
backtrace: Backtrace::generate(),
});
};
Octocrab {
Ok(Octocrab {
client: self.client.clone(),
auth_state: AuthState::Installation {
app: app_auth,
installation: id,
token: CachedToken::default(),
},
}
})
}

/// Similar to `installation`, but also eagerly caches the installation
Expand All @@ -1034,7 +1036,7 @@ impl Octocrab {
&self,
id: InstallationId,
) -> Result<(Octocrab, SecretString)> {
let crab = self.installation(id);
let crab = self.installation(id)?;
let token = crab.request_installation_auth_token().await?;
Ok((crab, token))
}
Expand Down Expand Up @@ -1480,7 +1482,9 @@ impl Octocrab {
{
(app, installation, token)
} else {
panic!("Installation not configured");
return Err(Error::Installation {
backtrace: Backtrace::generate(),
});
};
let mut request = Builder::new();
let mut sensitive_value =
Expand Down
Loading