From 653e6c2b26cbb38c92b4b6af5de4d5f60363f7f2 Mon Sep 17 00:00:00 2001 From: Bigyin Date: Mon, 16 Mar 2020 01:15:23 +0300 Subject: [PATCH 1/2] Add RootMessage function --- cause.go | 38 ++++++++++++++++++++++++++++++++++++++ cause_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/cause.go b/cause.go index 1e2df9f..286a5a1 100644 --- a/cause.go +++ b/cause.go @@ -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 + } +} diff --git a/cause_test.go b/cause_test.go index f63a71b..f24132f 100644 --- a/cause_test.go +++ b/cause_test.go @@ -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)) + } +} From f758a2ddeae955f62d7170462647b9e593e50c07 Mon Sep 17 00:00:00 2001 From: Bigyin Date: Mon, 16 Mar 2020 02:42:30 +0300 Subject: [PATCH 2/2] Updated travis.yml --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index fc3d7eb..10d6f90 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: