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

Expose status_code in GithubError #598

Closed
flying-sheep opened this issue Mar 7, 2024 · 3 comments · Fixed by #607
Closed

Expose status_code in GithubError #598

flying-sheep opened this issue Mar 7, 2024 · 3 comments · Fixed by #607
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@flying-sheep
Copy link
Contributor

flying-sheep commented Mar 7, 2024

I’d like to handle a 404 by matching the status code, not the error message or error vector.

@XAMPPRocky XAMPPRocky added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Mar 7, 2024
@XAMPPRocky
Copy link
Owner

Thank you for your issue! However what advantage does that have over matching on the error kind?

@flying-sheep
Copy link
Contributor Author

If that existed for GitHubError, that would be even better, but it doesn’t:

#[non_exhaustive]
pub struct GitHubError {
    pub documentation_url: Option<String>,
    pub errors: Option<Vec<Value>>,
    pub message: String,
}

@flying-sheep
Copy link
Contributor Author

flying-sheep commented Mar 15, 2024

OK, done.

One way to use it would be something like the following.

If you want, I can something like that to the PR, but we’d have to decide on a name (e.g. should it just be impl<T> Into<octocrab::Result<Option<T>>> for octocrab::Result<T>?)

use octocrab::error::{Error, GitHubError};

pub trait OctocrabOptional<T> {
    fn found(self) -> octocrab::Result<Option<T>>;
}

impl<T> OctocrabOptional<T> for octocrab::Result<T> {
    fn found(self) -> octocrab::Result<Option<T>> {
        match self {
            Ok(commit) => Ok(Some(commit)),
            Err(Error::GitHub {
                source:
                    GitHubError {
                        status_code: http::StatusCode::NOT_FOUND,
                        ..
                    },
                ..
            }) => Ok(None),
            Err(e) => Err(e),
        }
    }
}

Then you could do things like:

let ref_: Option<Ref> = octocrab::instance()
    .repos(org, repo)
    .get_ref(&Reference::Branch("main".to_owned()))
    .await
    .found()?;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants