Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
git_ui: Improve error handling in commit message generation (#29005)
Umesh Yadav3853e83da76c
diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rsindex fd18f6e7bf6..0dedfa16679 100644--- a/crates/git_ui/src/git_panel.rs+++ b/crates/git_ui/src/git_panel.rs@@ -27,7 +27,7 @@ use git::status::StageStatus;use git::{Amend, ToggleStaged, repository::RepoPath, status::FileStatus};use git::{ExpandCommitEditor, RestoreTrackedFiles, StageAll, TrashUntrackedFiles, UnstageAll};use gpui::{- Action, Animation, AnimationExt as _, AsyncWindowContext, Axis, ClickEvent, Corner,+ Action, Animation, AnimationExt as _, AsyncApp, AsyncWindowContext, Axis, ClickEvent, Corner,DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, KeyContext,ListHorizontalSizingBehavior, ListSizingBehavior, Modifiers, ModifiersChangedEvent,MouseButton, MouseDownEvent, Point, PromptLevel, ScrollStrategy, Subscription, Task,@@ -68,7 +68,7 @@ use util::{ResultExt, TryFutureExt, maybe};use workspace::{Workspace,dock::{DockPosition, Panel, PanelEvent},- notifications::DetachAndPromptErr,+ notifications::{DetachAndPromptErr, ErrorMessagePrompt, NotificationId},};use zed_llm_client::CompletionIntent;@@ -1779,7 +1779,19 @@ impl GitPanel {this.generate_commit_message_task.take();});- let mut diff_text = diff.await??;+ let mut diff_text = match diff.await {+ Ok(result) => match result {+ Ok(text) => text,+ Err(e) => {+ Self::show_commit_message_error(&this, &e, cx);+ return anyhow::Ok(());+ }+ },+ Err(e) => {+ Self::show_commit_message_error(&this, &e, cx);+ return anyhow::Ok(());+ }+ };const ONE_MB: usize = 1_000_000;if diff_text.len() > ONE_MB {@@ -1817,26 +1829,37 @@ impl GitPanel {};let stream = model.stream_completion_text(request, &cx);- let mut messages = stream.await?;-- if !text_empty {- this.update(cx, |this, cx| {- this.commit_message_buffer(cx).update(cx, |buffer, cx| {- let insert_position = buffer.anchor_before(buffer.len());- buffer.edit([(insert_position..insert_position, "\n")], None, cx)- });- })?;- }-- while let Some(message) = messages.stream.next().await {- let text = message?;+ match stream.await {+ Ok(mut messages) => {+ if !text_empty {+ this.update(cx, |this, cx| {+ this.commit_message_buffer(cx).update(cx, |buffer, cx| {+ let insert_position = buffer.anchor_before(buffer.len());+ buffer.edit([(insert_position..insert_position, "\n")], None, cx)+ });+ })?;+ }- this.update(cx, |this, cx| {- this.commit_message_buffer(cx).update(cx, |buffer, cx| {- let insert_position = buffer.anchor_before(buffer.len());- buffer.edit([(insert_position..insert_position, text)], None, cx);- });- })?;+ while let Some(message) = messages.stream.next().await {+ match message {+ Ok(text) => {+ this.update(cx, |this, cx| {+ this.commit_message_buffer(cx).update(cx, |buffer, cx| {+ let insert_position = buffer.anchor_before(buffer.len());+ buffer.edit([(insert_position..insert_position, text)], None, cx);+ });+ })?;+ }+ Err(e) => {+ Self::show_commit_message_error(&this, &e, cx);+ break;+ }+ }+ }+ }+ Err(e) => {+ Self::show_commit_message_error(&this, &e, cx);+ }}anyhow::Ok(())@@ -2694,6 +2717,26 @@ impl GitPanel {}}+ fn show_commit_message_error<E>(weak_this: &WeakEntity<Self>, err: &E, cx: &mut AsyncApp)+ where+ E: std::fmt::Debug + std::fmt::Display,+ {+ if let Ok(Some(workspace)) = weak_this.update(cx, |this, _cx| this.workspace.upgrade()) {+ let _ = workspace.update(cx, |workspace, cx| {+ struct CommitMessageError;+ let notification_id = NotificationId::unique::<CommitMessageError>();+ workspace.show_notification(notification_id, cx, |cx| {+ cx.new(|cx| {+ ErrorMessagePrompt::new(+ format!("Failed to generate commit message: {err}"),+ cx,+ )+ })+ });+ });+ }+ }+fn show_remote_output(&self, action: RemoteAction, info: RemoteCommandOutput, cx: &mut App) {let Some(workspace) = self.workspace.upgrade() else {return;