Skip to content

Commit

Permalink
refactor: remove sprint functionality and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Deusser authored and rdeusser committed Apr 1, 2022
1 parent 595c9f4 commit f93d755
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 113 deletions.
66 changes: 66 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package stacktrace

import (
"fmt"
"strings"
)

type Error struct {
wrapped error
cause error
message string
frames []frame
}

func (e *Error) Error() string {
var sb strings.Builder

sb.WriteString("Caused by: ")
sb.WriteString(e.message)

if _, ok := e.cause.(*Error); !ok {
if e.cause != nil {
sb.WriteString(" <-- ")
sb.WriteString(e.cause.Error())
}
}

sb.WriteString("\n")

for _, frame := range e.frames {
if frame.file != "" && frame.line != 0 {
sb.WriteString(" at ")
sb.WriteString(frame.file)
sb.WriteString(":")
sb.WriteString(fmt.Sprint(frame.line))

if frame.function != "" {
sb.WriteString(" (")
sb.WriteString(frame.function)
sb.WriteString(")")
}

sb.WriteString("\n")
}
}

if _, ok := e.cause.(*Error); ok {
buf := sb.String()
sb.Reset()
sb.WriteString(buf)
sb.WriteString("\n")
sb.WriteString(e.cause.Error())
}

return sb.String()
}

func (e *Error) Wrapped() error {
if err, ok := e.cause.(*Error); ok {
e.wrapped = fmt.Errorf("%v: %v", e.message, err.Wrapped())
} else {
e.wrapped = fmt.Errorf("%v: %v", e.message, e.cause)
}

return e.wrapped
}
7 changes: 7 additions & 0 deletions frame.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package stacktrace

type frame struct {
file string
function string
line int
}
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/rdeusser/stacktrace

go 1.15

require (
github.com/hexops/autogold v1.2.0
github.com/stretchr/testify v1.7.0
)
require github.com/hexops/autogold v1.3.1-0.20211101223314-d58c8acb6dd6
46 changes: 25 additions & 21 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,60 +1,64 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/hexops/autogold v0.8.1/go.mod h1:97HLDXyG23akzAoRYJh/2OBs3kd80eHyKPvZw0S5ZBY=
github.com/hexops/autogold v1.2.0 h1:qZN9ARrud+VavYs/lZc+4cZvWrwptj3pCtms9jukeVA=
github.com/hexops/autogold v1.2.0/go.mod h1:8YhEzAjkMWmNx+uO18cGxVirXgQXjdvaMwt0Tswbo7w=
github.com/hexops/autogold v1.3.1-0.20211101223314-d58c8acb6dd6 h1:3/817mARLIxfrl8+TwZq6o9654mk6fcQEAO54XuxhmU=
github.com/hexops/autogold v1.3.1-0.20211101223314-d58c8acb6dd6/go.mod h1:1o44arMgGsKIEjLjwBS2bcV17nOqn7P8IT/sYFzQYkk=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/hexops/valast v1.3.0 h1:/dVcyUWEaQxUGbS6ImRH6cmF9DbhRyMqIxIo6kyHoPY=
github.com/hexops/valast v1.3.0/go.mod h1:hQo2H4eaDuesNxXXgI4kzc8+BwD5o2EFTkkEKd5P3zI=
github.com/hexops/valast v1.4.0 h1:sFzyxPDP0riFQUzSBXTCCrAbbIndHPWMndxuEjXdZlc=
github.com/hexops/valast v1.4.0/go.mod h1:uVjKZ0smVuYlgCSPz9NRi5A04sl7lp6GtFWsROKDgEs=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/nightlyone/lockfile v1.0.0 h1:RHep2cFKK4PonZJDdEl4GmkabuhbsRMgk/k3uAmxBiA=
github.com/nightlyone/lockfile v1.0.0/go.mod h1:rywoIealpdNse2r832aiD9jRk8ErCatROs6LzC841CI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041 h1:llrF3Fs4018ePo4+G/HV/uQUqEI1HMDjCeOf2V6puPc=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/shurcooL/go-goon v0.0.0-20210110234559-7585751d9a17 h1:lRAUE0dIvigSSFAmaM2dfg7OH8T+a8zJ5smEh09a/GI=
github.com/shurcooL/go-goon v0.0.0-20210110234559-7585751d9a17/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0 h1:8pl+sMODzuvGJkmj2W4kZihvVb5mKm8pB/X44PIQHv8=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1 h1:Kvvh58BN8Y9/lBi7hTekvtMpm07eUZ0ck5pRHpsMWrY=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20210101214203-2dba1e4ea05c/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210104081019-d8d6ddbec6ee h1:5xKxdl/RhlelmSPaxyVeq5PYSmJ4H14yeQT58qP1F6o=
golang.org/x/tools v0.0.0-20210104081019-d8d6ddbec6ee/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
mvdan.cc/gofumpt v0.0.0-20210102134722-7ed9b835dfb3/go.mod h1:yXG1r1WqZVKWbVRtBWKWX9+CxGYfA51nSomhM0woR48=
mvdan.cc/gofumpt v0.0.0-20210102162653-179728cc0722 h1:BmHL9mnhlnM7UNAgL4UVuyKrUsYUDglfhqKlDUJIRqA=
mvdan.cc/gofumpt v0.0.0-20210102162653-179728cc0722/go.mod h1:yXG1r1WqZVKWbVRtBWKWX9+CxGYfA51nSomhM0woR48=
mvdan.cc/gofumpt v0.0.0-20210107193838-d24d34e18d44/go.mod h1:yXG1r1WqZVKWbVRtBWKWX9+CxGYfA51nSomhM0woR48=
mvdan.cc/gofumpt v0.1.0 h1:hsVv+Y9UsZ/mFZTxJZuHVI6shSQCtzZ11h1JEFPAZLw=
mvdan.cc/gofumpt v0.1.0/go.mod h1:yXG1r1WqZVKWbVRtBWKWX9+CxGYfA51nSomhM0woR48=
85 changes: 9 additions & 76 deletions stacktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,17 @@ import (
"fmt"
"os"
"runtime"
"strings"
)

