diff --git a/src/error.rs b/src/error.rs index a987fdc8..c4ac9a65 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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, @@ -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, diff --git a/src/lib.rs b/src/lib.rs index 12a07402..82b7981c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1010,20 +1010,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 { 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 @@ -1036,7 +1038,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)) } @@ -1499,7 +1501,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 =