Skip to content

Commit 4114515

Browse files
authored
refactor: use errors.New instead of fmt.Errorf (#3112)
1 parent 93f6366 commit 4114515

File tree

37 files changed

+122
-98
lines changed

37 files changed

+122
-98
lines changed

‎.golangci.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ linters-settings:
88
- (io.Writer).Write
99
- io.Copy
1010
- io.WriteString
11+
perfsprint:
12+
int-conversion: false
13+
err-error: false
14+
errorf: true
15+
sprintf1: false
16+
strconcat: false
1117
revive:
1218
enable-all-rules: false
1319
rules:
@@ -45,6 +51,7 @@ linters:
4551
- ineffassign
4652
- misspell
4753
- nakedret
54+
- perfsprint
4855
- prealloc
4956
- revive
5057
- staticcheck

‎_examples/dataloader/resolvers.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"fmt"
88
"math/rand"
9+
"strconv"
910
"time"
1011
)
1112

@@ -70,7 +71,7 @@ func (r *queryResolver) Customers(ctx context.Context) ([]*Customer, error) {
7071
func (r *queryResolver) Torture1d(ctx context.Context, customerIds []int) ([]*Customer, error) {
7172
result := make([]*Customer, len(customerIds))
7273
for i, id := range customerIds {
73-
result[i] = &Customer{ID: id, Name: fmt.Sprintf("%d", i), AddressID: rand.Int() % 10}
74+
result[i] = &Customer{ID: id, Name: strconv.Itoa(i), AddressID: rand.Int() % 10}
7475
}
7576
return result, nil
7677
}

‎_examples/scalars/model/model.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ type Point struct {
5656
func (p *Point) UnmarshalGQL(v any) error {
5757
pointStr, ok := v.(string)
5858
if !ok {
59-
return fmt.Errorf("points must be strings")
59+
return errors.New("points must be strings")
6060
}
6161

6262
parts := strings.Split(pointStr, ",")
6363

6464
if len(parts) != 2 {
65-
return fmt.Errorf("points must have 2 parts")
65+
return errors.New("points must have 2 parts")
6666
}
6767

6868
var err error
@@ -108,7 +108,7 @@ func MarshalID(id external.ObjectID) graphql.Marshaler {
108108
func UnmarshalID(v any) (external.ObjectID, error) {
109109
str, ok := v.(string)
110110
if !ok {
111-
return 0, fmt.Errorf("ids must be strings")
111+
return 0, errors.New("ids must be strings")
112112
}
113113
i, err := strconv.Atoi(str[1 : len(str)-1])
114114
return external.ObjectID(i), err
@@ -166,7 +166,7 @@ func (e Tier) String() string {
166166
func (e *Tier) UnmarshalGQL(v any) error {
167167
str, ok := v.(string)
168168
if !ok {
169-
return fmt.Errorf("enums must be strings")
169+
return errors.New("enums must be strings")
170170
}
171171

172172
var err error

‎_examples/todo/todo.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package todo
55
import (
66
"context"
77
"errors"
8-
"fmt"
98
"time"
109

1110
"github.com/mitchellh/mapstructure"
@@ -47,11 +46,11 @@ func New() Config {
4746
case RoleOwner:
4847
ownable, isOwnable := obj.(Ownable)
4948
if !isOwnable {
50-
return nil, fmt.Errorf("obj cant be owned")
49+
return nil, errors.New("obj cant be owned")
5150
}
5251

5352
if ownable.Owner().ID != getUserId(ctx) {
54-
return nil, fmt.Errorf("you don't own that")
53+
return nil, errors.New("you don't own that")
5554
}
5655
}
5756

‎client/websocket.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package client
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io"
78
"net/http/httptest"
@@ -127,7 +128,7 @@ func (p *Client) WebsocketWithPayload(query string, initPayload map[string]any,
127128
case connectionKaMsg:
128129
continue
129130
case errorMsg:
130-
return fmt.Errorf(string(op.Payload))
131+
return errors.New(string(op.Payload))
131132
default:
132133
return fmt.Errorf("expected data message, got %#v", op)
133134
}

‎codegen/config/binder.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ var (
9999
func (b *Binder) DefaultUserObject(name string) (types.Type, error) {
100100
models := b.cfg.Models[name].Model
101101
if len(models) == 0 {
102-
return nil, fmt.Errorf(name + " not found in typemap")
102+
return nil, fmt.Errorf("%s not found in typemap", name)
103103
}
104104

105105
if models[0] == "map[string]interface{}" {
@@ -125,7 +125,7 @@ func (b *Binder) DefaultUserObject(name string) (types.Type, error) {
125125

126126
func (b *Binder) FindObject(pkgName string, typeName string) (types.Object, error) {
127127
if pkgName == "" {
128-
return nil, fmt.Errorf("package cannot be nil")
128+
return nil, errors.New("package cannot be nil")
129129
}
130130

131131
pkg := b.pkgs.LoadWithTypes(pkgName)

‎codegen/config/config.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
67
"go/types"
78
"io"
@@ -564,11 +565,11 @@ func (c *Config) check() error {
564565
Declaree: "federation",
565566
})
566567
if c.Federation.ImportPath() != c.Exec.ImportPath() {
567-
return fmt.Errorf("federation and exec must be in the same package")
568+
return errors.New("federation and exec must be in the same package")
568569
}
569570
}
570571
if c.Federated {
571-
return fmt.Errorf("federated has been removed, instead use\nfederation:\n filename: path/to/federated.go")
572+
return errors.New("federated has been removed, instead use\nfederation:\n filename: path/to/federated.go")
572573
}
573574

574575
for importPath, pkg := range fileList {

‎codegen/config/exec.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"errors"
45
"fmt"
56
"go/types"
67
"path/filepath"
@@ -38,23 +39,23 @@ func (r *ExecConfig) Check() error {
3839
switch r.Layout {
3940
case ExecLayoutSingleFile:
4041
if r.Filename == "" {
41-
return fmt.Errorf("filename must be specified when using single-file layout")
42+
return errors.New("filename must be specified when using single-file layout")
4243
}
4344
if !strings.HasSuffix(r.Filename, ".go") {
44-
return fmt.Errorf("filename should be path to a go source file when using single-file layout")
45+
return errors.New("filename should be path to a go source file when using single-file layout")
4546
}
4647
r.Filename = abs(r.Filename)
4748
case ExecLayoutFollowSchema:
4849
if r.DirName == "" {
49-
return fmt.Errorf("dir must be specified when using follow-schema layout")
50+
return errors.New("dir must be specified when using follow-schema layout")
5051
}
5152
r.DirName = abs(r.DirName)
5253
default:
5354
return fmt.Errorf("invalid layout %s", r.Layout)
5455
}
5556

5657
if strings.ContainsAny(r.Package, "./\\") {
57-
return fmt.Errorf("package should be the output package name only, do not include the output filename")
58+
return errors.New("package should be the output package name only, do not include the output filename")
5859
}
5960

6061
if r.Package == "" && r.Dir() != "" {

‎codegen/config/package.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package config
22

33
import (
4-
"fmt"
4+
"errors"
55
"go/types"
66
"path/filepath"
77
"strings"
@@ -44,13 +44,13 @@ func (c *PackageConfig) IsDefined() bool {
4444

4545
func (c *PackageConfig) Check() error {
4646
if strings.ContainsAny(c.Package, "./\\") {
47-
return fmt.Errorf("package should be the output package name only, do not include the output filename")
47+
return errors.New("package should be the output package name only, do not include the output filename")
4848
}
4949
if c.Filename == "" {
50-
return fmt.Errorf("filename must be specified")
50+
return errors.New("filename must be specified")
5151
}
5252
if !strings.HasSuffix(c.Filename, ".go") {
53-
return fmt.Errorf("filename should be path to a go source file")
53+
return errors.New("filename should be path to a go source file")
5454
}
5555

5656
c.Filename = abs(c.Filename)

‎codegen/config/resolver.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"errors"
45
"fmt"
56
"go/types"
67
"path/filepath"
@@ -59,7 +60,7 @@ func (r *ResolverConfig) Check() error {
5960
}
6061

6162
if strings.ContainsAny(r.Package, "./\\") {
62-
return fmt.Errorf("package should be the output package name only, do not include the output filename")
63+
return errors.New("package should be the output package name only, do not include the output filename")
6364
}
6465

6566
if r.Package == "" && r.Dir() != "" {

0 commit comments

Comments
 (0)