-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #638 from cszczepaniak/cs/remove_pkg_errors
Remove github.com/pkg/errors
- Loading branch information
Showing
9 changed files
with
122 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package stackerr | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"runtime/debug" | ||
) | ||
|
||
type StackErr struct { | ||
cause error | ||
stack []byte | ||
} | ||
|
||
func NewStackErr(cause error) error { | ||
return StackErr{ | ||
cause: cause, | ||
stack: debug.Stack(), | ||
} | ||
} | ||
|
||
func NewStackErrf(cause error, f string, args ...any) error { | ||
msg := fmt.Sprintf(f, args...) | ||
cause = fmt.Errorf(msg+": %w", cause) | ||
return NewStackErr(cause) | ||
} | ||
|
||
func (se StackErr) Error() string { | ||
return se.cause.Error() | ||
} | ||
|
||
func (se StackErr) Unwrap() error { | ||
return se.cause | ||
} | ||
|
||
func (se StackErr) Stack() []byte { | ||
return se.stack | ||
} | ||
|
||
func GetStack(err error) ([]byte, bool) { | ||
var s interface { | ||
Stack() []byte | ||
} | ||
if errors.As(err, &s) { | ||
return s.Stack(), true | ||
} | ||
return nil, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package stackerr | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStackErr(t *testing.T) { | ||
err := assert.AnError | ||
|
||
s, ok := GetStack(err) | ||
assert.False(t, ok) | ||
assert.Empty(t, s) | ||
|
||
err = NewStackErr(err) | ||
assert.Equal(t, assert.AnError.Error(), err.Error()) | ||
|
||
s, ok = GetStack(err) | ||
assert.True(t, ok) | ||
assert.NotEmpty(t, s) | ||
|
||
err = NewStackErr(fmt.Errorf("wrapped error can still get stack: %w", err)) | ||
s, ok = GetStack(err) | ||
assert.True(t, ok) | ||
assert.NotEmpty(t, s) | ||
} | ||
|
||
func TestStackErrf(t *testing.T) { | ||
err := assert.AnError | ||
|
||
s, ok := GetStack(err) | ||
assert.False(t, ok) | ||
assert.Empty(t, s) | ||
|
||
err = NewStackErrf(err, "error message %d %s", 1, "a") | ||
assert.Equal(t, "error message 1 a: "+assert.AnError.Error(), err.Error()) | ||
|
||
s, ok = GetStack(err) | ||
assert.True(t, ok) | ||
assert.NotEmpty(t, s) | ||
} |