Skip to content

Commit

Permalink
fix: improve gh errors (#35)
Browse files Browse the repository at this point in the history
If i force `gh` to be unavailable
```sh
λ ~/thisisarepo ❯ PATH="" ./target/debug/github-lsp
Error: NoToken
```

and if I'm not in a repo
```sh
λ ~/notarepo ❯ github-lsp
Error: NoOwner
```

It is hard to make it fail UTF8, but if it ever happens, it will say
```sh
Error: NotUTF8
```
  • Loading branch information
AlexanderBrevig authored Mar 6, 2024
1 parent f1cfe64 commit 3715108
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/gh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ use tokio::process::Command;

#[derive(Debug)]
pub enum GitHubCLIError {
NoRepo,
NoToken,
NoOwner,
NotUTF8,
}
impl std::error::Error for GitHubCLIError {}
impl fmt::Display for GitHubCLIError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
GitHubCLIError::NoOwner => write!(f, "No Owner found"),
GitHubCLIError::NoToken => write!(f, "No Token found"),
GitHubCLIError::NotUTF8 => write!(f, "No valid UTF-8 found"),
GitHubCLIError::NoRepo => write!(f, "No gh repo found"),
}
}
}
Expand All @@ -39,7 +43,7 @@ async fn gh_cli_token() -> Result<String, GitHubCLIError> {
.stdout
.to_owned();
let output = String::from_utf8(output)
.map_err(|_| GitHubCLIError::NoToken)?
.map_err(|_| GitHubCLIError::NotUTF8)?
.trim()
.to_owned();
Ok(output)
Expand All @@ -55,11 +59,11 @@ pub async fn gh_cli_owner_name() -> std::result::Result<(String, String), GitHub
.arg(".owner.login,.name")
.output()
.await
.map_err(|_| GitHubCLIError::NoOwner)?
.map_err(|_| GitHubCLIError::NoRepo)?
.stdout
.to_owned();
String::from_utf8(output)
.map_err(|_| GitHubCLIError::NoOwner)?
.map_err(|_| GitHubCLIError::NotUTF8)?
.trim()
.split_once('\n')
.map(|on| (on.0.to_string(), on.1.to_string()))
Expand Down

0 comments on commit 3715108

Please sign in to comment.