const maxDepth = 10

type frame struct {
file string
line int
function string
}

type stacktrace struct {
wrapped error
cause error
message string
frames []frame
}

func (s *stacktrace) Error() string {
var b strings.Builder

b.WriteString("Caused by: ")
b.WriteString(s.message)

if _, ok := s.cause.(*stacktrace); !ok {
if s.cause != nil {
b.WriteString(" <-- ")
b.WriteString(s.cause.Error())
}
}

b.WriteString("\n")

for _, frame := range s.frames {
if frame.file != "" && frame.line != 0 {
b.WriteString(" at ")
b.WriteString(frame.file)
b.WriteString(":")
b.WriteString(fmt.Sprint(frame.line))

if frame.function != "" {
b.WriteString(" (")
b.WriteString(frame.function)
b.WriteString(")")
}

b.WriteString("\n")
}
}

if _, ok := s.cause.(*stacktrace); ok {
buf := b.String()
b.Reset()
b.WriteString(buf)
b.WriteString("\n")
b.WriteString(s.cause.Error())
}

return b.String()
}

func (s *stacktrace) Wrapped() error {
if e, ok := s.cause.(*stacktrace); ok {
s.wrapped = fmt.Errorf("%v: %v", s.message, e.Wrapped())
} else {
s.wrapped = fmt.Errorf("%v: %v", s.message, s.cause)
}

return s.wrapped
}
const maxDepth = 5

// Propagate propagates errors up through the call stack.
func Propagate(err error, format string, args ...interface{}) error {
func Propagate(err error, format string, args ...interface{}) *Error {
pc := make([]uintptr, maxDepth)
_ = runtime.Callers(2, pc[:])
frames := runtime.CallersFrames(pc)

st := &stacktrace{
serr := &Error{
wrapped: err,
cause: err,
message: fmt.Sprintf(format, args...),
Expand All @@ -94,14 +27,14 @@ func Propagate(err error, format string, args ...interface{}) error {
break
}

st.frames = append(st.frames, frame{
serr.frames = append(serr.frames, frame{
file: f.File,
line: f.Line,
function: f.Function,
line: f.Line,
})
}

return st
return serr
}

// Throw panics, recovers, and prints the stacktrace to standard out.
Expand All @@ -113,10 +46,10 @@ func Throw(err error) {
panic(err)
}

// Error returns the wrapped error much like `github.com/pkg/errors`
// Unwrap returns the wrapped error much like `github.com/pkg/errors`
// does (i.e. no stacktrace).
func Error(err error) error {
if e, ok := err.(*stacktrace); ok {
func Unwrap(err error) error {
if e, ok := err.(*Error); ok {
return e.Wrapped()
}
return err
Expand Down
10 changes: 5 additions & 5 deletions stacktrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
"github.com/hexops/autogold"
)

func fnlibcalls() error {
func caller() error {
err := errors.New("json: cannot unmarshal json array to something or other")
return Propagate(err, "unmarshaling json")
}

func fninlib() error {
err := fnlibcalls()
func callee() error {
err := caller()
return Propagate(err, "doing something important")
}

func TestPropogate(t *testing.T) {
err := fninlib()
func TestPropagate(t *testing.T) {
err := callee()
autogold.Equal(t, autogold.Raw(err.Error()))
}
14 changes: 7 additions & 7 deletions testdata/TestPropogate.golden → testdata/TestPropagate.golden
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Caused by: doing something important
at /Users/rdeusser/src/stacktrace/stacktrace_test.go:17 (github.com/rdeusser/stacktrace.fninlib)
at /Users/rdeusser/src/stacktrace/stacktrace_test.go:21 (github.com/rdeusser/stacktrace.TestPropogate)
at /usr/local/Cellar/go/1.15.4/libexec/src/testing/testing.go:1123 (testing.tRunner)
at /Users/rdeusser/src/stacktrace/stacktrace_test.go:17 (github.com/rdeusser/stacktrace.callee)
at /Users/rdeusser/src/stacktrace/stacktrace_test.go:21 (github.com/rdeusser/stacktrace.TestPropagate)
at /usr/local/Cellar/go1.18/1.18/libexec/src/testing/testing.go:1439 (testing.tRunner)

Caused by: unmarshaling json <-- json: cannot unmarshal json array to something or other
at /Users/rdeusser/src/stacktrace/stacktrace_test.go:12 (github.com/rdeusser/stacktrace.fnlibcalls)
at /Users/rdeusser/src/stacktrace/stacktrace_test.go:16 (github.com/rdeusser/stacktrace.fninlib)
at /Users/rdeusser/src/stacktrace/stacktrace_test.go:21 (github.com/rdeusser/stacktrace.TestPropogate)
at /usr/local/Cellar/go/1.15.4/libexec/src/testing/testing.go:1123 (testing.tRunner)
at /Users/rdeusser/src/stacktrace/stacktrace_test.go:12 (github.com/rdeusser/stacktrace.caller)
at /Users/rdeusser/src/stacktrace/stacktrace_test.go:16 (github.com/rdeusser/stacktrace.callee)
at /Users/rdeusser/src/stacktrace/stacktrace_test.go:21 (github.com/rdeusser/stacktrace.TestPropagate)
at /usr/local/Cellar/go1.18/1.18/libexec/src/testing/testing.go:1439 (testing.tRunner)

0 comments on commit f93d755

Please sign in to comment.