Skip to content

Commit 363d9c3

Browse files
authored
Merge pull request #267 from mrhenry/lazy-invariant
Don't format the error message unless it is really needed
2 parents 77b1ae5 + d951837 commit 363d9c3

File tree

4 files changed

+60
-53
lines changed

4 files changed

+60
-53
lines changed

definition.go

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -269,20 +269,20 @@ func NewScalar(config ScalarConfig) *Scalar {
269269
st.PrivateName = config.Name
270270
st.PrivateDescription = config.Description
271271

272-
err = invariant(
272+
err = invariantf(
273273
config.Serialize != nil,
274-
fmt.Sprintf(`%v must provide "serialize" function. If this custom Scalar is `+
274+
`%v must provide "serialize" function. If this custom Scalar is `+
275275
`also used as an input type, ensure "parseValue" and "parseLiteral" `+
276-
`functions are also provided.`, st),
276+
`functions are also provided.`, st,
277277
)
278278
if err != nil {
279279
st.err = err
280280
return st
281281
}
282282
if config.ParseValue != nil || config.ParseLiteral != nil {
283-
err = invariant(
283+
err = invariantf(
284284
config.ParseValue != nil && config.ParseLiteral != nil,
285-
fmt.Sprintf(`%v must provide both "parseValue" and "parseLiteral" functions.`, st),
285+
`%v must provide both "parseValue" and "parseLiteral" functions.`, st,
286286
)
287287
if err != nil {
288288
st.err = err
@@ -499,20 +499,20 @@ func defineInterfaces(ttype *Object, interfaces []*Interface) ([]*Interface, err
499499
return ifaces, nil
500500
}
501501
for _, iface := range interfaces {
502-
err := invariant(
502+
err := invariantf(
503503
iface != nil,
504-
fmt.Sprintf(`%v may only implement Interface types, it cannot implement: %v.`, ttype, iface),
504+
`%v may only implement Interface types, it cannot implement: %v.`, ttype, iface,
505505
)
506506
if err != nil {
507507
return ifaces, err
508508
}
509509
if iface.ResolveType != nil {
510-
err = invariant(
510+
err = invariantf(
511511
iface.ResolveType != nil,
512-
fmt.Sprintf(`Interface Type %v does not provide a "resolveType" function `+
512+
`Interface Type %v does not provide a "resolveType" function `+
513513
`and implementing Type %v does not provide a "isTypeOf" `+
514514
`function. There is no way to resolve this implementing type `+
515-
`during execution.`, iface, ttype),
515+
`during execution.`, iface, ttype,
516516
)
517517
if err != nil {
518518
return ifaces, err
@@ -527,9 +527,9 @@ func defineInterfaces(ttype *Object, interfaces []*Interface) ([]*Interface, err
527527
func defineFieldMap(ttype Named, fieldMap Fields) (FieldDefinitionMap, error) {
528528
resultFieldMap := FieldDefinitionMap{}
529529

530-
err := invariant(
530+
err := invariantf(
531531
len(fieldMap) > 0,
532-
fmt.Sprintf(`%v fields must be an object with field names as keys or a function which return such an object.`, ttype),
532+
`%v fields must be an object with field names as keys or a function which return such an object.`, ttype,
533533
)
534534
if err != nil {
535535
return resultFieldMap, err
@@ -539,9 +539,9 @@ func defineFieldMap(ttype Named, fieldMap Fields) (FieldDefinitionMap, error) {
539539
if field == nil {
540540
continue
541541
}
542-
err = invariant(
542+
err = invariantf(
543543
field.Type != nil,
544-
fmt.Sprintf(`%v.%v field type must be Output Type but got: %v.`, ttype, fieldName, field.Type),
544+
`%v.%v field type must be Output Type but got: %v.`, ttype, fieldName, field.Type,
545545
)
546546
if err != nil {
547547
return resultFieldMap, err
@@ -567,16 +567,16 @@ func defineFieldMap(ttype Named, fieldMap Fields) (FieldDefinitionMap, error) {
567567
if err != nil {
568568
return resultFieldMap, err
569569
}
570-
err = invariant(
570+
err = invariantf(
571571
arg != nil,
572-
fmt.Sprintf(`%v.%v args must be an object with argument names as keys.`, ttype, fieldName),
572+
`%v.%v args must be an object with argument names as keys.`, ttype, fieldName,
573573
)
574574
if err != nil {
575575
return resultFieldMap, err
576576
}
577-
err = invariant(
577+
err = invariantf(
578578
arg.Type != nil,
579-
fmt.Sprintf(`%v.%v(%v:) argument type must be Input Type but got: %v.`, ttype, fieldName, argName, arg.Type),
579+
`%v.%v(%v:) argument type must be Input Type but got: %v.`, ttype, fieldName, argName, arg.Type,
580580
)
581581
if err != nil {
582582
return resultFieldMap, err
@@ -856,30 +856,30 @@ func NewUnion(config UnionConfig) *Union {
856856
objectType.PrivateDescription = config.Description
857857
objectType.ResolveType = config.ResolveType
858858

859-
err = invariant(
859+
err = invariantf(
860860
len(config.Types) > 0,
861-
fmt.Sprintf(`Must provide Array of types for Union %v.`, config.Name),
861+
`Must provide Array of types for Union %v.`, config.Name,
862862
)
863863
if err != nil {
864864
objectType.err = err
865865
return objectType
866866
}
867867
for _, ttype := range config.Types {
868-
err := invariant(
868+
err := invariantf(
869869
ttype != nil,
870-
fmt.Sprintf(`%v may only contain Object types, it cannot contain: %v.`, objectType, ttype),
870+
`%v may only contain Object types, it cannot contain: %v.`, objectType, ttype,
871871
)
872872
if err != nil {
873873
objectType.err = err
874874
return objectType
875875
}
876876
if objectType.ResolveType == nil {
877-
err = invariant(
877+
err = invariantf(
878878
ttype.IsTypeOf != nil,
879-
fmt.Sprintf(`Union Type %v does not provide a "resolveType" function `+
879+
`Union Type %v does not provide a "resolveType" function `+
880880
`and possible Type %v does not provide a "isTypeOf" `+
881881
`function. There is no way to resolve this possible type `+
882-
`during execution.`, objectType, ttype),
882+
`during execution.`, objectType, ttype,
883883
)
884884
if err != nil {
885885
objectType.err = err
@@ -980,19 +980,19 @@ func NewEnum(config EnumConfig) *Enum {
980980
func (gt *Enum) defineEnumValues(valueMap EnumValueConfigMap) ([]*EnumValueDefinition, error) {
981981
values := []*EnumValueDefinition{}
982982

983-
err := invariant(
983+
err := invariantf(
984984
len(valueMap) > 0,
985-
fmt.Sprintf(`%v values must be an object with value names as keys.`, gt),
985+
`%v values must be an object with value names as keys.`, gt,
986986
)
987987
if err != nil {
988988
return values, err
989989
}
990990

991991
for valueName, valueConfig := range valueMap {
992-
err := invariant(
992+
err := invariantf(
993993
valueConfig != nil,
994-
fmt.Sprintf(`%v.%v must refer to an object with a "value" key `+
995-
`representing an internal value but got: %v.`, gt, valueName, valueConfig),
994+
`%v.%v must refer to an object with a "value" key `+
995+
`representing an internal value but got: %v.`, gt, valueName, valueConfig,
996996
)
997997
if err != nil {
998998
return values, err
@@ -1173,9 +1173,9 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
11731173
}
11741174
resultFieldMap := InputObjectFieldMap{}
11751175

1176-
err := invariant(
1176+
err := invariantf(
11771177
len(fieldMap) > 0,
1178-
fmt.Sprintf(`%v fields must be an object with field names as keys or a function which return such an object.`, gt),
1178+
`%v fields must be an object with field names as keys or a function which return such an object.`, gt,
11791179
)
11801180
if err != nil {
11811181
gt.err = err
@@ -1190,9 +1190,9 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
11901190
if err != nil {
11911191
continue
11921192
}
1193-
err = invariant(
1193+
err = invariantf(
11941194
fieldConfig.Type != nil,
1195-
fmt.Sprintf(`%v.%v field type must be Input Type but got: %v.`, gt, fieldName, fieldConfig.Type),
1195+
`%v.%v field type must be Input Type but got: %v.`, gt, fieldName, fieldConfig.Type,
11961196
)
11971197
if err != nil {
11981198
gt.err = err
@@ -1253,7 +1253,7 @@ type List struct {
12531253
func NewList(ofType Type) *List {
12541254
gl := &List{}
12551255

1256-
err := invariant(ofType != nil, fmt.Sprintf(`Can only create List of a Type but got: %v.`, ofType))
1256+
err := invariantf(ofType != nil, `Can only create List of a Type but got: %v.`, ofType)
12571257
if err != nil {
12581258
gl.err = err
12591259
return gl
@@ -1306,7 +1306,7 @@ func NewNonNull(ofType Type) *NonNull {
13061306
gl := &NonNull{}
13071307

13081308
_, isOfTypeNonNull := ofType.(*NonNull)
1309-
err := invariant(ofType != nil && !isOfTypeNonNull, fmt.Sprintf(`Can only create NonNull of a Nullable Type but got: %v.`, ofType))
1309+
err := invariantf(ofType != nil && !isOfTypeNonNull, `Can only create NonNull of a Nullable Type but got: %v.`, ofType)
13101310
if err != nil {
13111311
gl.err = err
13121312
return gl
@@ -1333,8 +1333,8 @@ func (gl *NonNull) Error() error {
13331333
var NameRegExp, _ = regexp.Compile("^[_a-zA-Z][_a-zA-Z0-9]*$")
13341334

13351335
func assertValidName(name string) error {
1336-
return invariant(
1336+
return invariantf(
13371337
NameRegExp.MatchString(name),
1338-
fmt.Sprintf(`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "%v" does not.`, name),
1339-
)
1338+
`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "%v" does not.`, name)
1339+
13401340
}

executor.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,9 @@ func completeValue(eCtx *executionContext, returnType Type, fieldASTs []*ast.Fie
668668
}
669669

670670
// Not reachable. All possible output types have been considered.
671-
err := invariant(false,
672-
fmt.Sprintf(`Cannot complete value of unexpected type "%v."`, returnType),
673-
)
671+
err := invariantf(false,
672+
`Cannot complete value of unexpected type "%v."`, returnType)
673+
674674
if err != nil {
675675
panic(gqlerrors.FormatError(err))
676676
}
@@ -781,11 +781,11 @@ func completeListValue(eCtx *executionContext, returnType *List, fieldASTs []*as
781781
if info.ParentType != nil {
782782
parentTypeName = info.ParentType.Name()
783783
}
784-
err := invariant(
784+
err := invariantf(
785785
resultVal.IsValid() && resultVal.Type().Kind() == reflect.Slice,
786-
fmt.Sprintf("User Error: expected iterable, but did not find one "+
787-
"for field %v.%v.", parentTypeName, info.FieldName),
788-
)
786+
"User Error: expected iterable, but did not find one "+
787+
"for field %v.%v.", parentTypeName, info.FieldName)
788+
789789
if err != nil {
790790
panic(gqlerrors.FormatError(err))
791791
}

schema.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,10 @@ func typeMapReducer(schema *Schema, typeMap TypeMap, objectType Type) (TypeMap,
288288
}
289289

290290
if mappedObjectType, ok := typeMap[objectType.Name()]; ok {
291-
err := invariant(
291+
err = invariantf(
292292
mappedObjectType == objectType,
293-
fmt.Sprintf(`Schema must contain unique named types but contains multiple types named "%v".`, objectType.Name()),
294-
)
293+
`Schema must contain unique named types but contains multiple types named "%v".`, objectType.Name())
294+
295295
if err != nil {
296296
return typeMap, err
297297
}
@@ -408,11 +408,11 @@ func assertObjectImplementsInterface(schema *Schema, object *Object, iface *Inte
408408
ifaceField := ifaceFieldMap[fieldName]
409409

410410
// Assert interface field exists on object.
411-
err := invariant(
411+
err := invariantf(
412412
objectField != nil,
413-
fmt.Sprintf(`"%v" expects field "%v" but "%v" does not `+
414-
`provide it.`, iface, fieldName, object),
415-
)
413+
`"%v" expects field "%v" but "%v" does not `+
414+
`provide it.`, iface, fieldName, object)
415+
416416
if err != nil {
417417
return err
418418
}

values.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,10 @@ func invariant(condition bool, message string) error {
467467
}
468468
return nil
469469
}
470+
471+
func invariantf(condition bool, format string, a ...interface{}) error {
472+
if !condition {
473+
return gqlerrors.NewFormattedError(fmt.Sprintf(format, a...))
474+
}
475+
return nil
476+
}

0 commit comments

Comments
 (0)