From caad55350252793b53aaf699062644a2cddc6a08 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 15 Apr 2024 08:25:17 +0200 Subject: Remove ErrorMsg There is no reason any more for application code to show error messages in a panel. Just return an error instead. --- pkg/gui/context/menu_context.go | 4 +++- pkg/gui/controllers/basic_commits_controller.go | 3 ++- pkg/gui/controllers/branches_controller.go | 14 ++++++------- pkg/gui/controllers/commit_message_controller.go | 4 +++- pkg/gui/controllers/commits_files_controller.go | 3 ++- .../custom_patch_options_menu_action.go | 7 ++++--- pkg/gui/controllers/files_controller.go | 23 +++++++++++----------- pkg/gui/controllers/git_flow_controller.go | 3 ++- pkg/gui/controllers/helpers/commits_helper.go | 3 ++- pkg/gui/controllers/helpers/fixup_helper.go | 16 ++++++++------- .../controllers/helpers/merge_and_rebase_helper.go | 11 ++++++----- .../controllers/helpers/patch_building_helper.go | 4 +++- pkg/gui/controllers/helpers/repos_helper.go | 3 ++- pkg/gui/controllers/helpers/snake_helper.go | 3 ++- pkg/gui/controllers/helpers/update_helper.go | 6 ++++-- pkg/gui/controllers/helpers/working_tree_helper.go | 7 ++++--- pkg/gui/controllers/helpers/worktree_helper.go | 7 ++++--- pkg/gui/controllers/list_controller_trait.go | 12 +++++++---- pkg/gui/controllers/sync_controller.go | 5 +++-- pkg/gui/controllers/toggle_whitespace_action.go | 4 +++- pkg/gui/controllers/undo_controller.go | 5 +++-- pkg/gui/controllers/workspace_reset_controller.go | 3 ++- pkg/gui/controllers/worktrees_controller.go | 5 +++-- pkg/gui/keybindings.go | 3 ++- pkg/gui/popup/popup_handler.go | 6 +----- .../services/custom_commands/handler_creator.go | 3 ++- pkg/gui/types/common.go | 4 ---- 27 files changed, 98 insertions(+), 73 deletions(-) diff --git a/pkg/gui/context/menu_context.go b/pkg/gui/context/menu_context.go index 6d87d2bb7..e4c3d7280 100644 --- a/pkg/gui/context/menu_context.go +++ b/pkg/gui/context/menu_context.go @@ -1,6 +1,8 @@ package context import ( + "errors" + "github.com/jesseduffield/lazygit/pkg/gui/keybindings" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/types" @@ -148,7 +150,7 @@ func (self *MenuContext) GetKeybindings(opts types.KeybindingsOpts) []*types.Bin func (self *MenuContext) OnMenuPress(selectedItem *types.MenuItem) error { if selectedItem != nil && selectedItem.DisabledReason != nil { if selectedItem.DisabledReason.ShowErrorInPanel { - return self.c.ErrorMsg(selectedItem.DisabledReason.Text) + return errors.New(selectedItem.DisabledReason.Text) } self.c.ErrorToast(self.c.Tr.DisabledMenuItemPrefix + selectedItem.DisabledReason.Text) diff --git a/pkg/gui/controllers/basic_commits_controller.go b/pkg/gui/controllers/basic_commits_controller.go index 73d069029..22e3da9d5 100644 --- a/pkg/gui/controllers/basic_commits_controller.go +++ b/pkg/gui/controllers/basic_commits_controller.go @@ -1,6 +1,7 @@ package controllers import ( + "errors" "fmt" "github.com/jesseduffield/lazygit/pkg/commands/git_commands" @@ -314,7 +315,7 @@ func (self *BasicCommitsController) handleOldCherryPickKey() error { "paste": keybindings.Label(self.c.UserConfig.Keybinding.Commits.PasteCommits), }) - return self.c.ErrorMsg(msg) + return errors.New(msg) } func (self *BasicCommitsController) openDiffTool(commit *models.Commit) error { diff --git a/pkg/gui/controllers/branches_controller.go b/pkg/gui/controllers/branches_controller.go index 0091f1c77..b08ddd0cd 100644 --- a/pkg/gui/controllers/branches_controller.go +++ b/pkg/gui/controllers/branches_controller.go @@ -334,7 +334,7 @@ func (self *BranchesController) context() *context.BranchesContext { func (self *BranchesController) press(selectedBranch *models.Branch) error { if selectedBranch == self.c.Helpers().Refs.GetCheckedOutRef() { - return self.c.ErrorMsg(self.c.Tr.AlreadyCheckedOutBranch) + return errors.New(self.c.Tr.AlreadyCheckedOutBranch) } worktreeForRef, ok := self.worktreeForBranch(selectedBranch) @@ -378,7 +378,7 @@ func (self *BranchesController) promptToCheckoutWorktree(worktree *models.Worktr func (self *BranchesController) handleCreatePullRequest(selectedBranch *models.Branch) error { if !selectedBranch.IsTrackingRemote() { - return self.c.ErrorMsg(self.c.Tr.PullRequestNoUpstream) + return errors.New(self.c.Tr.PullRequestNoUpstream) } return self.createPullRequest(selectedBranch.UpstreamBranch, "") } @@ -548,7 +548,7 @@ func (self *BranchesController) forceDelete(branch *models.Branch) error { Prompt: message, HandleConfirm: func() error { if err := self.c.Git().Branch.LocalDelete(branch.Name, true); err != nil { - return self.c.ErrorMsg(err.Error()) + return err } return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.BRANCHES}}) }, @@ -615,13 +615,13 @@ func (self *BranchesController) notRebasingOntoSelf(branch *models.Branch) *type func (self *BranchesController) fastForward(branch *models.Branch) error { if !branch.IsTrackingRemote() { - return self.c.ErrorMsg(self.c.Tr.FwdNoUpstream) + return errors.New(self.c.Tr.FwdNoUpstream) } if !branch.RemoteBranchStoredLocally() { - return self.c.ErrorMsg(self.c.Tr.FwdNoLocalUpstream) + return errors.New(self.c.Tr.FwdNoLocalUpstream) } if branch.HasCommitsToPush() { - return self.c.ErrorMsg(self.c.Tr.FwdCommitsToPush) + return errors.New(self.c.Tr.FwdCommitsToPush) } action := self.c.Tr.Actions.FastForwardBranch @@ -766,7 +766,7 @@ func (self *BranchesController) createPullRequestMenu(selectedBranch *models.Bra LabelColumns: fromToLabelColumns(checkedOutBranch.Name, selectedBranch.Name), OnPress: func() error { if !checkedOutBranch.IsTrackingRemote() || !selectedBranch.IsTrackingRemote() { - return self.c.ErrorMsg(self.c.Tr.PullRequestNoUpstream) + return errors.New(self.c.Tr.PullRequestNoUpstream) } return self.createPullRequest(checkedOutBranch.UpstreamBranch, selectedBranch.UpstreamBranch) }, diff --git a/pkg/gui/controllers/commit_message_controller.go b/pkg/gui/controllers/commit_message_controller.go index 84e553d87..4012dc950 100644 --- a/pkg/gui/controllers/commit_message_controller.go +++ b/pkg/gui/controllers/commit_message_controller.go @@ -1,6 +1,8 @@ package controllers import ( + "errors" + "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" @@ -114,7 +116,7 @@ func (self *CommitMessageController) setCommitMessageAtIndex(index int) (bool, e if err == git_commands.ErrInvalidCommitIndex { return false, nil } - return false, self.c.ErrorMsg(self.c.Tr.CommitWithoutMessageErr) + return false, errors.New(self.c.Tr.CommitWithoutMessageErr) } if self.c.UserConfig.Git.Commit.AutoWrapCommitMessage { commitMessage = helpers.TryRemoveHardLineBreaks(commitMessage, self.c.UserConfig.Git.Commit.AutoWrapWidth) diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index 3c13b591d..2b2c709f3 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -1,6 +1,7 @@ package controllers import ( + "errors" "strings" "github.com/jesseduffield/gocui" @@ -179,7 +180,7 @@ func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error func (self *CommitFilesController) discard(selectedNodes []*filetree.CommitFileNode) error { parentContext, ok := self.c.CurrentContext().GetParentContext() if !ok || parentContext.GetKey() != context.LOCAL_COMMITS_CONTEXT_KEY { - return self.c.ErrorMsg(self.c.Tr.CanOnlyDiscardFromLocalCommits) + return errors.New(self.c.Tr.CanOnlyDiscardFromLocalCommits) } if ok, err := self.c.Helpers().PatchBuilding.ValidateNormalWorkingTreeState(); !ok { diff --git a/pkg/gui/controllers/custom_patch_options_menu_action.go b/pkg/gui/controllers/custom_patch_options_menu_action.go index c3defb380..2d57f0ac0 100644 --- a/pkg/gui/controllers/custom_patch_options_menu_action.go +++ b/pkg/gui/controllers/custom_patch_options_menu_action.go @@ -1,6 +1,7 @@ package controllers import ( + "errors" "fmt" "github.com/jesseduffield/gocui" @@ -15,11 +16,11 @@ type CustomPatchOptionsMenuAction struct { func (self *CustomPatchOptionsMenuAction) Call() error { if !self.c.Git().Patch.PatchBuilder.Active() { - return self.c.ErrorMsg(self.c.Tr.NoPatchError) + return errors.New(self.c.Tr.NoPatchError) } if self.c.Git().Patch.PatchBuilder.IsEmpty() { - return self.c.ErrorMsg(self.c.Tr.EmptyPatchError) + return errors.New(self.c.Tr.EmptyPatchError) } menuItems := []*types.MenuItem{ @@ -115,7 +116,7 @@ func (self *CustomPatchOptionsMenuAction) getPatchCommitIndex() int { func (self *CustomPatchOptionsMenuAction) validateNormalWorkingTreeState() (bool, error) { if self.c.Git().Status.WorkingTreeState() != enums.REBASE_MODE_NONE { - return false, self.c.ErrorMsg(self.c.Tr.CantPatchWhileRebasingError) + return false, errors.New(self.c.Tr.CantPatchWhileRebasingError) } return true, nil } diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go index eeaae31a6..b71991fc2 100644 --- a/pkg/gui/controllers/files_controller.go +++ b/pkg/gui/controllers/files_controller.go @@ -1,6 +1,7 @@ package controllers import ( + "errors" "strings" "github.com/jesseduffield/gocui" @@ -388,7 +389,7 @@ func (self *FilesController) pressWithLock(selectedNodes []*filetree.FileNode) e // if any files within have inline merge conflicts we can't stage or unstage, // or it'll end up with those >>>>>> lines actually staged if node.GetHasInlineMergeConflicts() { - return self.c.ErrorMsg(self.c.Tr.ErrStageDirWithInlineMergeConflicts) + return errors.New(self.c.Tr.ErrStageDirWithInlineMergeConflicts) } } @@ -496,7 +497,7 @@ func (self *FilesController) EnterFile(opts types.OnFocusOpts) error { return self.switchToMerge() } if file.HasMergeConflicts { - return self.c.ErrorMsg(self.c.Tr.FileStagingRequirements) + return errors.New(self.c.Tr.FileStagingRequirements) } return self.c.PushContext(self.c.Contexts().Staging, opts) @@ -523,7 +524,7 @@ func (self *FilesController) toggleStagedAllWithLock() error { // if any files within have inline merge conflicts we can't stage or unstage, // or it'll end up with those >>>>>> lines actually staged if root.GetHasInlineMergeConflicts() { - return self.c.ErrorMsg(self.c.Tr.ErrStageDirWithInlineMergeConflicts) + return errors.New(self.c.Tr.ErrStageDirWithInlineMergeConflicts) } if root.GetHasUnstagedChanges() { @@ -606,14 +607,14 @@ func (self *FilesController) ignoreOrExcludeFile(node *filetree.FileNode, trText func (self *FilesController) ignore(node *filetree.FileNode) error { if node.GetPath() == ".gitignore" { - return self.c.ErrorMsg(self.c.Tr.Actions.IgnoreFileErr) + return errors.New(self.c.Tr.Actions.IgnoreFileErr) } return self.ignoreOrExcludeFile(node, self.c.Tr.IgnoreTracked, self.c.Tr.IgnoreTrackedPrompt, self.c.Tr.Actions.IgnoreExcludeFile, self.c.Git().WorkingTree.Ignore) } func (self *FilesController) exclude(node *filetree.FileNode) error { if node.GetPath() == ".gitignore" { - return self.c.ErrorMsg(self.c.Tr.Actions.ExcludeGitIgnoreErr) + return errors.New(self.c.Tr.Actions.ExcludeGitIgnoreErr) } return self.ignoreOrExcludeFile(node, self.c.Tr.ExcludeTracked, self.c.Tr.ExcludeTrackedPrompt, self.c.Tr.Actions.ExcludeFile, self.c.Git().WorkingTree.Exclude) @@ -658,7 +659,7 @@ func (self *FilesController) handleAmendCommitPress() error { HandleConfirm: func() error { return self.c.Helpers().WorkingTree.WithEnsureCommitableFiles(func() error { if len(self.c.Model().Commits) == 0 { - return self.c.ErrorMsg(self.c.Tr.NoCommitToAmend) + return errors.New(self.c.Tr.NoCommitToAmend) } return self.c.Helpers().AmendHelper.AmendHead() @@ -765,7 +766,7 @@ func (self *FilesController) createStashMenu() error { Label: self.c.Tr.StashAllChanges, OnPress: func() error { if !self.c.Helpers().WorkingTree.IsWorkingTreeDirty() { - return self.c.ErrorMsg(self.c.Tr.NoFilesToStash) + return errors.New(self.c.Tr.NoFilesToStash) } return self.handleStashSave(self.c.Git().Stash.Push, self.c.Tr.Actions.StashAllChanges) }, @@ -775,7 +776,7 @@ func (self *FilesController) createStashMenu() error { Label: self.c.Tr.StashAllChangesKeepIndex, OnPress: func() error { if !self.c.Helpers().WorkingTree.IsWorkingTreeDirty() { - return self.c.ErrorMsg(self.c.Tr.NoFilesToStash) + return errors.New(self.c.Tr.NoFilesToStash) } // if there are no staged files it behaves the same as Stash.Save return self.handleStashSave(self.c.Git().Stash.StashAndKeepIndex, self.c.Tr.Actions.StashAllChangesKeepIndex) @@ -794,7 +795,7 @@ func (self *FilesController) createStashMenu() error { OnPress: func() error { // there must be something in staging otherwise the current implementation mucks the stash up if !self.c.Helpers().WorkingTree.AnyStagedFiles() { - return self.c.ErrorMsg(self.c.Tr.NoTrackedStagedFilesStash) + return errors.New(self.c.Tr.NoTrackedStagedFilesStash) } return self.handleStashSave(self.c.Git().Stash.SaveStagedChanges, self.c.Tr.Actions.StashStagedChanges) }, @@ -804,7 +805,7 @@ func (self *FilesController) createStashMenu() error { Label: self.c.Tr.StashUnstagedChanges, OnPress: func() error { if !self.c.Helpers().WorkingTree.IsWorkingTreeDirty() { - return self.c.ErrorMsg(self.c.Tr.NoFilesToStash) + return errors.New(self.c.Tr.NoFilesToStash) } if self.c.Helpers().WorkingTree.AnyStagedFiles() { return self.handleStashSave(self.c.Git().Stash.StashUnstagedChanges, self.c.Tr.Actions.StashUnstagedChanges) @@ -986,7 +987,7 @@ func (self *FilesController) fetchAux(task gocui.Task) (err error) { err = self.c.Git().Sync.Fetch(task) if err != nil && strings.Contains(err.Error(), "exit status 128") { - _ = self.c.ErrorMsg(self.c.Tr.PassUnameWrong) + return errors.New(self.c.Tr.PassUnameWrong) } _ = self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.BRANCHES, types.COMMITS, types.REMOTES, types.TAGS}, Mode: types.ASYNC}) diff --git a/pkg/gui/controllers/git_flow_controller.go b/pkg/gui/controllers/git_flow_controller.go index 1cf123a00..7c3e45603 100644 --- a/pkg/gui/controllers/git_flow_controller.go +++ b/pkg/gui/controllers/git_flow_controller.go @@ -1,6 +1,7 @@ package controllers import ( + "errors" "fmt" "github.com/jesseduffield/lazygit/pkg/commands/models" @@ -46,7 +47,7 @@ func (self *GitFlowController) GetKeybindings(opts types.KeybindingsOpts) []*typ func (self *GitFlowController) handleCreateGitFlowMenu(branch *models.Branch) error { if !self.c.Git().Flow.GitFlowEnabled() { - return self.c.ErrorMsg("You need to install git-flow and enable it in this repo to use git-flow features") + return errors.New("You need to install git-flow and enable it in this repo to use git-flow features") } startHandler := func(branchType string) func() error { diff --git a/pkg/gui/controllers/helpers/commits_helper.go b/pkg/gui/controllers/helpers/commits_helper.go index 8f8904808..6e1a181c7 100644 --- a/pkg/gui/controllers/helpers/commits_helper.go +++ b/pkg/gui/controllers/helpers/commits_helper.go @@ -1,6 +1,7 @@ package helpers import ( + "errors" "path/filepath" "strings" "time" @@ -167,7 +168,7 @@ func (self *CommitsHelper) HandleCommitConfirm() error { summary, description := self.getCommitSummary(), self.getCommitDescription() if summary == "" { - return self.c.ErrorMsg(self.c.Tr.CommitWithoutMessageErr) + return errors.New(self.c.Tr.CommitWithoutMessageErr) } err := self.c.Contexts().CommitMessage.OnConfirm(summary, description) diff --git a/pkg/gui/controllers/helpers/fixup_helper.go b/pkg/gui/controllers/helpers/fixup_helper.go index 79104120e..b60d48f4f 100644 --- a/pkg/gui/controllers/helpers/fixup_helper.go +++ b/pkg/gui/controllers/helpers/fixup_helper.go @@ -1,6 +1,8 @@ package helpers import ( + "errors" + "fmt" "regexp" "strings" "sync" @@ -36,19 +38,19 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error { return err } if diff == "" { - return self.c.ErrorMsg(self.c.Tr.NoChangedFiles) + return errors.New(self.c.Tr.NoChangedFiles) } deletedLineInfos, hasHunksWithOnlyAddedLines := self.parseDiff(diff) if len(deletedLineInfos) == 0 { - return self.c.ErrorMsg(self.c.Tr.NoDeletedLinesInDiff) + return errors.New(self.c.Tr.NoDeletedLinesInDiff) } hashes := self.blameDeletedLines(deletedLineInfos) if len(hashes) == 0 { // This should never happen - return self.c.ErrorMsg(self.c.Tr.NoBaseCommitsFound) + return errors.New(self.c.Tr.NoBaseCommitsFound) } if len(hashes) > 1 { subjects, err := self.c.Git().Commit.GetHashesAndCommitMessagesFirstLine(hashes) @@ -58,7 +60,7 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error { message := lo.Ternary(hasStagedChanges, self.c.Tr.MultipleBaseCommitsFoundStaged, self.c.Tr.MultipleBaseCommitsFoundUnstaged) - return self.c.ErrorMsg(message + "\n\n" + subjects) + return fmt.Errorf("%s\n\n%s", message, subjects) } commit, index, ok := lo.FindIndexOf(self.c.Model().Commits, func(commit *models.Commit) bool { @@ -70,13 +72,13 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error { // If the commit is not found, it's most likely because it's already // merged, and more than 300 commits away. Check if the last known // commit is already merged; if so, show the "already merged" error. - return self.c.ErrorMsg(self.c.Tr.BaseCommitIsAlreadyOnMainBranch) + return errors.New(self.c.Tr.BaseCommitIsAlreadyOnMainBranch) } // If we get here, the current branch must have more then 300 commits. Unlikely... - return self.c.ErrorMsg(self.c.Tr.BaseCommitIsNotInCurrentView) + return errors.New(self.c.Tr.BaseCommitIsNotInCurrentView) } if commit.Status == models.StatusMerged { - return self.c.ErrorMsg(self.c.Tr.BaseCommitIsAlreadyOnMainBranch) + return errors.New(self.c.Tr.BaseCommitIsAlreadyOnMainBranch) } doIt := func() error { diff --git a/pkg/gui/controllers/helpers/merge_and_rebase_helper.go b/pkg/gui/controllers/helpers/merge_and_rebase_helper.go index 16b1eea2b..4bffcfa99 100644 --- a/pkg/gui/controllers/helpers/merge_and_rebase_helper.go +++ b/pkg/gui/controllers/helpers/merge_and_rebase_helper.go @@ -1,6 +1,7 @@ package helpers import ( + "errors" "fmt" "os" "path/filepath" @@ -78,7 +79,7 @@ func (self *MergeAndRebaseHelper) genericMergeCommand(command string) error { status := self.c.Git().Status.WorkingTreeState() if status != enums.REBASE_MODE_MERGING && status != enums.REBASE_MODE_REBASING { - return self.c.ErrorMsg(self.c.Tr.NotMergingOrRebasing) + return errors.New(self.c.Tr.NotMergingOrRebasing) } self.c.LogAction(fmt.Sprintf("Merge/Rebase: %s", command)) @@ -169,9 +170,9 @@ func (self *MergeAndRebaseHelper) CheckForConflicts(result error) error { if isMergeConflictErr(result.Error()) { return self.PromptForConflictHandling() - } else { - return self.c.ErrorMsg(result.Error()) } + + return result } func (self *MergeAndRebaseHelper) PromptForConflictHandling() error { @@ -298,11 +299,11 @@ func (self *MergeAndRebaseHelper) RebaseOntoRef(ref string) error { func (self *MergeAndRebaseHelper) MergeRefIntoCheckedOutBranch(refName string) error { if self.c.Git().Branch.IsHeadDetached() { - return self.c.ErrorMsg("Cannot merge branch in detached head state. You might have checked out a commit directly or a remote branch, in which case you should checkout the local branch you want to be on") + return errors.New("Cannot merge branch in detached head state. You might have checked out a commit directly or a remote branch, in which case you should checkout the local branch you want to be on") } checkedOutBranchName := self.refsHelper.GetCheckedOutRef().Name if checkedOutBranchName == refName { - return self.c.ErrorMsg(self.c.Tr.CantMergeBranchIntoItself) + return errors.New(self.c.Tr.CantMergeBranchIntoItself) } prompt := utils.ResolvePlaceholderString( self.c.Tr.ConfirmMerge, diff --git a/pkg/gui/controllers/helpers/patch_building_helper.go b/pkg/gui/controllers/helpers/patch_building_helper.go index a9fe4f6da..d8f83255d 100644 --- a/pkg/gui/controllers/helpers/patch_building_helper.go +++ b/pkg/gui/controllers/helpers/patch_building_helper.go @@ -1,6 +1,8 @@ package helpers import ( + "errors" + "github.com/jesseduffield/lazygit/pkg/commands/types/enums" "github.com/jesseduffield/lazygit/pkg/gui/patch_exploring" "github.com/jesseduffield/lazygit/pkg/gui/types" @@ -24,7 +26,7 @@ func NewPatchBuildingHelper( func (self *PatchBuildingHelper) ValidateNormalWorkingTreeState() (bool, error) { if self.c.Git().Status.WorkingTreeState() != enums.REBASE_MODE_NONE { - return false, self.c.ErrorMsg(self.c.Tr.CantPatchWhileRebasingError) + return false, errors.New(self.c.Tr.CantPatchWhileRebasingError) } return true, nil } diff --git a/pkg/gui/controllers/helpers/repos_helper.go b/pkg/gui/controllers/helpers/repos_helper.go index 22883510a..decebba36 100644 --- a/pkg/gui/controllers/helpers/repos_helper.go +++ b/pkg/gui/controllers/helpers/repos_helper.go @@ -1,6 +1,7 @@ package helpers import ( + "errors" "fmt" "os" "path/filepath" @@ -156,7 +157,7 @@ func (self *ReposHelper) DispatchSwitchTo(path string, errMsg string, contextKey if err := os.Chdir(path); err != nil { if os.IsNotExist(err) { - return self.c.ErrorMsg(errMsg) + return errors.New(errMsg) } return err } diff --git a/pkg/gui/controllers/helpers/snake_helper.go b/pkg/gui/controllers/helpers/snake_helper.go index fb4d67bb4..6940bbd02 100644 --- a/pkg/gui/controllers/helpers/snake_helper.go +++ b/pkg/gui/controllers/helpers/snake_helper.go @@ -1,6 +1,7 @@ package helpers import ( + "errors" "fmt" "strings" @@ -39,7 +40,7 @@ func (self *SnakeHelper) renderSnakeGame(cells [][]snake.CellType, alive bool) { view := self.c.Views().Snake if !alive { - _ = self.c.ErrorMsg(self.c.Tr.YouDied) + self.c.OnUIThread(func() error { return errors.New(self.c.Tr.YouDied) }) return } diff --git a/pkg/gui/controllers/helpers/update_helper.go b/pkg/gui/controllers/helpers/update_helper.go index 511d72dc4..f50a757de 100644 --- a/pkg/gui/controllers/helpers/update_helper.go +++ b/pkg/gui/controllers/helpers/update_helper.go @@ -1,6 +1,8 @@ package helpers import ( + "errors" + "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/updates" @@ -44,7 +46,7 @@ func (self *UpdateHelper) CheckForUpdateInForeground() error { return err } if newVersion == "" { - return self.c.ErrorMsg(self.c.Tr.FailedToRetrieveLatestVersionErr) + return errors.New(self.c.Tr.FailedToRetrieveLatestVersionErr) } return self.showUpdatePrompt(newVersion) }, true) @@ -71,7 +73,7 @@ func (self *UpdateHelper) onUpdateFinish(err error) error { "errMessage": err.Error(), }, ) - return self.c.ErrorMsg(errMessage) + return errors.New(errMessage) } return self.c.Alert(self.c.Tr.UpdateCompletedTitle, self.c.Tr.UpdateCompleted) }) diff --git a/pkg/gui/controllers/helpers/working_tree_helper.go b/pkg/gui/controllers/helpers/working_tree_helper.go index b26801c8c..edd7005e9 100644 --- a/pkg/gui/controllers/helpers/working_tree_helper.go +++ b/pkg/gui/controllers/helpers/working_tree_helper.go @@ -1,6 +1,7 @@ package helpers import ( + "errors" "fmt" "regexp" @@ -137,7 +138,7 @@ func (self *WorkingTreeHelper) HandleCommitEditorPress() error { func (self *WorkingTreeHelper) HandleWIPCommitPress() error { skipHookPrefix := self.c.UserConfig.Git.SkipHookPrefix if skipHookPrefix == "" { - return self.c.ErrorMsg(self.c.Tr.SkipHookPrefixNotConfigured) + return errors.New(self.c.Tr.SkipHookPrefixNotConfigured) } return self.HandleCommitPressWithMessage(skipHookPrefix) @@ -153,7 +154,7 @@ func (self *WorkingTreeHelper) HandleCommitPress() error { prefixReplace := commitPrefixConfig.Replace rgx, err := regexp.Compile(prefixPattern) if err != nil { - return self.c.ErrorMsg(fmt.Sprintf("%s: %s", self.c.Tr.CommitPrefixPatternError, err.Error())) + return fmt.Errorf("%s: %s", self.c.Tr.CommitPrefixPatternError, err.Error()) } prefix := rgx.ReplaceAllString(self.refHelper.GetCheckedOutRef().Name, prefixReplace) message = prefix @@ -169,7 +170,7 @@ func (self *WorkingTreeHelper) WithEnsureCommitableFiles(handler func() error) e } if len(self.c.Model().Files) == 0 { - return self.c.ErrorMsg(self.c.Tr.NoFilesStagedTitle) + return errors.New(self.c.Tr.NoFilesStagedTitle) } if !self.AnyStagedFiles() { diff --git a/pkg/gui/controllers/helpers/worktree_helper.go b/pkg/gui/controllers/helpers/worktree_helper.go index 02df23398..7c763e0fb 100644 --- a/pkg/gui/controllers/helpers/worktree_helper.go +++ b/pkg/gui/controllers/helpers/worktree_helper.go @@ -1,6 +1,7 @@ package helpers import ( + "errors" "strings" "github.com/jesseduffield/gocui" @@ -139,7 +140,7 @@ func (self *WorktreeHelper) NewWorktreeCheckout(base string, canCheckoutBase boo Title: self.c.Tr.NewBranchName, HandleConfirm: func(branchName string) error { if branchName == "" { - return self.c.ErrorMsg(self.c.Tr.BranchNameCannotBeBlank) + return errors.New(self.c.Tr.BranchNameCannotBeBlank) } opts.Branch = branchName @@ -154,7 +155,7 @@ func (self *WorktreeHelper) NewWorktreeCheckout(base string, canCheckoutBase boo func (self *WorktreeHelper) Switch(worktree *models.Worktree, contextKey types.ContextKey) error { if worktree.IsCurrent { - return self.c.ErrorMsg(self.c.Tr.AlreadyInWorktree) + return errors.New(self.c.Tr.AlreadyInWorktree) } self.c.LogAction(self.c.Tr.SwitchToWorktree) @@ -192,7 +193,7 @@ func (self *WorktreeHelper) Remove(worktree *models.Worktree, force bool) error if !force { return self.Remove(worktree, true) } - return self.c.ErrorMsg(errMessage) + return err } return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.WORKTREES, types.BRANCHES, types.FILES}}) }) diff --git a/pkg/gui/controllers/list_controller_trait.go b/pkg/gui/controllers/list_controller_trait.go index 0edaa0114..fa5fc1492 100644 --- a/pkg/gui/controllers/list_controller_trait.go +++ b/pkg/gui/controllers/list_controller_trait.go @@ -1,6 +1,10 @@ package controllers -import "github.com/jesseduffield/lazygit/pkg/gui/types" +import ( + "errors" + + "github.com/jesseduffield/lazygit/pkg/gui/types" +) // Embed this into your list controller to get some convenience methods for // ensuring a single item is selected, etc. @@ -106,7 +110,7 @@ func (self *ListControllerTrait[T]) withItem(callback func(T) error) func() erro var zeroValue T commit := self.getSelectedItem() if commit == zeroValue { - return self.c.ErrorMsg(self.c.Tr.NoItemSelected) + return errors.New(self.c.Tr.NoItemSelected) } return callback(commit) @@ -117,7 +121,7 @@ func (self *ListControllerTrait[T]) withItems(callback func([]T) error) func() e return func() error { items, _, _ := self.getSelectedItems() if len(items) == 0 { - return self.c.ErrorMsg(self.c.Tr.NoItemSelected) + return errors.New(self.c.Tr.NoItemSelected) } return callback(items) @@ -129,7 +133,7 @@ func (self *ListControllerTrait[T]) withItemsRange(callback func([]T, int, int) return func() error { items, startIdx, endIdx := self.getSelectedItems() if len(items) == 0 { - return self.c.ErrorMsg(self.c.Tr.NoItemSelected) + return errors.New(self.c.Tr.NoItemSelected) } return callback(items, startIdx, endIdx) diff --git a/pkg/gui/controllers/sync_controller.go b/pkg/gui/controllers/sync_controller.go index 00d35636e..403f31d94 100644 --- a/pkg/gui/controllers/sync_controller.go +++ b/pkg/gui/controllers/sync_controller.go @@ -1,6 +1,7 @@ package controllers import ( + "errors" "fmt" "strings" @@ -197,7 +198,7 @@ func (self *SyncController) pushAux(currentBranch *models.Branch, opts pushOpts) }) if err != nil { if strings.Contains(err.Error(), "Updates were rejected") { - return self.c.ErrorMsg(self.c.Tr.UpdatesRejected) + return errors.New(self.c.Tr.UpdatesRejected) } return err } @@ -208,7 +209,7 @@ func (self *SyncController) pushAux(currentBranch *models.Branch, opts pushOpts) func (self *SyncController) requestToForcePush(currentBranch *models.Branch, opts pushOpts) error { forcePushDisabled := self.c.UserConfig.Git.DisableForcePushing if forcePushDisabled { - return self.c.ErrorMsg(self.c.Tr.ForcePushDisabled) + return errors.New(self.c.Tr.ForcePushDisabled) } return self.c.Confirm(types.ConfirmOpts{ diff --git a/pkg/gui/controllers/toggle_whitespace_action.go b/pkg/gui/controllers/toggle_whitespace_action.go index 175738b59..41232a769 100644 --- a/pkg/gui/controllers/toggle_whitespace_action.go +++ b/pkg/gui/controllers/toggle_whitespace_action.go @@ -1,6 +1,8 @@ package controllers import ( + "errors" + "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/samber/lo" @@ -20,7 +22,7 @@ func (self *ToggleWhitespaceAction) Call() error { if lo.Contains(contextsThatDontSupportIgnoringWhitespace, self.c.CurrentContext().GetKey()) { // Ignoring whitespace is not supported in these views. Let the user // know that it's not going to work in case they try to turn it on. - return self.c.ErrorMsg(self.c.Tr.IgnoreWhitespaceNotSupportedHere) + return errors.New(self.c.Tr.IgnoreWhitespaceNotSupportedHere) } self.c.GetAppState().IgnoreWhitespaceInDiffView = !self.c.GetAppState().IgnoreWhitespaceInDiffView diff --git a/pkg/gui/controllers/undo_controller.go b/pkg/gui/controllers/undo_controller.go index fdb56343a..96e9f3f11 100644 --- a/pkg/gui/controllers/undo_controller.go +++ b/pkg/gui/controllers/undo_controller.go @@ -1,6 +1,7 @@ package controllers import ( + "errors" "fmt" "github.com/jesseduffield/gocui" @@ -78,7 +79,7 @@ func (self *UndoController) reflogUndo() error { undoingStatus := self.c.Tr.UndoingStatus if self.c.Git().Status.WorkingTreeState() == enums.REBASE_MODE_REBASING { - return self.c.ErrorMsg(self.c.Tr.CantUndoWhileRebasing) + return errors.New(self.c.Tr.CantUndoWhileRebasing) } return self.parseReflogForActions(func(counter int, action reflogAction) (bool, error) { @@ -126,7 +127,7 @@ func (self *UndoController) reflogRedo() error { redoingStatus := self.c.Tr.RedoingStatus if self.c.Git().Status.WorkingTreeState() == enums.REBASE_MODE_REBASING { - return self.c.ErrorMsg(self.c.Tr.CantRedoWhileRebasing) + return errors.New(self.c.Tr.CantRedoWhileRebasing) } return self.parseReflogForActions(func(counter int, action reflogAction) (bool, error) { diff --git a/pkg/gui/controllers/workspace_reset_controller.go b/pkg/gui/controllers/workspace_reset_controller.go index cc0f4cb5a..3115d6f68 100644 --- a/pkg/gui/controllers/workspace_reset_controller.go +++ b/pkg/gui/controllers/workspace_reset_controller.go @@ -2,6 +2,7 @@ package controllers import ( "bytes" + "errors" "fmt" "math" "math/rand" @@ -88,7 +89,7 @@ func (self *FilesController) createResetMenu() error { OnPress: func() error { self.c.LogAction(self.c.Tr.Actions.RemoveStagedFiles) if !self.c.Helpers().WorkingTree.IsWorkingTreeDirty() { - return self.c.ErrorMsg(self.c.Tr.NoTrackedStagedFilesStash) + return errors.New(self.c.Tr.NoTrackedStagedFilesStash) } if err := self.c.Git().Stash.SaveStagedChanges("[lazygit] tmp stash"); err != nil { return err diff --git a/pkg/gui/controllers/worktrees_controller.go b/pkg/gui/controllers/worktrees_controller.go index e9d15c02a..b9982c8c8 100644 --- a/pkg/gui/controllers/worktrees_controller.go +++ b/pkg/gui/controllers/worktrees_controller.go @@ -1,6 +1,7 @@ package controllers import ( + "errors" "fmt" "strings" "text/tabwriter" @@ -117,11 +118,11 @@ func (self *WorktreesController) add() error { func (self *WorktreesController) remove(worktree *models.Worktree) error { if worktree.IsMain { - return self.c.ErrorMsg(self.c.Tr.CantDeleteMainWorktree) + return errors.New(self.c.Tr.CantDeleteMainWorktree) } if worktree.IsCurrent { - return self.c.ErrorMsg(self.c.Tr.CantDeleteCurrentWorktree) + return errors.New(self.c.Tr.CantDeleteCurrentWorktree) } return self.c.Helpers().Worktree.Remove(worktree, false) diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index c6bb8ada5..83f72c074 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -1,6 +1,7 @@ package gui import ( + "errors" "log" "github.com/jesseduffield/gocui" @@ -442,7 +443,7 @@ func (gui *Gui) callKeybindingHandler(binding *types.Binding) error { } if disabledReason != nil { if disabledReason.ShowErrorInPanel { - return gui.c.ErrorMsg(disabledReason.Text) + return errors.New(disabledReason.Text) } gui.c.ErrorToast(gui.Tr.DisabledMenuItemPrefix + disabledReason.Text) diff --git a/pkg/gui/popup/popup_handler.go b/pkg/gui/popup/popup_handler.go index f5a6eb8c6..1ca9cc3ee 100644 --- a/pkg/gui/popup/popup_handler.go +++ b/pkg/gui/popup/popup_handler.go @@ -80,12 +80,8 @@ func (self *PopupHandler) WithWaitingStatusSync(message string, f func() error) } func (self *PopupHandler) ErrorHandler(err error) error { - return self.ErrorMsg(err.Error()) -} - -func (self *PopupHandler) ErrorMsg(message string) error { // Need to set bold here explicitly; otherwise it gets cancelled by the red colouring. - coloredMessage := style.FgRed.SetBold().Sprint(strings.TrimSpace(message)) + coloredMessage := style.FgRed.SetBold().Sprint(strings.TrimSpace(err.Error())) if err := self.onErrorFn(); err != nil { return err } diff --git a/pkg/gui/services/custom_commands/handler_creator.go b/pkg/gui/services/custom_commands/handler_creator.go index 4d4fb896f..9a127f362 100644 --- a/pkg/gui/services/custom_commands/handler_creator.go +++ b/pkg/gui/services/custom_commands/handler_creator.go @@ -1,6 +1,7 @@ package custom_commands import ( + "errors" "fmt" "strings" "text/template" @@ -103,7 +104,7 @@ func (self *HandlerCreator) call(customCommand config.CustomCommand) func() erro return self.confirmPrompt(resolvedPrompt, g) } default: - return self.c.ErrorMsg("custom command prompt must have a type of 'input', 'menu', 'menuFromCommand', or 'confirm'") + return errors.New("custom command prompt must have a type of 'input', 'menu', 'menuFromCommand', or 'confirm'") } } diff --git a/pkg/gui/types/common.go b/pkg/gui/types/common.go index 3b959be56..44f07c1e3 100644 --- a/pkg/gui/types/common.go +++ b/pkg/gui/types/common.go @@ -131,10 +131,6 @@ type IModeMgr interface { } type IPopupHandler interface { - // Shows a popup with a (localized) "Error" caption and the given error message (in red). - // - // This is a convenience wrapper around Alert(). - ErrorMsg(message string) error // The global error handler for gocui. Not to be used by application code. ErrorHandler(err error) error // Shows a notification popup with the given title and message to the user. -- cgit v1.2.3