Skip to content

Commit

Permalink
Merge pull request #154 from ydb-platform/errors-without-printf
Browse files Browse the repository at this point in the history
v3.14.2: Refactored internal error wrapping (with file and line identification…
  • Loading branch information
asmyasnikov committed Mar 12, 2022
2 parents 82ae6c8 + 3e8e2c6 commit 4b473a2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 3.14.2
* Refactored internal error wrapping (with file and line identification) - replaced `fmt.Printf("%w", err)` error wrapping to internal `stackError`

## 3.14.1
* Added `balacers.CreateFromConfig` balancer creator
* Added `Create` method to interface `balancer.Balancer`
Expand Down
44 changes: 42 additions & 2 deletions internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ func (e *errorWithIssues) Error() string {

// Error is alias to fmt.Errorf() with prepend file:line prefix
func Error(err error) error {
return ErrorfSkip(1, "%w", err)
return &stackError{
stackRecord: stackRecord(1),
err: err,
}
}

// Errorf is alias to fmt.Errorf() with prepend file:line prefix
Expand All @@ -113,8 +116,16 @@ func Errorf(format string, args ...interface{}) error {

// ErrorfSkip is alias to fmt.Errorf() with prepend file:line prefix
func ErrorfSkip(depth int, format string, args ...interface{}) error {
return &stackError{
stackRecord: stackRecord(depth + 1),
err: fmt.Errorf(format, args...),
}
}

func stackRecord(depth int) string {
function, file, line, _ := runtime.Caller(depth + 1)
return fmt.Errorf(runtime.FuncForPC(function).Name()+" ("+fileName(file)+":"+strconv.Itoa(line)+") "+format, args...)
name := runtime.FuncForPC(function).Name()
return funcName(name) + "(" + packageName(name) + "/" + fileName(file) + ":" + strconv.Itoa(line) + ")"
}

func fileName(original string) string {
Expand All @@ -124,3 +135,32 @@ func fileName(original string) string {
}
return original[i+1:]
}

func packageName(original string) string {
i := strings.LastIndex(original, ".")
if i == -1 {
return original
}
return original[:i]
}

func funcName(original string) string {
i := strings.LastIndex(original, "/")
if i == -1 {
return original
}
return original[i+1:]
}

type stackError struct {
stackRecord string
err error
}

func (e *stackError) Error() string {
return e.err.Error() + " at `" + e.stackRecord + "`"
}

func (e *stackError) Unwrap() error {
return e.err
}
34 changes: 34 additions & 0 deletions internal/errors/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package errors

import (
"fmt"
"testing"
)

func TestError(t *testing.T) {
for _, test := range []struct {
error error
text string
}{
{
error: Error(fmt.Errorf("TestError")),
text: "TestError at `errors.TestError(github.com/ydb-platform/ydb-go-sdk/v3/internal/errors/errors_test.go:14)`",
},
{
error: Errorf("TestError%s", "Printf"),
// nolint:lll
text: "TestErrorPrintf at `errors.TestError(github.com/ydb-platform/ydb-go-sdk/v3/internal/errors/errors_test.go:18)`",
},
{
error: ErrorfSkip(0, "TestError%s", "Printf"),
// nolint:lll
text: "TestErrorPrintf at `errors.TestError(github.com/ydb-platform/ydb-go-sdk/v3/internal/errors/errors_test.go:23)`",
},
} {
t.Run(test.text, func(t *testing.T) {
if test.error.Error() != test.text {
t.Fatalf("unexpected text of error: \"%s\", exp: \"%s\"", test.error.Error(), test.text)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/meta/version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package meta

const (
Version = "ydb-go-sdk/3.14.1"
Version = "ydb-go-sdk/3.14.2"
)

0 comments on commit 4b473a2

Please sign in to comment.