Skip to content

Commit b412118

Browse files
lvan100lianghuan
authored andcommitted
refactor/util: Optimize error handling and naming conventions
1 parent 360e0aa commit b412118

File tree

10 files changed

+20
-19
lines changed

10 files changed

+20
-19
lines changed

conf/bind_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func PointConverter(val string) (image.Point, error) {
4949
}
5050

5151
func PointSplitter(str string) ([]string, error) {
52-
if !(strings.HasPrefix(str, "(") && strings.HasSuffix(str, ")")) {
52+
if !strings.HasPrefix(str, "(") || !strings.HasSuffix(str, ")") {
5353
return nil, errors.New("split error")
5454
}
5555
var ret []string

gs/examples/bookman/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ func runTest(ctx context.Context) error {
5454
if err != nil {
5555
panic(err)
5656
}
57-
defer resp.Body.Close()
57+
defer func() {
58+
err = resp.Body.Close()
59+
_ = err
60+
}()
5861
fmt.Print(string(b))
5962
time.Sleep(time.Millisecond * 400)
6063
})

gs/examples/bookman/src/dao/book_dao/book_dao.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ func (dao *BookDao) ListBooks() ([]Book, error) {
5959

6060
// GetBook retrieves a book by its ISBN.
6161
func (dao *BookDao) GetBook(isbn string) (Book, error) {
62-
r, _ := dao.Store[isbn]
62+
r, ok := dao.Store[isbn]
63+
_ = ok
6364
return r, nil
6465
}
6566

gs/internal/gs_arg/arg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func Index(n int, arg gs.Arg) gs.Arg {
7878

7979
// GetArgValue panics if called directly. IndexArg must be processed by ArgList.
8080
func (arg IndexArg) GetArgValue(ctx gs.ArgContext, t reflect.Type) (reflect.Value, error) {
81-
panic(util.UnimplementedMethod)
81+
panic(util.ErrUnimplementedMethod)
8282
}
8383

8484
// ValueArg represents a fixed-value argument.

gs/internal/gs_cond/cond.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func OnExpression(expression string) gs.Condition {
269269

270270
// Matches checks if the condition is met according to the provided context.
271271
func (c *onExpression) Matches(ctx gs.CondContext) (bool, error) {
272-
err := util.UnimplementedMethod
272+
err := util.ErrUnimplementedMethod
273273
return false, errutil.WrapError(err, "condition matches error: %s", c)
274274
}
275275

gs/internal/gs_cond/cond_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ func TestOnExpression(t *testing.T) {
337337
ctx := NewMockCondContext(ctrl)
338338
cond := OnExpression("1+1==2")
339339
_, err := cond.Matches(ctx)
340-
assert.True(t, errors.Is(err, util.UnimplementedMethod))
340+
assert.True(t, errors.Is(err, util.ErrUnimplementedMethod))
341341
}
342342

343343
func TestNot(t *testing.T) {

gs/internal/gs_conf/conf.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,8 @@ func (c *AppConfig) Refresh() (conf.Properties, error) {
107107

108108
var sources []PropertyCopier
109109
sources = append(sources, NewNamedPropertyCopier("sys", sysconf.Clone()))
110-
for _, file := range localFiles {
111-
sources = append(sources, file)
112-
}
113-
for _, file := range remoteFiles {
114-
sources = append(sources, file)
115-
}
110+
sources = append(sources, localFiles...)
111+
sources = append(sources, remoteFiles...)
116112
sources = append(sources, NewNamedPropertyCopier("remote", c.RemoteProp))
117113
sources = append(sources, NewNamedPropertyCopier("env", c.Environment))
118114
sources = append(sources, NewNamedPropertyCopier("cmd", c.CommandArgs))
@@ -156,9 +152,7 @@ func (c *BootConfig) Refresh() (conf.Properties, error) {
156152

157153
var sources []PropertyCopier
158154
sources = append(sources, NewNamedPropertyCopier("sys", sysconf.Clone()))
159-
for _, file := range localFiles {
160-
sources = append(sources, file)
161-
}
155+
sources = append(sources, localFiles...)
162156
sources = append(sources, NewNamedPropertyCopier("env", c.Environment))
163157
sources = append(sources, NewNamedPropertyCopier("cmd", c.CommandArgs))
164158

gs/internal/gs_core/injecting/injecting_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ type LazyA struct {
211211
}
212212

213213
type LazyB struct {
214+
// nolint
214215
dummy int `value:"${dummy:=9}"`
215216
}
216217

gs/internal/gs_dync/dync_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func TestDync(t *testing.T) {
147147
"config.s4.value": "123",
148148
})
149149
err = p.Refresh(prop)
150+
assert.Nil(t, err)
150151
assert.That(t, p.ObjectsCount()).Equal(2)
151152
assert.That(t, cfg.S1.Value.Value()).Equal(99)
152153
assert.That(t, cfg.S2.Value.Value()).Equal(456)
@@ -157,6 +158,7 @@ func TestDync(t *testing.T) {
157158
"config.s3.value": "xyz",
158159
})
159160
err = p.Refresh(prop)
161+
assert.Nil(t, err)
160162
assert.That(t, p.ObjectsCount()).Equal(2)
161163
assert.That(t, cfg.S1.Value.Value()).Equal(99)
162164
assert.That(t, cfg.S2.Value.Value()).Equal(456)

util/error.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"errors"
2121
)
2222

23-
// ForbiddenMethod throws this error when calling a method is prohibited.
24-
var ForbiddenMethod = errors.New("forbidden method")
23+
// ErrForbiddenMethod throws this error when calling a method is prohibited.
24+
var ErrForbiddenMethod = errors.New("forbidden method")
2525

26-
// UnimplementedMethod throws this error when calling an unimplemented method.
27-
var UnimplementedMethod = errors.New("unimplemented method")
26+
// ErrUnimplementedMethod throws this error when calling an unimplemented method.
27+
var ErrUnimplementedMethod = errors.New("unimplemented method")

0 commit comments

Comments
 (0)