Skip to content

Commit 0c4918a

Browse files
committed
Replace fmt.Errorf and errors.New with pkg/errors
1 parent be0d13b commit 0c4918a

File tree

7 files changed

+46
-42
lines changed

7 files changed

+46
-42
lines changed

graphql.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package graphql
22

33
import (
44
"context"
5-
"fmt"
6-
75
"encoding/json"
6+
"fmt"
7+
"os"
88

9+
perrors "github.com/pkg/errors"
910
"github.com/qdentity/graphql-go/errors"
1011
"github.com/qdentity/graphql-go/internal/common"
1112
"github.com/qdentity/graphql-go/internal/exec"
@@ -52,6 +53,7 @@ func ParseSchema(schemaString string, resolver interface{}, opts ...SchemaOpt) (
5253
func MustParseSchema(schemaString string, resolver interface{}, opts ...SchemaOpt) *Schema {
5354
s, err := ParseSchema(schemaString, resolver, opts...)
5455
if err != nil {
56+
fmt.Fprintf(os.Stderr, "parse schema error: %+v\n\n", err)
5557
panic(err)
5658
}
5759
return s
@@ -165,12 +167,12 @@ func (s *Schema) exec(ctx context.Context, queryString string, operationName str
165167

166168
func getOperation(document *query.Document, operationName string) (*query.Operation, error) {
167169
if len(document.Operations) == 0 {
168-
return nil, fmt.Errorf("no operations in query document")
170+
return nil, perrors.Errorf("no operations in query document")
169171
}
170172

171173
if operationName == "" {
172174
if len(document.Operations) > 1 {
173-
return nil, fmt.Errorf("more than one operation in query document and no operation name given")
175+
return nil, perrors.Errorf("more than one operation in query document and no operation name given")
174176
}
175177
for _, op := range document.Operations {
176178
return op, nil // return the one and only operation
@@ -179,7 +181,7 @@ func getOperation(document *query.Document, operationName string) (*query.Operat
179181

180182
op := document.Operations.Get(operationName)
181183
if op == nil {
182-
return nil, fmt.Errorf("no operation with name %q", operationName)
184+
return nil, perrors.Errorf("no operation with name %q", operationName)
183185
}
184186
return op, nil
185187
}

id.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package graphql
22

33
import (
4-
"errors"
54
"strconv"
5+
6+
perrors "github.com/pkg/errors"
67
)
78

89
// ID represents GraphQL's "ID" scalar type. A custom type may be used instead.
@@ -20,7 +21,7 @@ func (id *ID) UnmarshalGraphQL(input interface{}) error {
2021
case int32:
2122
*id = ID(strconv.Itoa(int(input)))
2223
default:
23-
err = errors.New("wrong type")
24+
err = perrors.New("wrong type")
2425
}
2526
return err
2627
}

internal/exec/packer/packer.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package packer
22

33
import (
4-
"fmt"
54
"math"
65
"reflect"
76
"strings"
87

8+
perrors "github.com/pkg/errors"
99
"github.com/qdentity/graphql-go/errors"
1010
"github.com/qdentity/graphql-go/internal/common"
1111
"github.com/qdentity/graphql-go/internal/schema"
@@ -79,7 +79,7 @@ func (b *Builder) makePacker(schemaType common.Type, reflectType reflect.Type) (
7979
t, nonNull := unwrapNonNull(schemaType)
8080
if !nonNull {
8181
if reflectType.Kind() != reflect.Ptr {
82-
return nil, fmt.Errorf("%s is not a pointer", reflectType)
82+
return nil, perrors.Errorf("%s is not a pointer", reflectType)
8383
}
8484
elemType := reflectType.Elem()
8585
addPtr := true
@@ -104,7 +104,7 @@ func (b *Builder) makePacker(schemaType common.Type, reflectType reflect.Type) (
104104
func (b *Builder) makeNonNullPacker(schemaType common.Type, reflectType reflect.Type) (packer, error) {
105105
if u, ok := reflect.New(reflectType).Interface().(Unmarshaler); ok {
106106
if !u.ImplementsGraphQLType(schemaType.String()) {
107-
return nil, fmt.Errorf("can not unmarshal %s into %s", schemaType, reflectType)
107+
return nil, perrors.Errorf("can not unmarshal %s into %s", schemaType, reflectType)
108108
}
109109
return &unmarshalerPacker{
110110
ValueType: reflectType,
@@ -120,7 +120,7 @@ func (b *Builder) makeNonNullPacker(schemaType common.Type, reflectType reflect.
120120
case *schema.Enum:
121121
want := reflect.TypeOf("")
122122
if reflectType != want {
123-
return nil, fmt.Errorf("wrong type, expected %s", want)
123+
return nil, perrors.Errorf("wrong type, expected %s", want)
124124
}
125125
return &ValuePacker{
126126
ValueType: reflectType,
@@ -135,7 +135,7 @@ func (b *Builder) makeNonNullPacker(schemaType common.Type, reflectType reflect.
135135

136136
case *common.List:
137137
if reflectType.Kind() != reflect.Slice {
138-
return nil, fmt.Errorf("expected slice, got %s", reflectType)
138+
return nil, perrors.Errorf("expected slice, got %s", reflectType)
139139
}
140140
p := &listPacker{
141141
sliceType: reflectType,
@@ -146,7 +146,7 @@ func (b *Builder) makeNonNullPacker(schemaType common.Type, reflectType reflect.
146146
return p, nil
147147

148148
case *schema.Object, *schema.Interface, *schema.Union:
149-
return nil, fmt.Errorf("type of kind %s can not be used as input", t.Kind())
149+
return nil, perrors.Errorf("type of kind %s can not be used as input", t.Kind())
150150

151151
default:
152152
panic("unreachable")
@@ -161,7 +161,7 @@ func (b *Builder) MakeStructPacker(values common.InputValueList, typ reflect.Typ
161161
usePtr = true
162162
}
163163
if structType.Kind() != reflect.Struct {
164-
return nil, fmt.Errorf("expected struct or pointer to struct, got %s", typ)
164+
return nil, perrors.Errorf("expected struct or pointer to struct, got %s", typ)
165165
}
166166

167167
var fields []*structPackerField
@@ -173,10 +173,10 @@ func (b *Builder) MakeStructPacker(values common.InputValueList, typ reflect.Typ
173173

174174
sf, ok := structType.FieldByNameFunc(fx)
175175
if !ok {
176-
return nil, fmt.Errorf("missing argument %q", v.Name)
176+
return nil, perrors.Errorf("missing argument %q", v.Name)
177177
}
178178
if sf.PkgPath != "" {
179-
return nil, fmt.Errorf("field %q must be exported", sf.Name)
179+
return nil, perrors.Errorf("field %q must be exported", sf.Name)
180180
}
181181
fe.fieldIndex = sf.Index
182182

@@ -187,7 +187,7 @@ func (b *Builder) MakeStructPacker(values common.InputValueList, typ reflect.Typ
187187
}
188188

189189
if err := b.assignPacker(&fe.fieldPacker, ft, sf.Type); err != nil {
190-
return nil, fmt.Errorf("field %q: %s", sf.Name, err)
190+
return nil, perrors.Errorf("field %q: %s", sf.Name, err)
191191
}
192192

193193
fields = append(fields, fe)
@@ -296,7 +296,7 @@ func (p *ValuePacker) Pack(value interface{}) (reflect.Value, error) {
296296

297297
coerced, err := unmarshalInput(p.ValueType, value)
298298
if err != nil {
299-
return reflect.Value{}, fmt.Errorf("could not unmarshal %#v (%T) into %s: %s", value, value, p.ValueType, err)
299+
return reflect.Value{}, perrors.Errorf("could not unmarshal %#v (%T) into %s: %s", value, value, p.ValueType, err)
300300
}
301301
return reflect.ValueOf(coerced), nil
302302
}
@@ -332,13 +332,13 @@ func unmarshalInput(typ reflect.Type, input interface{}) (interface{}, error) {
332332
switch input := input.(type) {
333333
case int:
334334
if input < math.MinInt32 || input > math.MaxInt32 {
335-
return nil, fmt.Errorf("not a 32-bit integer")
335+
return nil, perrors.Errorf("not a 32-bit integer")
336336
}
337337
return int32(input), nil
338338
case float64:
339339
coerced := int32(input)
340340
if input < math.MinInt32 || input > math.MaxInt32 || float64(coerced) != input {
341-
return nil, fmt.Errorf("not a 32-bit integer")
341+
return nil, perrors.Errorf("not a 32-bit integer")
342342
}
343343
return coerced, nil
344344
}
@@ -352,7 +352,7 @@ func unmarshalInput(typ reflect.Type, input interface{}) (interface{}, error) {
352352
}
353353
}
354354

355-
return nil, fmt.Errorf("incompatible type")
355+
return nil, perrors.Errorf("incompatible type")
356356
}
357357

358358
func unwrapNonNull(t common.Type) (common.Type, bool) {

internal/exec/resolvable/resolvable.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"reflect"
77
"strings"
88

9+
perrors "github.com/pkg/errors"
910
"github.com/qdentity/graphql-go/internal/common"
1011
"github.com/qdentity/graphql-go/internal/exec/packer"
1112
"github.com/qdentity/graphql-go/internal/schema"
@@ -152,7 +153,7 @@ func (b *execBuilder) makeExec(t common.Type, resolverType reflect.Type) (Resolv
152153

153154
if !nonNull {
154155
if resolverType.Kind() != reflect.Ptr {
155-
return nil, fmt.Errorf("%s is not a pointer", resolverType)
156+
return nil, perrors.Errorf("%s is not a pointer", resolverType)
156157
}
157158
resolverType = resolverType.Elem()
158159
}
@@ -166,7 +167,7 @@ func (b *execBuilder) makeExec(t common.Type, resolverType reflect.Type) (Resolv
166167

167168
case *common.List:
168169
if resolverType.Kind() != reflect.Slice {
169-
return nil, fmt.Errorf("%s is not a slice", resolverType)
170+
return nil, perrors.Errorf("%s is not a slice", resolverType)
170171
}
171172
e := &List{}
172173
if err := b.assignExec(&e.Elem, t.OfType, resolverType.Elem()); err != nil {
@@ -194,15 +195,15 @@ func makeScalarExec(t *schema.Scalar, resolverType reflect.Type) (Resolvable, er
194195
implementsType = r.ImplementsGraphQLType(t.Name)
195196
}
196197
if !implementsType {
197-
return nil, fmt.Errorf("can not use %s as %s", resolverType, t.Name)
198+
return nil, perrors.Errorf("can not use %s as %s", resolverType, t.Name)
198199
}
199200
return &Scalar{}, nil
200201
}
201202

202203
func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, possibleTypes []*schema.Object, nonNull bool, resolverType reflect.Type) (*Object, error) {
203204
if !nonNull {
204205
if resolverType.Kind() != reflect.Ptr && resolverType.Kind() != reflect.Interface {
205-
return nil, fmt.Errorf("%s is not a pointer or interface", resolverType)
206+
return nil, perrors.Errorf("%s is not a pointer or interface", resolverType)
206207
}
207208
}
208209

@@ -216,13 +217,13 @@ func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, p
216217
if findMethod(reflect.PtrTo(resolverType), f.Name) != -1 {
217218
hint = " (hint: the method exists on the pointer type)"
218219
}
219-
return nil, fmt.Errorf("%s does not resolve %q: missing method for field %q%s", resolverType, typeName, f.Name, hint)
220+
return nil, perrors.Errorf("%s does not resolve %q: missing method for field %q%s", resolverType, typeName, f.Name, hint)
220221
}
221222

222223
m := resolverType.Method(methodIndex)
223224
fe, err := b.makeFieldExec(typeName, f, m, methodIndex, methodHasReceiver)
224225
if err != nil {
225-
return nil, fmt.Errorf("%s\n\treturned by (%s).%s", err, resolverType, m.Name)
226+
return nil, perrors.Errorf("%s\n\treturned by (%s).%s", err, resolverType, m.Name)
226227
}
227228
Fields[f.Name] = fe
228229
}
@@ -231,10 +232,10 @@ func (b *execBuilder) makeObjectExec(typeName string, fields schema.FieldList, p
231232
for _, impl := range possibleTypes {
232233
methodIndex := findMethod(resolverType, "To"+impl.Name)
233234
if methodIndex == -1 {
234-
return nil, fmt.Errorf("%s does not resolve %q: missing method %q to convert to %q", resolverType, typeName, "To"+impl.Name, impl.Name)
235+
return nil, perrors.Errorf("%s does not resolve %q: missing method %q to convert to %q", resolverType, typeName, "To"+impl.Name, impl.Name)
235236
}
236237
if resolverType.Method(methodIndex).Type.NumOut() != 2 {
237-
return nil, fmt.Errorf("%s does not resolve %q: method %q should return a value and a bool indicating success", resolverType, typeName, "To"+impl.Name)
238+
return nil, perrors.Errorf("%s does not resolve %q: method %q should return a value and a bool indicating success", resolverType, typeName, "To"+impl.Name)
238239
}
239240
a := &TypeAssertion{
240241
MethodIndex: methodIndex,
@@ -273,7 +274,7 @@ func (b *execBuilder) makeFieldExec(typeName string, f *schema.Field, m reflect.
273274
var argsPacker *packer.StructPacker
274275
if len(f.Args) > 0 {
275276
if len(in) == 0 {
276-
return nil, fmt.Errorf("must have parameter for field arguments")
277+
return nil, perrors.Errorf("must have parameter for field arguments")
277278
}
278279
var err error
279280
argsPacker, err = b.packerBuilder.MakeStructPacker(f.Args, in[0])
@@ -289,17 +290,17 @@ func (b *execBuilder) makeFieldExec(typeName string, f *schema.Field, m reflect.
289290
}
290291

291292
if len(in) > 0 {
292-
return nil, fmt.Errorf("too many parameters")
293+
return nil, perrors.Errorf("too many parameters")
293294
}
294295

295296
if m.Type.NumOut() > 2 {
296-
return nil, fmt.Errorf("too many return values")
297+
return nil, perrors.Errorf("too many return values")
297298
}
298299

299300
hasError := m.Type.NumOut() == 2
300301
if hasError {
301302
if m.Type.Out(1) != errorType {
302-
return nil, fmt.Errorf(`must have "error" as its second return value`)
303+
return nil, perrors.Errorf(`must have "error" as its second return value`)
303304
}
304305
}
305306

internal/exec/selected/selected.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package selected
22

33
import (
4-
"fmt"
54
"reflect"
65
"sync"
76

7+
perrors "github.com/pkg/errors"
88
"github.com/qdentity/graphql-go/errors"
99
"github.com/qdentity/graphql-go/internal/common"
1010
"github.com/qdentity/graphql-go/internal/exec/packer"
@@ -167,7 +167,7 @@ func applyFragment(r *Request, e *resolvable.Object, frag *query.Fragment) []Sel
167167
if frag.On.Name != "" && frag.On.Name != e.Name {
168168
a, ok := e.TypeAssertions[frag.On.Name]
169169
if !ok {
170-
panic(fmt.Errorf("%q does not implement %q", frag.On, e.Name)) // TODO proper error handling
170+
panic(perrors.Errorf("%q does not implement %q", frag.On, e.Name)) // TODO proper error handling
171171
}
172172

173173
return []Selection{&TypeAssertion{

relay/relay.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@ package relay
33
import (
44
"encoding/base64"
55
"encoding/json"
6-
"errors"
7-
"fmt"
86
"net/http"
97
"strings"
108

11-
graphql "github.com/qdentity/graphql-go"
9+
perrors "github.com/pkg/errors"
10+
"github.com/qdentity/graphql-go"
1211
)
1312

1413
func MarshalID(kind string, spec interface{}) graphql.ID {
1514
d, err := json.Marshal(spec)
1615
if err != nil {
17-
panic(fmt.Errorf("relay.MarshalID: %s", err))
16+
panic(perrors.Errorf("relay.MarshalID: %s", err))
1817
}
1918
return graphql.ID(base64.URLEncoding.EncodeToString(append([]byte(kind+":"), d...)))
2019
}
@@ -38,7 +37,7 @@ func UnmarshalSpec(id graphql.ID, v interface{}) error {
3837
}
3938
i := strings.IndexByte(string(s), ':')
4039
if i == -1 {
41-
return errors.New("invalid graphql.ID")
40+
return perrors.New("invalid graphql.ID")
4241
}
4342
return json.Unmarshal([]byte(s[i+1:]), v)
4443
}

time.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package graphql
22

33
import (
4-
"fmt"
54
"time"
5+
6+
perrors "github.com/pkg/errors"
67
)
78

89
// Time is a custom GraphQL type to represent an instant in time. It has to be added to a schema
@@ -31,6 +32,6 @@ func (t *Time) UnmarshalGraphQL(input interface{}) error {
3132
t.Time = time.Unix(int64(input), 0)
3233
return nil
3334
default:
34-
return fmt.Errorf("wrong type")
35+
return perrors.Errorf("wrong type")
3536
}
3637
}

0 commit comments

Comments
 (0)