Skip to content

Commit

Permalink
use go-errors package to display stacktrace of errors that cause panics
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Feb 11, 2019
1 parent 3a60706 commit cfe3605
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 37 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/exec_live_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package commands
import (
"bufio"
"bytes"
"errors"
"github.com/go-errors/errors"
"os"
"os/exec"
"strings"
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/git.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions pkg/commands/os.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -201,35 +202,36 @@ 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
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
}

// 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)
}
3 changes: 2 additions & 1 deletion pkg/commands/pull_request.go
Original file line number Diff line number Diff line change
@@ -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, ...)
Expand Down
3 changes: 2 additions & 1 deletion pkg/git/patch_modifier.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
3 changes: 2 additions & 1 deletion pkg/gui/commits_panel.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
7 changes: 5 additions & 2 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
// "io"
// "io/ioutil"

"errors"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"time"

"github.com/go-errors/errors"

// "strings"

"github.com/fatih/color"
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/options_menu_panel.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gui

import (
"errors"
"github.com/go-errors/errors"

"github.com/jesseduffield/gocui"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/staging_panel.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gui

import (
"errors"
"github.com/go-errors/errors"

"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/git"
Expand Down
2 changes: 1 addition & 1 deletion pkg/test/test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package test

import (
"errors"
"github.com/go-errors/errors"
"os"
"os/exec"
"path/filepath"
Expand Down
3 changes: 2 additions & 1 deletion pkg/updates/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package updates

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -13,6 +12,8 @@ import (
"strings"
"time"

"github.com/go-errors/errors"

"github.com/kardianos/osext"

getter "github.com/jesseduffield/go-getter"
Expand Down
3 changes: 2 additions & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
Expand All @@ -11,6 +10,8 @@ import (
"strings"
"time"

"github.com/go-errors/errors"

"github.com/fatih/color"
)

Expand Down
37 changes: 22 additions & 15 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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{
Expand All @@ -245,39 +244,43 @@ func TestRenderDisplayableList(t *testing.T) {
Displayable(&myDisplayable{[]string{}}),
},
"\n",
nil,
"",
},
{
[]Displayable{
Displayable(&myDisplayable{[]string{"aa", "b"}}),
Displayable(&myDisplayable{[]string{"c", "d"}}),
},
"aa b\nc d",
nil,
"",
},
{
[]Displayable{
Displayable(&myDisplayable{[]string{"a"}}),
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{
Expand All @@ -287,27 +290,31 @@ func TestRenderList(t *testing.T) {
{[]string{"c", "d"}},
},
"aa b\nc d",
nil,
"",
},
{
[]*myStruct{
{},
{},
},
"",
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)
}
}
}

Expand Down

0 comments on commit cfe3605

Please sign in to comment.