Skip to content

Commit 3e8e2c6

Browse files
committed
Refactored internal error wrapping (with file and line identification) - replaced fmt.Printf("%w", err) error wrapping to internal stackError
1 parent 9caa466 commit 3e8e2c6

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.14.2
2+
* Refactored internal error wrapping (with file and line identification) - replaced `fmt.Printf("%w", err)` error wrapping to internal `stackError`
3+
14
## 3.14.1
25
* Added `balacers.CreateFromConfig` balancer creator
36
* Added `Create` method to interface `balancer.Balancer`

internal/errors/errors.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ func (e *errorWithIssues) Error() string {
103103

104104
// Error is alias to fmt.Errorf() with prepend file:line prefix
105105
func Error(err error) error {
106-
return ErrorfSkip(1, "%w", err)
106+
return &stackError{
107+
stackRecord: stackRecord(1),
108+
err: err,
109+
}
107110
}
108111

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

114117
// ErrorfSkip is alias to fmt.Errorf() with prepend file:line prefix
115118
func ErrorfSkip(depth int, format string, args ...interface{}) error {
119+
return &stackError{
120+
stackRecord: stackRecord(depth + 1),
121+
err: fmt.Errorf(format, args...),
122+
}
123+
}
124+
125+
func stackRecord(depth int) string {
116126
function, file, line, _ := runtime.Caller(depth + 1)
117-
return fmt.Errorf(runtime.FuncForPC(function).Name()+" ("+fileName(file)+":"+strconv.Itoa(line)+") "+format, args...)
127+
name := runtime.FuncForPC(function).Name()
128+
return funcName(name) + "(" + packageName(name) + "/" + fileName(file) + ":" + strconv.Itoa(line) + ")"
118129
}
119130

120131
func fileName(original string) string {
@@ -124,3 +135,32 @@ func fileName(original string) string {
124135
}
125136
return original[i+1:]
126137
}
138+
139+
func packageName(original string) string {
140+
i := strings.LastIndex(original, ".")
141+
if i == -1 {
142+
return original
143+
}
144+
return original[:i]
145+
}
146+
147+
func funcName(original string) string {
148+
i := strings.LastIndex(original, "/")
149+
if i == -1 {
150+
return original
151+
}
152+
return original[i+1:]
153+
}
154+
155+
type stackError struct {
156+
stackRecord string
157+
err error
158+
}
159+
160+
func (e *stackError) Error() string {
161+
return e.err.Error() + " at `" + e.stackRecord + "`"
162+
}
163+
164+
func (e *stackError) Unwrap() error {
165+
return e.err
166+
}

internal/errors/errors_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package errors
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestError(t *testing.T) {
9+
for _, test := range []struct {
10+
error error
11+
text string
12+
}{
13+
{
14+
error: Error(fmt.Errorf("TestError")),
15+
text: "TestError at `errors.TestError(github.com/ydb-platform/ydb-go-sdk/v3/internal/errors/errors_test.go:14)`",
16+
},
17+
{
18+
error: Errorf("TestError%s", "Printf"),
19+
// nolint:lll
20+
text: "TestErrorPrintf at `errors.TestError(github.com/ydb-platform/ydb-go-sdk/v3/internal/errors/errors_test.go:18)`",
21+
},
22+
{
23+
error: ErrorfSkip(0, "TestError%s", "Printf"),
24+
// nolint:lll
25+
text: "TestErrorPrintf at `errors.TestError(github.com/ydb-platform/ydb-go-sdk/v3/internal/errors/errors_test.go:23)`",
26+
},
27+
} {
28+
t.Run(test.text, func(t *testing.T) {
29+
if test.error.Error() != test.text {
30+
t.Fatalf("unexpected text of error: \"%s\", exp: \"%s\"", test.error.Error(), test.text)
31+
}
32+
})
33+
}
34+
}

internal/meta/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package meta
22

33
const (
4-
Version = "ydb-go-sdk/3.14.1"
4+
Version = "ydb-go-sdk/3.14.2"
55
)

0 commit comments

Comments
 (0)