Skip to content

Commit 8e820e3

Browse files
lvan100lianghuan
authored andcommitted
refactor(conf): optimize error handling
1 parent 3119bb9 commit 8e820e3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+2472
-2097
lines changed

CODE_OF_CONDUCT.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,4 @@ Appeals can be submitted via email to the maintainers.
146146
* 暂停或永久禁用项目访问权限
147147
* 向相关平台报告
148148

149-
如对处理结果有异议,可通过电子邮件向维护者提交申诉。
149+
如对处理结果有异议,可通过电子邮件向维护者提交申诉。

CONTRIBUTING.md

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,37 @@ new features. Please follow the guidelines below to contribute.
3131

3232
2. **Maintain consistent coding style**
3333

34-
* Follow Go’s official style guidelines (`gofmt`, `golint`, `go vet`).
35-
* Recommended: [`golangci-lint`](https://github.com/golangci/golangci-lint) for local linting.
34+
* Follow Go’s official style guidelines (`gofmt`, `golint`, `go vet`).
35+
* Recommended: [`golangci-lint`](https://github.com/golangci/golangci-lint) for local linting.
3636

3737
3. **Write tests**
3838

39-
* All new features or bug fixes must include unit tests.
40-
* Use Go’s `testing` package; test files should be named `xxx_test.go`.
41-
* Example:
39+
* All new features or bug fixes must include unit tests.
40+
* Use Go’s `testing` package; test files should be named `xxx_test.go`.
41+
* Example:
4242

43-
```go
44-
func TestAdd(t *testing.T) {
45-
result := Add(1, 2)
46-
if result != 3 {
47-
t.Errorf("expected 3, got %d", result)
48-
}
49-
}
50-
```
43+
```go
44+
func TestAdd(t *testing.T) {
45+
result := Add(1, 2)
46+
if result != 3 {
47+
t.Errorf("expected 3, got %d", result)
48+
}
49+
}
50+
```
5151
5252
4. **Update documentation**
5353
54-
* If your changes affect usage or APIs, update README or code comments.
54+
* If your changes affect usage or APIs, update README or code comments.
5555
5656
5. **Submit and create a Pull Request**
5757
58-
* Clearly describe:
58+
* Clearly describe:
5959
60-
* **What**: What changes are made
61-
* **Why**: Why the changes are needed
62-
* **How**: How it was implemented
63-
* **Testing**: How it was tested
64-
* Link related issues if applicable.
60+
* **What**: What changes are made
61+
* **Why**: Why the changes are needed
62+
* **How**: How it was implemented
63+
* **Testing**: How it was tested
64+
* Link related issues if applicable.
6565
6666
## Branch Naming Guidelines
6767
@@ -126,37 +126,37 @@ Thank you for contributing to Go-Spring!
126126
127127
2. **保持一致的代码风格**
128128
129-
* 遵循 Go 官方代码规范(使用 `gofmt``golint``go vet`)。
130-
* 推荐使用 [`golangci-lint`](https://github.com/golangci/golangci-lint) 进行本地检查。
129+
* 遵循 Go 官方代码规范(使用 `gofmt``golint``go vet`)。
130+
* 推荐使用 [`golangci-lint`](https://github.com/golangci/golangci-lint) 进行本地检查。
131131
132132
3. **编写测试用例**
133133
134-
* 所有新功能或 Bug 修复必须配备单元测试。
135-
* 使用 Go 内置 `testing` 包,测试文件命名为 `xxx_test.go`
136-
* 示例:
134+
* 所有新功能或 Bug 修复必须配备单元测试。
135+
* 使用 Go 内置 `testing` 包,测试文件命名为 `xxx_test.go`
136+
* 示例:
137137
138-
```go
139-
func TestAdd(t *testing.T) {
140-
result := Add(1, 2)
141-
if result != 3 {
142-
t.Errorf("expected 3, got %d", result)
143-
}
144-
}
145-
```
138+
```go
139+
func TestAdd(t *testing.T) {
140+
result := Add(1, 2)
141+
if result != 3 {
142+
t.Errorf("expected 3, got %d", result)
143+
}
144+
}
145+
```
146146
147147
4. **更新文档**
148148
149-
* 如果变更影响使用或接口,请同步更新 README 或代码注释。
149+
* 如果变更影响使用或接口,请同步更新 README 或代码注释。
150150
151151
5. **提交并创建 Pull Request**
152152
153-
* 清晰说明:
153+
* 清晰说明:
154154
155-
* **What**:本次修改的内容
156-
* **Why**:修改原因
157-
* **How**:实现方式
158-
* **Testing**:测试情况
159-
* 关联相关 Issue(如有)。
155+
* **What**:本次修改的内容
156+
* **Why**:修改原因
157+
* **How**:实现方式
158+
* **Testing**:测试情况
159+
* 关联相关 Issue(如有)。
160160
161161
## 分支命名规范
162162
@@ -185,4 +185,4 @@ Thank you for contributing to Go-Spring!
185185
* 可通过 GitHub Issue 提问或反馈
186186
* 参与项目讨论区交流
187187
188-
感谢你为 Go-Spring 做出的贡献!
188+
感谢你为 Go-Spring 做出的贡献!

conf/bind.go

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ import (
2323
"strconv"
2424
"strings"
2525

26-
"github.com/go-spring/log"
27-
"github.com/go-spring/spring-core/util"
26+
"github.com/go-spring/spring-base/util"
2827
)
2928

3029
var (
@@ -93,12 +92,12 @@ func (tag ParsedTag) String() string {
9392
func ParseTag(tag string) (ret ParsedTag, err error) {
9493
j := strings.LastIndex(tag, "}")
9594
if j <= 0 {
96-
err = log.FormatError(ErrInvalidSyntax, "parse tag '%s' error", tag)
95+
err = util.FormatError(ErrInvalidSyntax, "parse tag '%s' error", tag)
9796
return
9897
}
9998
k := strings.Index(tag, "${")
10099
if k < 0 {
101-
err = log.FormatError(ErrInvalidSyntax, "parse tag '%s' error", tag)
100+
err = util.FormatError(ErrInvalidSyntax, "parse tag '%s' error", tag)
102101
return
103102
}
104103
if i := strings.LastIndex(tag, ">>"); i > j {
@@ -145,7 +144,7 @@ func (param *BindParam) BindTag(tag string, validate reflect.StructTag) error {
145144
param.Tag = parsedTag
146145
return nil
147146
}
148-
return log.FormatError(ErrInvalidSyntax, "parse tag '%s' error", tag)
147+
return util.FormatError(ErrInvalidSyntax, "parse tag '%s' error", tag)
149148
}
150149
if parsedTag.Key == "ROOT" {
151150
parsedTag.Key = ""
@@ -180,16 +179,18 @@ type Filter interface {
180179
func BindValue(p Properties, v reflect.Value, t reflect.Type, param BindParam, filter Filter) (RetErr error) {
181180

182181
if !util.IsPropBindingTarget(t) {
183-
err := errors.New("target should be value type")
184-
return log.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
182+
err := util.FormatError(nil, "target should be value type")
183+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
185184
}
186185

187186
// run validation if "expr" tag is defined and no prior error
188187
defer func() {
189188
if RetErr == nil {
190189
tag, ok := param.Validate.Lookup("expr")
191190
if ok && len(tag) > 0 {
192-
RetErr = validateField(tag, v.Interface())
191+
if RetErr = validateField(tag, v.Interface()); RetErr != nil {
192+
RetErr = util.FormatError(RetErr, "validate path=%s type=%s error", param.Path, v.Type().String())
193+
}
193194
}
194195
}
195196
}()
@@ -200,8 +201,8 @@ func BindValue(p Properties, v reflect.Value, t reflect.Type, param BindParam, f
200201
case reflect.Slice:
201202
return bindSlice(p, v, t, param, filter)
202203
case reflect.Array:
203-
err := errors.New("use slice instead of array")
204-
return log.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
204+
err := util.FormatError(nil, "use slice instead of array")
205+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
205206
default: // for linter
206207
}
207208

@@ -216,7 +217,7 @@ func BindValue(p Properties, v reflect.Value, t reflect.Type, param BindParam, f
216217
// resolve property value (with default and references)
217218
val, err := resolve(p, param)
218219
if err != nil {
219-
return log.WrapError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
220+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
220221
}
221222

222223
// try converter function first
@@ -225,7 +226,7 @@ func BindValue(p Properties, v reflect.Value, t reflect.Type, param BindParam, f
225226
out := fnValue.Call([]reflect.Value{reflect.ValueOf(val)})
226227
if !out[1].IsNil() {
227228
err = out[1].Interface().(error)
228-
return log.WrapError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
229+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
229230
}
230231
v.Set(out[0])
231232
return nil
@@ -239,28 +240,28 @@ func BindValue(p Properties, v reflect.Value, t reflect.Type, param BindParam, f
239240
v.SetUint(u)
240241
return nil
241242
}
242-
return log.WrapError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
243+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
243244
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
244245
var i int64
245246
if i, err = strconv.ParseInt(val, 0, 0); err == nil {
246247
v.SetInt(i)
247248
return nil
248249
}
249-
return log.WrapError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
250+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
250251
case reflect.Float32, reflect.Float64:
251252
var f float64
252253
if f, err = strconv.ParseFloat(val, 64); err == nil {
253254
v.SetFloat(f)
254255
return nil
255256
}
256-
return log.WrapError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
257+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
257258
case reflect.Bool:
258259
var b bool
259260
if b, err = strconv.ParseBool(val); err == nil {
260261
v.SetBool(b)
261262
return nil
262263
}
263-
return log.WrapError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
264+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
264265
default:
265266
// treat everything else as string
266267
v.SetString(val)
@@ -283,7 +284,7 @@ func bindSlice(p Properties, v reflect.Value, t reflect.Type, param BindParam, f
283284
elemType := t.Elem()
284285
p, err := getSlice(p, elemType, param)
285286
if err != nil {
286-
return log.WrapError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
287+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
287288
}
288289

289290
slice := reflect.MakeSlice(t, 0, 0)
@@ -305,7 +306,7 @@ func bindSlice(p Properties, v reflect.Value, t reflect.Type, param BindParam, f
305306
break
306307
}
307308
if err != nil {
308-
return log.WrapError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
309+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
309310
}
310311
slice = reflect.Append(slice, subValue)
311312
}
@@ -336,13 +337,13 @@ func getSlice(p Properties, et reflect.Type, param BindParam) (Properties, error
336337
strVal = p.Get(param.Key)
337338
} else {
338339
if !param.Tag.HasDef {
339-
return nil, fmt.Errorf("property %q %w", param.Key, ErrNotExist)
340+
return nil, util.FormatError(nil, "property %q %w", param.Key, ErrNotExist)
340341
}
341342
if param.Tag.Def == "" {
342343
return nil, nil
343344
}
344345
if !util.IsPrimitiveValueType(et) && converters[et] == nil {
345-
return nil, fmt.Errorf("can't find converter for %s", et.String())
346+
return nil, util.FormatError(nil, "can't find converter for %s", et.String())
346347
}
347348
strVal = param.Tag.Def
348349
}
@@ -365,10 +366,10 @@ func getSlice(p Properties, et reflect.Type, param BindParam) (Properties, error
365366
} else if fn, ok := splitters[s]; ok && fn != nil {
366367
// use custom splitter function
367368
if arrVal, err = fn(strVal); err != nil {
368-
return nil, log.FormatError(err, "split %q error", strVal)
369+
return nil, util.FormatError(err, "split %q error", strVal)
369370
}
370371
} else {
371-
return nil, fmt.Errorf("unknown splitter '%s'", s)
372+
return nil, util.FormatError(nil, "unknown splitter '%s'", s)
372373
}
373374

374375
r := New()
@@ -396,8 +397,8 @@ func getSlice(p Properties, et reflect.Type, param BindParam) (Properties, error
396397
func bindMap(p Properties, v reflect.Value, t reflect.Type, param BindParam, filter Filter) error {
397398

398399
if param.Tag.HasDef && param.Tag.Def != "" {
399-
err := errors.New("map can't have a non-empty default value")
400-
return log.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
400+
err := util.FormatError(nil, "map can't have a non-empty default value")
401+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
401402
}
402403

403404
elemType := t.Elem()
@@ -416,13 +417,13 @@ func bindMap(p Properties, v reflect.Value, t reflect.Type, param BindParam, fil
416417
if param.Tag.HasDef {
417418
return nil
418419
}
419-
return fmt.Errorf("property %q %w", param.Key, ErrNotExist)
420+
return util.FormatError(nil, "property %q %w", param.Key, ErrNotExist)
420421
}
421422

422423
// fetch subkeys under the current key prefix
423424
keys, err := p.SubKeys(param.Key)
424425
if err != nil {
425-
return log.WrapError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
426+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
426427
}
427428

428429
for _, key := range keys {
@@ -464,8 +465,8 @@ func bindMap(p Properties, v reflect.Value, t reflect.Type, param BindParam, fil
464465
func bindStruct(p Properties, v reflect.Value, t reflect.Type, param BindParam, filter Filter) error {
465466

466467
if param.Tag.HasDef && param.Tag.Def != "" {
467-
err := errors.New("struct can't have a non-empty default value")
468-
return log.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
468+
err := util.FormatError(nil, "struct can't have a non-empty default value")
469+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
469470
}
470471

471472
for i := range t.NumField() {
@@ -484,12 +485,12 @@ func bindStruct(p Properties, v reflect.Value, t reflect.Type, param BindParam,
484485

485486
if tag, ok := ft.Tag.Lookup("value"); ok {
486487
if err := subParam.BindTag(tag, ft.Tag); err != nil {
487-
return log.WrapError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
488+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
488489
}
489490
if filter != nil {
490491
ret, err := filter.Do(fv.Addr().Interface(), subParam)
491492
if err != nil {
492-
return log.WrapError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
493+
return util.FormatError(err, "bind path=%s type=%s error", param.Path, v.Type().String())
493494
}
494495
if ret {
495496
continue
@@ -531,12 +532,12 @@ func resolve(p Properties, param BindParam) (string, error) {
531532
return resolveString(p, val)
532533
}
533534
if p.Has(param.Key) {
534-
return "", fmt.Errorf("property %q isn't simple value", param.Key)
535+
return "", util.FormatError(nil, "property %q isn't simple value", param.Key)
535536
}
536537
if param.Tag.HasDef {
537538
return resolveString(p, param.Tag.Def)
538539
}
539-
return "", fmt.Errorf("property %q %w", param.Key, ErrNotExist)
540+
return "", util.FormatError(nil, "property %q %w", param.Key, ErrNotExist)
540541
}
541542

542543
// resolveString expands property references of the form ${key}
@@ -590,7 +591,7 @@ func resolveString(p Properties, s string) (string, error) {
590591

591592
if end < 0 {
592593
err := ErrInvalidSyntax
593-
return "", log.FormatError(err, "resolve string %q error", s)
594+
return "", util.FormatError(err, "resolve string %q error", s)
594595
}
595596

596597
var param BindParam
@@ -599,13 +600,13 @@ func resolveString(p Properties, s string) (string, error) {
599600
// resolve the referenced property
600601
resolved, err := resolve(p, param)
601602
if err != nil {
602-
return "", log.WrapError(err, "resolve string %q error", s)
603+
return "", util.FormatError(err, "resolve string %q error", s)
603604
}
604605

605606
// resolve the remaining part of the string
606607
suffix, err := resolveString(p, s[end+1:])
607608
if err != nil {
608-
return "", log.WrapError(err, "resolve string %q error", s)
609+
return "", util.FormatError(err, "resolve string %q error", s)
609610
}
610611

611612
// combine: prefix + resolved + suffix

0 commit comments

Comments
 (0)