Skip to content

Commit

Permalink
use NewStackSkip for correct stack start
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Weber committed Jan 21, 2025
1 parent c6d123e commit 312e498
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
14 changes: 7 additions & 7 deletions errstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// New returns an error with the supplied message.
// New also records the stack trace at the point it was called.
func New(message string) error {
return &fundamental{stderrors.New(message), stackfmt.NewStack()}
return &fundamental{stderrors.New(message), stackfmt.NewStackSkip(1)}
}

// Errorf formats according to a format specifier and returns the string
Expand All @@ -19,11 +19,11 @@ func New(message string) error {
func Errorf(format string, args ...interface{}) error {
err := fmt.Errorf(format, args...)
if _, ok := err.(unwrapper); ok {
return &addStack{withStack{err, stackfmt.NewStack()}}
return &addStack{withStack{err, stackfmt.NewStackSkip(1)}}
} else if _, ok := err.(unwraps); ok {
return &addStack{withStack{err, stackfmt.NewStack()}}
return &addStack{withStack{err, stackfmt.NewStackSkip(1)}}
}
return &fundamental{err, stackfmt.NewStack()}
return &fundamental{err, stackfmt.NewStackSkip(1)}
}

// fundamental is a base error that doesn't wrap other errors
Expand Down Expand Up @@ -63,7 +63,7 @@ func AddStack(err error) error {
if HasStack(err) {
return err
}
return &addStack{withStack{err, stackfmt.NewStack()}}
return &addStack{withStack{err, stackfmt.NewStackSkip(1)}}
}

// Same as AddStack but specify an additional number of callers to skip
Expand Down Expand Up @@ -118,7 +118,7 @@ func Wrap(err error, message string) error {
}
return &withMessage{
msg: message,
withStack: withStack{err, stackfmt.NewStack()},
withStack: withStack{err, stackfmt.NewStackSkip(1)},
}
}

Expand All @@ -135,7 +135,7 @@ func Wrapf(err error, format string, args ...interface{}) error {
}
return &withMessage{
msg: fmt.Sprintf(format, args...),
withStack: withStack{err, stackfmt.NewStack()},
withStack: withStack{err, stackfmt.NewStackSkip(1)},
}
}

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/gregwebs/errors
go 1.23.3

require (
github.com/gregwebs/errors/errwrap v0.0.0-20250109233418-ed1e850dab1e // indirect
github.com/gregwebs/errors/slogerr v0.0.0-20250109233418-ed1e850dab1e // indirect
github.com/gregwebs/stackfmt v0.0.0-20250109232346-61b11c0ff204 // indirect
github.com/gregwebs/errors/errwrap v0.0.0-20250109233418-ed1e850dab1e
github.com/gregwebs/errors/slogerr v0.0.0-20250109233418-ed1e850dab1e
github.com/gregwebs/stackfmt v0.1.1
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ github.com/gregwebs/errors/errwrap v0.0.0-20250109233418-ed1e850dab1e h1:Wafq/gb
github.com/gregwebs/errors/errwrap v0.0.0-20250109233418-ed1e850dab1e/go.mod h1:Xqoa1jjxE/0w8gjUTyRQEOXrG8d+sAwQmZEwnUyTtig=
github.com/gregwebs/errors/slogerr v0.0.0-20250109233418-ed1e850dab1e h1:IyuYN/MGfJmwZrD8zwEAXtxDoxOXGHAvXA4SRlsmQZI=
github.com/gregwebs/errors/slogerr v0.0.0-20250109233418-ed1e850dab1e/go.mod h1:emBiGGVdfUO1UnPUCvgkN49Y2eL+EClImkMR+4Iq0rY=
github.com/gregwebs/stackfmt v0.0.0-20250109232346-61b11c0ff204 h1:h4p6a7+y8j1hAQZooo/NUfOPUj1LKCr9oPPkIUtIYbI=
github.com/gregwebs/stackfmt v0.0.0-20250109232346-61b11c0ff204/go.mod h1:MQWvus3s9juULflTEiFIllieKiz7AnX7i4HWyPOs2nQ=
github.com/gregwebs/stackfmt v0.1.1 h1:bqHZv69OGARsyNPD4tJfkJnu5oWjA86dISQKKfFt98A=
github.com/gregwebs/stackfmt v0.1.1/go.mod h1:MQWvus3s9juULflTEiFIllieKiz7AnX7i4HWyPOs2nQ=

0 comments on commit 312e498

Please sign in to comment.