Skip to content

fix: handle API error response bodies - #2

Open
zensh wants to merge 1 commit into
SpenserCai:mainfrom
ldclabs:fix-api-status-errors
Open

zensh wants to merge 1 commit into
SpenserCai:mainfrom
ldclabs:fix-api-status-errors

Conversation

@zensh

@zensh zensh commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Summary

  • check JSON response bodies for non-zero ret or errcode values on send, typing, and lifecycle notification endpoints
  • map the session expiration error code to the existing SessionExpired error
  • add unit coverage for success, generic API error, and session-expired response bodies

Tests

  • cargo fmt --check
  • cargo test
  • cargo clippy --all-targets -- -D warnings
  • git diff --check

Check JSON response bodies for non-zero ret or errcode values on send, typing, and lifecycle notification endpoints.

Map session expiration responses to the existing SessionExpired error so callers can apply the same cooldown behavior as polling.

Co-Authored-By: Anda Bot <noreply@anda.bot>
Copilot AI review requested due to automatic review settings June 6, 2026 05:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds centralized parsing of iLink API error codes from JSON responses and enforces API-level success checks for several endpoints.

Changes:

  • Introduced api_error_from_json / ensure_api_success helpers to interpret ret/errcode and map session-expired responses.
  • Updated send_message, send_typing, notify_start, and notify_stop to validate the JSON body indicates success.
  • Added unit tests covering success, generic API errors, and session-expired mapping.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/api/client.rs
Comment on lines +52 to +57
let errmsg = value
.get("errmsg")
.or_else(|| value.get("message"))
.or_else(|| value.get("msg"))
.and_then(serde_json::Value::as_str)
.map_or_else(|| value.to_string(), ToOwned::to_owned);
Comment thread src/api/client.rs
Comment on lines 294 to 320
/// Notify server that this bot is starting (best-effort).
pub async fn notify_start(&self) -> Result<()> {
let body = serde_json::json!({ "base_info": self.base_info() });
let _: serde_json::Value = self
let value: serde_json::Value = self
.post_json(
"ilink/bot/msg/notifystart",
&body,
Some(Duration::from_secs(10)),
)
.await?;
ensure_api_success(&value)?;
Ok(())
}

/// Notify server that this bot is stopping (best-effort).
pub async fn notify_stop(&self) -> Result<()> {
let body = serde_json::json!({ "base_info": self.base_info() });
let _: serde_json::Value = self
let value: serde_json::Value = self
.post_json(
"ilink/bot/msg/notifystop",
&body,
Some(Duration::from_secs(10)),
)
.await?;
ensure_api_success(&value)?;
Ok(())
}
Comment thread src/api/client.rs
Comment on lines +385 to +386
#[test]
fn api_status_accepts_success_body() {
Comment thread src/api/client.rs
Comment on lines +392 to +393
#[test]
fn api_status_rejects_nonzero_body() {
Comment thread src/api/client.rs
Comment on lines +406 to +407
#[test]
fn api_status_maps_session_expired_body() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants