Skip to content

Commit

Permalink
Properly extract errors from the Anthropic API (#15534)
Browse files Browse the repository at this point in the history
Before, we missed "successful" responses with the API errors, now they
are properly shown in the assistant panel.


![image](https://github.com/user-attachments/assets/0c0936af-86c2-4def-9a58-25d5e0912b97)


Release Notes:

- N/A
  • Loading branch information
SomeoneToIgnore authored Jul 31, 2024
1 parent 821ce2f commit 9384f66
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 12 additions & 2 deletions crates/anthropic/src/anthropic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ pub async fn stream_completion(
let body_str = std::str::from_utf8(&body)?;

match serde_json::from_str::<Event>(body_str) {
Ok(Event::Error { error }) => Err(api_error_to_err(error)),
Ok(_) => Err(anyhow!(
"Unexpected success response while expecting an error: {}",
body_str,
"Unexpected success response while expecting an error: '{body_str}'",
)),
Err(_) => Err(anyhow!(
"Failed to connect to API: {} {}",
Expand All @@ -200,13 +200,23 @@ pub fn extract_text_from_events(
ContentDelta::TextDelta { text } => Some(Ok(text)),
_ => None,
},
Event::Error { error } => Some(Err(api_error_to_err(error))),
_ => None,
},
Err(error) => Some(Err(error)),
}
})
}

fn api_error_to_err(
ApiError {
error_type,
message,
}: ApiError,
) -> anyhow::Error {
anyhow!("API error. Type: '{error_type}', message: '{message}'",)
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Message {
pub role: Role,
Expand Down
6 changes: 3 additions & 3 deletions crates/assistant/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ impl ContextOperation {
}
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub enum ContextEvent {
MessagesEdited,
SummaryChanged,
Expand Down Expand Up @@ -2188,15 +2188,15 @@ impl ContextVersion {
}
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct PendingSlashCommand {
pub name: String,
pub argument: Option<String>,
pub status: PendingSlashCommandStatus,
pub source_range: Range<language::Anchor>,
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub enum PendingSlashCommandStatus {
Idle,
Running { _task: Shared<Task<()>> },
Expand Down

0 comments on commit 9384f66

Please sign in to comment.