From cfe3605e6b29e23db8dca9eedecc58cb13341587 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 11 Feb 2019 21:30:27 +1100 Subject: use go-errors package to display stacktrace of errors that cause panics --- pkg/commands/exec_live_default.go | 2 +- pkg/commands/git.go | 7 ++++--- pkg/commands/os.go | 18 ++++++++++-------- pkg/commands/pull_request.go | 3 ++- pkg/git/patch_modifier.go | 3 ++- pkg/gui/commits_panel.go | 3 ++- pkg/gui/gui.go | 7 +++++-- pkg/gui/options_menu_panel.go | 2 +- pkg/gui/staging_panel.go | 2 +- pkg/test/test.go | 2 +- pkg/updates/updates.go | 3 ++- pkg/utils/utils.go | 3 ++- pkg/utils/utils_test.go | 37 ++++++++++++++++++++++--------------- 13 files changed, 55 insertions(+), 37 deletions(-) (limited to 'pkg') diff --git a/pkg/commands/exec_live_default.go b/pkg/commands/exec_live_default.go index dfdff5308..2d0b78c21 100644 --- a/pkg/commands/exec_live_default.go +++ b/pkg/commands/exec_live_default.go @@ -5,7 +5,7 @@ package commands import ( "bufio" "bytes" - "errors" + "github.com/go-errors/errors" "os" "os/exec" "strings" diff --git a/pkg/commands/git.go b/pkg/commands/git.go index a1f01e422..4d9283a23 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -1,12 +1,13 @@ package commands import ( - "errors" "fmt" "os" "os/exec" "strings" + "github.com/go-errors/errors" + "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/sirupsen/logrus" @@ -27,11 +28,11 @@ func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir f } if !os.IsNotExist(err) { - return err + return errors.Wrap(err, 0) } if err = chdir(".."); err != nil { - return err + return errors.Wrap(err, 0) } } } diff --git a/pkg/commands/os.go b/pkg/commands/os.go index ffc0516ad..0bd9bf34a 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -1,13 +1,14 @@ package commands import ( - "errors" "io/ioutil" "os" "os/exec" "regexp" "strings" + "github.com/go-errors/errors" + "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/mgutz/str" @@ -122,7 +123,7 @@ func sanitisedCommandOutput(output []byte, err error) (string, error) { // errors like 'exit status 1' are not very useful so we'll create an error // from the combined output if outputString == "" { - return "", err + return "", errors.Wrap(err, 0) } return outputString, errors.New(outputString) } @@ -201,12 +202,12 @@ func (c *OSCommand) Unquote(message string) string { func (c *OSCommand) AppendLineToFile(filename, line string) error { f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) if err != nil { - return err + return errors.Wrap(err, 0) } defer f.Close() _, err = f.WriteString("\n" + line) - return err + return errors.Wrap(err, 0) } // CreateTempFile writes a string to a new temp file and returns the file's name @@ -214,16 +215,16 @@ func (c *OSCommand) CreateTempFile(filename, content string) (string, error) { tmpfile, err := ioutil.TempFile("", filename) if err != nil { c.Log.Error(err) - return "", err + return "", errors.Wrap(err, 0) } if _, err := tmpfile.WriteString(content); err != nil { c.Log.Error(err) - return "", err + return "", errors.Wrap(err, 0) } if err := tmpfile.Close(); err != nil { c.Log.Error(err) - return "", err + return "", errors.Wrap(err, 0) } return tmpfile.Name(), nil @@ -231,5 +232,6 @@ func (c *OSCommand) CreateTempFile(filename, content string) (string, error) { // RemoveFile removes a file at the specified path func (c *OSCommand) RemoveFile(filename string) error { - return os.Remove(filename) + err := os.Remove(filename) + return errors.Wrap(err, 0) } diff --git a/pkg/commands/pull_request.go b/pkg/commands/pull_request.go index 043a3c07d..7f0a55334 100644 --- a/pkg/commands/pull_request.go +++ b/pkg/commands/pull_request.go @@ -1,9 +1,10 @@ package commands import ( - "errors" "fmt" "strings" + + "github.com/go-errors/errors" ) // Service is a service that repository is on (Github, Bitbucket, ...) diff --git a/pkg/git/patch_modifier.go b/pkg/git/patch_modifier.go index 3c523232e..f27040775 100644 --- a/pkg/git/patch_modifier.go +++ b/pkg/git/patch_modifier.go @@ -1,11 +1,12 @@ package git import ( - "errors" "regexp" "strconv" "strings" + "github.com/go-errors/errors" + "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/sirupsen/logrus" diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index 4ff79960d..8ae1175be 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -1,9 +1,10 @@ package gui import ( - "errors" "fmt" + "github.com/go-errors/errors" + "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/utils" diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index ed7cfba0f..59cfd3a8b 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -6,7 +6,6 @@ import ( // "io" // "io/ioutil" - "errors" "io/ioutil" "log" "os" @@ -14,6 +13,8 @@ import ( "strings" "time" + "github.com/go-errors/errors" + // "strings" "github.com/fatih/color" @@ -568,7 +569,9 @@ func (gui *Gui) RunWithSubprocesses() { gui.SubProcess.Stdin = nil gui.SubProcess = nil } else { - log.Panicln(err) + newErr := errors.Wrap(err, 0) + stackTrace := newErr.ErrorStack() + log.Panicln(stackTrace) } } } diff --git a/pkg/gui/options_menu_panel.go b/pkg/gui/options_menu_panel.go index ac01ad03d..cc736a5ae 100644 --- a/pkg/gui/options_menu_panel.go +++ b/pkg/gui/options_menu_panel.go @@ -1,7 +1,7 @@ package gui import ( - "errors" + "github.com/go-errors/errors" "github.com/jesseduffield/gocui" ) diff --git a/pkg/gui/staging_panel.go b/pkg/gui/staging_panel.go index 1408cfb45..836c978f2 100644 --- a/pkg/gui/staging_panel.go +++ b/pkg/gui/staging_panel.go @@ -1,7 +1,7 @@ package gui import ( - "errors" + "github.com/go-errors/errors" "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/git" diff --git a/pkg/test/test.go b/pkg/test/test.go index 6346ac556..b3db699d0 100644 --- a/pkg/test/test.go +++ b/pkg/test/test.go @@ -1,7 +1,7 @@ package test import ( - "errors" + "github.com/go-errors/errors" "os" "os/exec" "path/filepath" diff --git a/pkg/updates/updates.go b/pkg/updates/updates.go index a08a2edcc..a4ec67e27 100644 --- a/pkg/updates/updates.go +++ b/pkg/updates/updates.go @@ -2,7 +2,6 @@ package updates import ( "encoding/json" - "errors" "fmt" "io/ioutil" "net/http" @@ -13,6 +12,8 @@ import ( "strings" "time" + "github.com/go-errors/errors" + "github.com/kardianos/osext" getter "github.com/jesseduffield/go-getter" diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 390f85f70..60985dd0b 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -2,7 +2,6 @@ package utils import ( "encoding/json" - "errors" "fmt" "log" "os" @@ -11,6 +10,8 @@ import ( "strings" "time" + "github.com/go-errors/errors" + "github.com/fatih/color" ) diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index f7545f5e9..ee502c1f8 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -1,7 +1,6 @@ package utils import ( - "errors" "testing" "github.com/stretchr/testify/assert" @@ -233,9 +232,9 @@ func TestGetDisplayStringArrays(t *testing.T) { // TestRenderDisplayableList is a function. func TestRenderDisplayableList(t *testing.T) { type scenario struct { - input []Displayable - expectedString string - expectedError error + input []Displayable + expectedString string + expectedErrorMessage string } scenarios := []scenario{ @@ -245,7 +244,7 @@ func TestRenderDisplayableList(t *testing.T) { Displayable(&myDisplayable{[]string{}}), }, "\n", - nil, + "", }, { []Displayable{ @@ -253,7 +252,7 @@ func TestRenderDisplayableList(t *testing.T) { Displayable(&myDisplayable{[]string{"c", "d"}}), }, "aa b\nc d", - nil, + "", }, { []Displayable{ @@ -261,23 +260,27 @@ func TestRenderDisplayableList(t *testing.T) { Displayable(&myDisplayable{[]string{"b", "c"}}), }, "", - errors.New("Each item must return the same number of strings to display"), + "Each item must return the same number of strings to display", }, } for _, s := range scenarios { str, err := renderDisplayableList(s.input) assert.EqualValues(t, s.expectedString, str) - assert.EqualValues(t, s.expectedError, err) + if s.expectedErrorMessage != "" { + assert.EqualError(t, err, s.expectedErrorMessage) + } else { + assert.NoError(t, err) + } } } // TestRenderList is a function. func TestRenderList(t *testing.T) { type scenario struct { - input interface{} - expectedString string - expectedError error + input interface{} + expectedString string + expectedErrorMessage string } scenarios := []scenario{ @@ -287,7 +290,7 @@ func TestRenderList(t *testing.T) { {[]string{"c", "d"}}, }, "aa b\nc d", - nil, + "", }, { []*myStruct{ @@ -295,19 +298,23 @@ func TestRenderList(t *testing.T) { {}, }, "", - errors.New("item does not implement the Displayable interface"), + "item does not implement the Displayable interface", }, { &myStruct{}, "", - errors.New("RenderList given a non-slice type"), + "RenderList given a non-slice type", }, } for _, s := range scenarios { str, err := RenderList(s.input) assert.EqualValues(t, s.expectedString, str) - assert.EqualValues(t, s.expectedError, err) + if s.expectedErrorMessage != "" { + assert.EqualError(t, err, s.expectedErrorMessage) + } else { + assert.NoError(t, err) + } } } -- cgit v1.2.3