Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions crates/lib/docs_rs_repository_stats/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
config::Config,
updater::{FetchRepositoriesResult, Repository, RepositoryForge, RepositoryName},
};
use anyhow::Result;
use anyhow::{Result, bail};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use docs_rs_utils::APP_USER_AGENT;
Expand Down Expand Up @@ -187,18 +187,24 @@ impl GitHub {
query: &str,
variables: impl serde::Serialize,
) -> Result<GraphResponse<T>> {
Ok(self
let response = self
.client
.post(&self.endpoint)
.json(&serde_json::json!({
"query": query,
"variables": variables,
}))
.send()
.await?
.error_for_status()?
.json()
.await?)
.await?;

let status = response.status();

if status.is_client_error() || status.is_server_error() {
let body = response.text().await?;
bail!("GitHub GraphQL response status: {}\n{}", status, body);
} else {
Ok(response.json().await?)
}
}
}

Expand Down Expand Up @@ -384,4 +390,31 @@ mod tests {
assert_eq!(repo.issues, 12);
Ok(())
}

#[tokio::test]
async fn test_403_error_with_body() -> Result<()> {
let config = github_config()?;
let (mut server, updater) = mock_server_and_github(&config).await;

let _m1 = server
.mock("POST", "/graphql")
.with_header("content-type", "application/json")
.with_status(403)
.with_body("some error text")
.create();

let err = updater
.fetch_repository(
&repository_name("https://gitlab.com/foo/bar").expect("repository_name failed"),
)
.await
.unwrap_err();

assert_eq!(
err.to_string(),
"GitHub GraphQL response status: 403 Forbidden\nsome error text"
);

Ok(())
}
}
Loading