Skip to content

Commit 3715108

Browse files
fix: improve gh errors (#35)
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 ```
1 parent f1cfe64 commit 3715108

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/gh/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ use tokio::process::Command;
99

1010
#[derive(Debug)]
1111
pub enum GitHubCLIError {
12+
NoRepo,
1213
NoToken,
1314
NoOwner,
15+
NotUTF8,
1416
}
1517
impl std::error::Error for GitHubCLIError {}
1618
impl fmt::Display for GitHubCLIError {
1719
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1820
match self {
1921
GitHubCLIError::NoOwner => write!(f, "No Owner found"),
2022
GitHubCLIError::NoToken => write!(f, "No Token found"),
23+
GitHubCLIError::NotUTF8 => write!(f, "No valid UTF-8 found"),
24+
GitHubCLIError::NoRepo => write!(f, "No gh repo found"),
2125
}
2226
}
2327
}
@@ -39,7 +43,7 @@ async fn gh_cli_token() -> Result<String, GitHubCLIError> {
3943
.stdout
4044
.to_owned();
4145
let output = String::from_utf8(output)
42-
.map_err(|_| GitHubCLIError::NoToken)?
46+
.map_err(|_| GitHubCLIError::NotUTF8)?
4347
.trim()
4448
.to_owned();
4549
Ok(output)
@@ -55,11 +59,11 @@ pub async fn gh_cli_owner_name() -> std::result::Result<(String, String), GitHub
5559
.arg(".owner.login,.name")
5660
.output()
5761
.await
58-
.map_err(|_| GitHubCLIError::NoOwner)?
62+
.map_err(|_| GitHubCLIError::NoRepo)?
5963
.stdout
6064
.to_owned();
6165
String::from_utf8(output)
62-
.map_err(|_| GitHubCLIError::NoOwner)?
66+
.map_err(|_| GitHubCLIError::NotUTF8)?
6367
.trim()
6468
.split_once('\n')
6569
.map(|on| (on.0.to_string(), on.1.to_string()))

0 commit comments

Comments
 (0)