Skip to content

Commit

Permalink
Use errors.New instead of fmt.Errorf w/o args (#3004)
Browse files Browse the repository at this point in the history
  • Loading branch information
sashamelentyev committed Nov 10, 2022
1 parent 4a32cbc commit b2acb1c
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 22 deletions.
4 changes: 2 additions & 2 deletions internal/codegenerator/parse_req_test.go
Expand Up @@ -2,7 +2,7 @@ package codegenerator_test

import (
"bytes"
"fmt"
"errors"
"io"
"strings"
"testing"
Expand Down Expand Up @@ -66,5 +66,5 @@ type invalidReader struct {
}

func (*invalidReader) Read(p []byte) (int, error) {
return 0, fmt.Errorf("invalid reader")
return 0, errors.New("invalid reader")
}
5 changes: 3 additions & 2 deletions internal/descriptor/services.go
@@ -1,6 +1,7 @@
package descriptor

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -132,7 +133,7 @@ func (r *Registry) newMethod(svc *Service, md *descriptorpb.MethodDescriptorProt
tmpl := parsed.Compile()

if md.GetClientStreaming() && len(tmpl.Fields) > 0 {
return nil, fmt.Errorf("cannot use path parameter in client streaming")
return nil, errors.New("cannot use path parameter in client streaming")
}

b := &Binding{
Expand Down Expand Up @@ -259,13 +260,13 @@ func (r *Registry) newParam(meth *Method, path string) (Parameter, error) {
}

func (r *Registry) newBody(meth *Method, path string) (*Body, error) {
msg := meth.RequestType
switch path {
case "":
return nil, nil
case "*":
return &Body{FieldPath: nil}, nil
}
msg := meth.RequestType
fields, err := r.resolveFieldPath(msg, path, false)
if err != nil {
return nil, err
Expand Down
13 changes: 6 additions & 7 deletions internal/httprule/parse.go
@@ -1,6 +1,7 @@
package httprule

import (
"errors"
"fmt"
"strings"
)
Expand Down Expand Up @@ -164,7 +165,7 @@ func (p *parser) segment() (segment, error) {

v, err := p.variable()
if err != nil {
return nil, fmt.Errorf("segment neither wildcards, literal or variable: %v", err)
return nil, fmt.Errorf("segment neither wildcards, literal or variable: %w", err)
}
return v, err
}
Expand Down Expand Up @@ -213,7 +214,7 @@ func (p *parser) fieldPath() (string, error) {
}
components := []string{c}
for {
if _, err = p.accept("."); err != nil {
if _, err := p.accept("."); err != nil {
return strings.Join(components, "."), nil
}
c, err := p.accept(typeIdent)
Expand All @@ -237,10 +238,8 @@ const (
typeEOF = termType("$")
)

const (
// eof is the terminal symbol which always appears at the end of token sequence.
eof = "\u0000"
)
// eof is the terminal symbol which always appears at the end of token sequence.
const eof = "\u0000"

// accept tries to accept a token in "p".
// This function consumes a token and returns it if it matches to the specified "term".
Expand Down Expand Up @@ -334,7 +333,7 @@ func expectPChars(t string) error {
// expectIdent determines if "ident" is a valid identifier in .proto schema ([[:alpha:]_][[:alphanum:]_]*).
func expectIdent(ident string) error {
if ident == "" {
return fmt.Errorf("empty identifier")
return errors.New("empty identifier")
}
for pos, r := range ident {
switch {
Expand Down
6 changes: 2 additions & 4 deletions protoc-gen-grpc-gateway/internal/gengateway/generator.go
Expand Up @@ -13,9 +13,7 @@ import (
"google.golang.org/protobuf/types/pluginpb"
)

var (
errNoTargetService = errors.New("no target service defined in the file")
)
var errNoTargetService = errors.New("no target service defined in the file")

type generator struct {
reg *descriptor.Registry
Expand Down Expand Up @@ -76,7 +74,7 @@ func (g *generator) Generate(targets []*descriptor.File) ([]*descriptor.Response
glog.V(1).Infof("Processing %s", file.GetName())

code, err := g.generate(file)
if err == errNoTargetService {
if errors.Is(err, errNoTargetService) {
glog.V(1).Infof("%s: %v", file.GetName(), err)
continue
}
Expand Down
4 changes: 2 additions & 2 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Expand Up @@ -1076,7 +1076,7 @@ func renderServices(services []*descriptor.Service, paths openapiPathsObject, re
case descriptorpb.FieldDescriptorProto_TYPE_GROUP, descriptorpb.FieldDescriptorProto_TYPE_MESSAGE:
if descriptor.IsWellKnownType(parameter.Target.GetTypeName()) {
if parameter.IsRepeated() {
return fmt.Errorf("only primitive and enum types are allowed in repeated path parameters")
return errors.New("only primitive and enum types are allowed in repeated path parameters")
}
schema := schemaOfField(parameter.Target, reg, customRefs)
paramType = schema.Type
Expand Down Expand Up @@ -2113,7 +2113,7 @@ func updateOpenAPIDataFromComments(reg *descriptor.Registry, swaggerObject inter
}
if len(description) > 0 {
if !descriptionValue.CanSet() {
return fmt.Errorf("encountered object type with a summary, but no description")
return errors.New("encountered object type with a summary, but no description")
}
// overrides the schema value only if it's empty
// keep the comment precedence when updating the package definition
Expand Down
6 changes: 3 additions & 3 deletions runtime/errors_test.go
Expand Up @@ -2,7 +2,7 @@ package runtime_test

import (
"context"
"fmt"
"errors"
"net/http"
"net/http/httptest"
"strconv"
Expand Down Expand Up @@ -32,7 +32,7 @@ func TestDefaultHTTPError(t *testing.T) {
details string
}{
{
err: fmt.Errorf("example error"),
err: errors.New("example error"),
status: http.StatusInternalServerError,
marshaler: &runtime.JSONPb{},
contentType: "application/json",
Expand All @@ -54,7 +54,7 @@ func TestDefaultHTTPError(t *testing.T) {
details: "type.googleapis.com/google.rpc.PreconditionFailure",
},
{
err: fmt.Errorf("example error"),
err: errors.New("example error"),
status: http.StatusInternalServerError,
marshaler: &CustomMarshaler{&runtime.JSONPb{}},
contentType: "Custom-Content-Type",
Expand Down
4 changes: 2 additions & 2 deletions runtime/fieldmask_test.go
Expand Up @@ -2,7 +2,7 @@ package runtime

import (
"bytes"
"fmt"
"errors"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -260,7 +260,7 @@ func TestFieldMaskErrors(t *testing.T) {
{
name: "object under scalar",
input: `{"uuid": {"a": "x"}}`,
expectedErr: fmt.Errorf("JSON structure did not match request type"),
expectedErr: errors.New("JSON structure did not match request type"),
},
} {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit b2acb1c

Please sign in to comment.