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

Refine more errors #696

Merged
merged 4 commits into from
Nov 7, 2021
Merged

Refine more errors #696

merged 4 commits into from
Nov 7, 2021

Conversation

kazk
Copy link
Member

@kazk kazk commented Nov 6, 2021

Continuing to refine errors for #688.

  • Move client auth related errors from kube::ConfigError to kube::client::AuthError including OAuthError. Change all errors under kube::client::auth to AuthError. Add kube::Error::Auth(AuthError).
  • Move kube::ConfigError to kube::config and split into InferConfigError (error from Config::infer), InClusterError (in-cluster config errors), KubeconfigError. No more boxing. kube::config can be extracted to kube-config if we want to. Refine errors to preserve context of each error as much as possible. Replace kube::Error::Kubeconfig(ConfigError) with kube::Error::InferConfig(InferConfigError).
  • Move all errors from WebSocket upgrade to kube::client::UpgradeConnectionError, and replace with kube::Error::UpgradeConnection(UpgradeConnectionError) variant.

- Add `kube::client::AuthError` (`kube::client::auth::Error`)
  - Move error variants from `kube::ConfigError` related to client auth
    - `InvalidBasicAuth`
    - `InvalidBearerToken`
    - `UnrefreshableTokenResponse`
    - `ExecPluginFailed`
    - `MalformedTokenExpirationDate`
    - `OAuth`
  - Add `AuthError::ReadTokenFile` (was `ConfigError::ReadFile`)
  - Add `AuthError::ParseTokenKey` (was `kube::Error::SerdeError`)
- Move `kube::error::OAuthError` to `kube::client::OAuthError`
  - Add `OAuthError::BuildRequest` (was `kube::Error::HttpError`)
  - Add `OAuthError::ConcatBuffers` (was `kube::Error::HyperError`)
- Change all error under `kube::client::auth` to `kube::client::AuthError`
- Add `kube::Error::Auth(kube::client::AuthError)`

Signed-off-by: kazk <kazk.dev@gmail.com>
- Move and split `kube::error::ConfigError` into in-cluster and kubeconfig
- Add `kube::config::InferConfigError`
- Add `kube::config::InClusterError`
- Add `kube::config::KubeconfigError`
- Remove `kube::Error::InvalidUri`
- Change `kube::Error::Kubeconfig` to
  `kube::Error::InferConfig(kube::config::InferConfigError)`
- Change errors in `kube::config` to new `kube::config` errors
- Refine errors to preserve context

Signed-off-by: kazk <kazk.dev@gmail.com>
Signed-off-by: kazk <kazk.dev@gmail.com>
@kazk kazk mentioned this pull request Nov 6, 2021
19 tasks
@kazk kazk marked this pull request as ready for review November 6, 2021 08:12
Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a huge batch you've tackled there. Looks great. Really starting to see the benefit now.

The previously daunting src/errors.rs file is looking very small now.

Copy link
Member Author

@kazk kazk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some notes.

I need to write a changelog that's not too intimidating.


/// Failed to exec auth
#[error("failed exec auth: {0}")]
AuthExec(String),
Copy link
Member Author

@kazk kazk Nov 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AuthExec(String) should be broken down, but we can do that later.
In general, we shouldn't have code like Error::AuthExec(format!("result is not a string {:?}", e)).

Comment on lines +378 to +388
fn load_from_base64_or_file<P: AsRef<Path>>(
value: &Option<String>,
file: &Option<P>,
) -> Result<Vec<u8>, LoadDataError> {
let data = value
.as_deref()
.map(load_from_base64)
.or_else(|| file.as_ref().map(load_from_file))
.unwrap_or(Err(LoadDataError::NoBase64DataOrFile))?;
Ok(ensure_trailing_newline(data))
}
Copy link
Member Author

@kazk kazk Nov 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The corresponding function looked like the following before this change:

fn data_or_file_with_base64<P: AsRef<Path>>(data: &Option<String>, file: &Option<P>) -> Result<Vec<u8>> {
    let mut blob = match (data, file) {
        (Some(d), _) => base64::decode(&d)
            .map_err(ConfigError::Base64Decode)
            .map_err(Error::Kubeconfig),
        (_, Some(f)) => read_file(f),
        _ => Err(Error::Kubeconfig(ConfigError::NoBase64FileOrData)),
    }?;
    //Ensure there is a trailing newline in the blob
    //Don't bother if the blob is empty
    if blob.last().map(|end| *end != b'\n').unwrap_or(false) {
        blob.push(b'\n');
    }
    Ok(blob)
}

I changed the error to a separate LoadDataError, so we can describe errors like "decoding base64 failed when loading client key" instead of "decoding base64 failed in kubeconfig".

Changed to .map, .or_else, .unwrap_or(err) because I think it makes the behavior more obvious. Not sure if we should allow both value and file to be Some.

@kazk kazk added the errors error handling or error ergonomics label Nov 6, 2021
Signed-off-by: kazk <kazk.dev@gmail.com>
@kazk kazk merged commit 2b5e4ad into kube-rs:master Nov 7, 2021
@kazk kazk deleted the refine-more-errors branch November 7, 2021 05:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
errors error handling or error ergonomics
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants