Skip to content

Commit b16d2fd

Browse files
committed
Don't format the error message unless it is really needed
1 parent 9b5a661 commit b16d2fd

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
@@ -482,20 +482,20 @@ func defineInterfaces(ttype *Object, interfaces []*Interface) ([]*Interface, err
482482
return ifaces, nil
483483
}
484484
for _, iface := range interfaces {
485-
err := invariant(
485+
err := invariantf(
486486
iface != nil,
487-
fmt.Sprintf(`%v may only implement Interface types, it cannot implement: %v.`, ttype, iface),
487+
`%v may only implement Interface types, it cannot implement: %v.`, ttype, iface,
488488
)
489489
if err != nil {
490490
return ifaces, err
491491
}
492492
if iface.ResolveType != nil {
493-
err = invariant(
493+
err = invariantf(
494494
iface.ResolveType != nil,
495-
fmt.Sprintf(`Interface Type %v does not provide a "resolveType" function `+
495+
`Interface Type %v does not provide a "resolveType" function `+
496496
`and implementing Type %v does not provide a "isTypeOf" `+
497497
`function. There is no way to resolve this implementing type `+
498-
`during execution.`, iface, ttype),
498+
`during execution.`, iface, ttype,
499499
)
500500
if err != nil {
501501
return ifaces, err
@@ -518,9 +518,9 @@ func defineFieldMap(ttype Named, fields interface{}) (FieldDefinitionMap, error)
518518

519519
resultFieldMap := FieldDefinitionMap{}
520520

521-
err := invariant(
521+
err := invariantf(
522522
len(fieldMap) > 0,
523-
fmt.Sprintf(`%v fields must be an object with field names as keys or a function which return such an object.`, ttype),
523+
`%v fields must be an object with field names as keys or a function which return such an object.`, ttype,
524524
)
525525
if err != nil {
526526
return resultFieldMap, err
@@ -530,9 +530,9 @@ func defineFieldMap(ttype Named, fields interface{}) (FieldDefinitionMap, error)
530530
if field == nil {
531531
continue
532532
}
533-
err = invariant(
533+
err = invariantf(
534534
field.Type != nil,
535-
fmt.Sprintf(`%v.%v field type must be Output Type but got: %v.`, ttype, fieldName, field.Type),
535+
`%v.%v field type must be Output Type but got: %v.`, ttype, fieldName, field.Type,
536536
)
537537
if err != nil {
538538
return resultFieldMap, err
@@ -558,16 +558,16 @@ func defineFieldMap(ttype Named, fields interface{}) (FieldDefinitionMap, error)
558558
if err != nil {
559559
return resultFieldMap, err
560560
}
561-
err = invariant(
561+
err = invariantf(
562562
arg != nil,
563-
fmt.Sprintf(`%v.%v args must be an object with argument names as keys.`, ttype, fieldName),
563+
`%v.%v args must be an object with argument names as keys.`, ttype, fieldName,
564564
)
565565
if err != nil {
566566
return resultFieldMap, err
567567
}
568-
err = invariant(
568+
err = invariantf(
569569
arg.Type != nil,
570-
fmt.Sprintf(`%v.%v(%v:) argument type must be Input Type but got: %v.`, ttype, fieldName, argName, arg.Type),
570+
`%v.%v(%v:) argument type must be Input Type but got: %v.`, ttype, fieldName, argName, arg.Type,
571571
)
572572
if err != nil {
573573
return resultFieldMap, err
@@ -834,30 +834,30 @@ func NewUnion(config UnionConfig) *Union {
834834
objectType.PrivateDescription = config.Description
835835
objectType.ResolveType = config.ResolveType
836836

837-
err = invariant(
837+
err = invariantf(
838838
len(config.Types) > 0,
839-
fmt.Sprintf(`Must provide Array of types for Union %v.`, config.Name),
839+
`Must provide Array of types for Union %v.`, config.Name,
840840
)
841841
if err != nil {
842842
objectType.err = err
843843
return objectType
844844
}
845845
for _, ttype := range config.Types {
846-
err := invariant(
846+
err := invariantf(
847847
ttype != nil,
848-
fmt.Sprintf(`%v may only contain Object types, it cannot contain: %v.`, objectType, ttype),
848+
`%v may only contain Object types, it cannot contain: %v.`, objectType, ttype,
849849
)
850850
if err != nil {
851851
objectType.err = err
852852
return objectType
853853
}
854854
if objectType.ResolveType == nil {
855-
err = invariant(
855+
err = invariantf(
856856
ttype.IsTypeOf != nil,
857-
fmt.Sprintf(`Union Type %v does not provide a "resolveType" function `+
857+
`Union Type %v does not provide a "resolveType" function `+
858858
`and possible Type %v does not provide a "isTypeOf" `+
859859
`function. There is no way to resolve this possible type `+
860-
`during execution.`, objectType, ttype),
860+
`during execution.`, objectType, ttype,
861861
)
862862
if err != nil {
863863
objectType.err = err
@@ -958,19 +958,19 @@ func NewEnum(config EnumConfig) *Enum {
958958
func (gt *Enum) defineEnumValues(valueMap EnumValueConfigMap) ([]*EnumValueDefinition, error) {
959959
values := []*EnumValueDefinition{}
960960

961-
err := invariant(
961+
err := invariantf(
962962
len(valueMap) > 0,
963-
fmt.Sprintf(`%v values must be an object with value names as keys.`, gt),
963+
`%v values must be an object with value names as keys.`, gt,
964964
)
965965
if err != nil {
966966
return values, err
967967
}
968968

969969
for valueName, valueConfig := range valueMap {
970-
err := invariant(
970+
err := invariantf(
971971
valueConfig != nil,
972-
fmt.Sprintf(`%v.%v must refer to an object with a "value" key `+
973-
`representing an internal value but got: %v.`, gt, valueName, valueConfig),
972+
`%v.%v must refer to an object with a "value" key `+
973+
`representing an internal value but got: %v.`, gt, valueName, valueConfig,
974974
)
975975
if err != nil {
976976
return values, err
@@ -1151,9 +1151,9 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
11511151
}
11521152
resultFieldMap := InputObjectFieldMap{}
11531153

1154-
err := invariant(
1154+
err := invariantf(
11551155
len(fieldMap) > 0,
1156-
fmt.Sprintf(`%v fields must be an object with field names as keys or a function which return such an object.`, gt),
1156+
`%v fields must be an object with field names as keys or a function which return such an object.`, gt,
11571157
)
11581158
if err != nil {
11591159
gt.err = err
@@ -1168,9 +1168,9 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
11681168
if err != nil {
11691169
continue
11701170
}
1171-
err = invariant(
1171+
err = invariantf(
11721172
fieldConfig.Type != nil,
1173-
fmt.Sprintf(`%v.%v field type must be Input Type but got: %v.`, gt, fieldName, fieldConfig.Type),
1173+
`%v.%v field type must be Input Type but got: %v.`, gt, fieldName, fieldConfig.Type,
11741174
)
11751175
if err != nil {
11761176
gt.err = err
@@ -1231,7 +1231,7 @@ type List struct {
12311231
func NewList(ofType Type) *List {
12321232
gl := &List{}
12331233

1234-
err := invariant(ofType != nil, fmt.Sprintf(`Can only create List of a Type but got: %v.`, ofType))
1234+
err := invariantf(ofType != nil, `Can only create List of a Type but got: %v.`, ofType)
12351235
if err != nil {
12361236
gl.err = err
12371237
return gl
@@ -1284,7 +1284,7 @@ func NewNonNull(ofType Type) *NonNull {
12841284
gl := &NonNull{}
12851285

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

13131313
func assertValidName(name string) error {
1314-
return invariant(
1314+
return invariantf(
13151315
NameRegExp.MatchString(name),
1316-
fmt.Sprintf(`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "%v" does not.`, name),
1317-
)
1316+
`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "%v" does not.`, name)
1317+
13181318
}

executor.go

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

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

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)