Skip to content

Commit

Permalink
feat(term): handle github unauthorized error
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Jul 7, 2024
1 parent 28ba85e commit 0f9acbb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
40 changes: 33 additions & 7 deletions crates/synd_term/src/client/github/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use graphql_client::GraphQLQuery;
use octocrab::Octocrab;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{
config,
Expand All @@ -10,6 +11,30 @@ use crate::{
},
};

#[derive(Debug, Error)]
pub(crate) enum GithubError {
#[error("invalid credential. please make sure a valid PAT is set")]
BadCredential,
#[error("github api error: {0}")]
Api(octocrab::Error),
}

impl From<octocrab::Error> for GithubError {
fn from(err: octocrab::Error) -> Self {
match &err {
octocrab::Error::GitHub { source, .. } => {
// octocrab does not re-export http crate
if source.status_code.as_u16() == 401 {
GithubError::BadCredential
} else {
GithubError::Api(err)
}
}
_ => GithubError::Api(err),
}
}
}

#[derive(Clone)]
pub struct GithubClient {
client: Octocrab,
Expand All @@ -30,15 +55,16 @@ impl GithubClient {
Self { client }
}

pub(crate) async fn mark_thread_as_done(&self, id: NotificationId) -> octocrab::Result<()> {
pub(crate) async fn mark_thread_as_done(&self, id: NotificationId) -> Result<(), GithubError> {
self.client
.activity()
.notifications()
.mark_as_read(id)
.await
.map_err(GithubError::from)
}

pub(crate) async fn unsubscribe_thread(&self, id: ThreadId) -> octocrab::Result<()> {
pub(crate) async fn unsubscribe_thread(&self, id: ThreadId) -> Result<(), GithubError> {
// The reasons for not using the `set_thread_subscription` method of `NotificationHandler` are twofold:
// 1. Since the API require the PUT method, but it is implemented using GET, it results in a "Not found" error.
// 2. During the deserialization of the `ThreadSubscription` response type, an empty string is assigned to the reason, causing an error when deserializing the `Reason` enum.
Expand Down Expand Up @@ -94,7 +120,7 @@ impl GithubClient {
include,
participating,
}: FetchNotificationsParams,
) -> octocrab::Result<Vec<Notification>> {
) -> Result<Vec<Notification>, GithubError> {
let mut page = self
.client
.activity()
Expand Down Expand Up @@ -138,7 +164,7 @@ impl GithubClient {
repository_key: RepositoryKey { name, owner },
..
}: NotificationContext<IssueId>,
) -> octocrab::Result<IssueContext> {
) -> Result<IssueContext, GithubError> {
let response: octocrab::Result<graphql_client::Response<issue_query::ResponseData>> = self
.client
.graphql(&IssueQuery::build_query(issue_query::Variables {
Expand All @@ -157,7 +183,7 @@ impl GithubClient {
(Some(data), _) => Ok(IssueContext::from(data)),
_ => unreachable!(),
},
Err(error) => Err(error),
Err(error) => Err(GithubError::from(error)),
}
}
}
Expand All @@ -180,7 +206,7 @@ impl GithubClient {
repository_key: RepositoryKey { name, owner },
..
}: NotificationContext<PullRequestId>,
) -> octocrab::Result<PullRequestContext> {
) -> Result<PullRequestContext, GithubError> {
let response: octocrab::Result<graphql_client::Response<pull_request_query::ResponseData>> =
self.client
.graphql(&PullRequestQuery::build_query(
Expand All @@ -201,7 +227,7 @@ impl GithubClient {
(Some(data), _) => Ok(PullRequestContext::from(data)),
_ => unreachable!(),
},
Err(error) => Err(error),
Err(error) => Err(GithubError::from(error)),
}
}
}
9 changes: 6 additions & 3 deletions crates/synd_term/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ use crate::{
application::{Direction, Populate, RequestSequence},
auth::{AuthenticationProvider, Credential, Verified},
client::{
github::FetchNotificationsParams, mutation::subscribe_feed::SubscribeFeedInput, payload,
query::subscription::SubscriptionOutput, SyndApiError,
github::{FetchNotificationsParams, GithubError},
mutation::subscribe_feed::SubscribeFeedInput,
payload,
query::subscription::SubscriptionOutput,
SyndApiError,
},
types::{
github::{
Expand Down Expand Up @@ -192,7 +195,7 @@ pub(crate) enum Command {
},
HandleGithubApiError {
// use Arc for impl Clone
error: Arc<octocrab::Error>,
error: Arc<GithubError>,
request_seq: RequestSequence,
},
}
Expand Down

0 comments on commit 0f9acbb

Please sign in to comment.