Skip to content

Commit 6ffe4ad

Browse files
neckarosclaude
andcommitted
fix: improve GitHub API error handling for plugin repo upload
Handle cases where the repository has no releases by checking HTTP status before parsing the response body. Provides clear error messages instead of cryptic "missing field 'assets'" deserialization errors. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 1a40c46 commit 6ffe4ad

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

src/tools/http_tools.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use mime_guess::MimeGuess;
44
use nanoid::nanoid;
55
use reqwest::Client;
66
use serde::Deserialize;
7+
use serde_json;
78
use tokio::{fs::{self, File}, io::AsyncWriteExt};
89

910
use crate::{error::RsResult, tools::file_tools::get_extension_from_mime};
@@ -46,6 +47,11 @@ pub struct GithubRelease {
4647
assets: Vec<GithubAsset>,
4748
}
4849

50+
#[derive(Deserialize, Debug)]
51+
pub struct GithubError {
52+
message: String,
53+
}
54+
4955
#[derive(Deserialize, Debug)]
5056
pub struct GithubAsset {
5157
name: String,
@@ -78,14 +84,25 @@ pub async fn download_latest_wasm(repo_url: &str, download_dir: &str, filename:
7884
.user_agent("RedseatRustApp/1.0") // Required by GitHub API
7985
.build()?;
8086
let api_url = format!("https://api.github.com/repos/{}/{}/releases/latest", owner, repo);
81-
let release: GithubRelease = client
87+
let response = client
8288
.get(&api_url)
8389
.header("Accept", "application/vnd.github.v3+json")
8490
.send()
85-
.await?
86-
.json()
8791
.await?;
8892

93+
let status = response.status();
94+
let body = response.text().await?;
95+
96+
if !status.is_success() {
97+
let error_msg = serde_json::from_str::<GithubError>(&body)
98+
.map(|e| e.message)
99+
.unwrap_or_else(|_| body.clone());
100+
return Err(crate::error::Error::Error(format!("GitHub API error ({}): {}", status, error_msg)));
101+
}
102+
103+
let release: GithubRelease = serde_json::from_str(&body)
104+
.map_err(|e| crate::error::Error::Error(format!("Failed to parse GitHub release: {}. The repository may not have any releases.", e)))?;
105+
89106
// Step 2: Find first .wasm asset
90107
let wasm_asset = release.assets.iter()
91108
.find(|asset| asset.name.ends_with(".wasm"))

0 commit comments

Comments
 (0)