Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RootMessage function #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ sudo: false

language: go

go: 1.6
go: 1.13

before_install:
- go get github.com/golang/lint/golint
- go get -u golang.org/x/lint/golint

script:
- go vet ./...
- $HOME/gopath/bin/golint ./...
- $GOPATH/bin/golint ./...
- go test -v ./...

notifications:
Expand Down
38 changes: 38 additions & 0 deletions cause.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,41 @@ func RootCause(err error) error {
err = st.cause
}
}

/*
RootMessage returns message which was associated with root cause error.
func f(file string) error {
....
....
file, err := os.Open(file)
if err != nil {
return stacktrace.Propagate(err, "formatted message for user", ...specific args)
}
....
....
}

err := f("./foo/bar")
if err != nil {
err := stacktrace.RootCause(err)
// do something with root error

// return formatted message for user
showToUser(stacktrace.RootMessage(err))
}

*/
func RootMessage(err error) string {
var message string
for {
st, ok := err.(*stacktrace)
if !ok {
return message
}
if st.cause == nil {
return st.message
}
err = st.cause
message = st.message
}
}
34 changes: 34 additions & 0 deletions cause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,37 @@ func TestRootCause(t *testing.T) {
assert.Equal(t, test.rootCause, stacktrace.RootCause(test.err))
}
}

func TestRootMessage(t *testing.T) {
for _, test := range []struct {
message string
err error
}{
{
message: "",
err: nil,
},
{
message: "",
err: customError("msg"),
},
{
message: "",
err: errors.New("msg"),
},
{
message: "msg",
err: stacktrace.NewError("msg"),
},
{
message: "msg2",
err: stacktrace.Propagate(customError("msg1"), "msg2"),
},
{
message: "msg2",
err: stacktrace.Propagate(errors.New("msg1"), "msg2"),
},
} {
assert.Equal(t, test.message, stacktrace.RootMessage(test.err))
}
}