Skip to content

Commit

Permalink
Don't format the error message unless it is really needed
Browse files Browse the repository at this point in the history
  • Loading branch information
fd committed Jan 5, 2018
1 parent 9b5a661 commit b16d2fd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 53 deletions.
78 changes: 39 additions & 39 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,20 +269,20 @@ func NewScalar(config ScalarConfig) *Scalar {
st.PrivateName = config.Name
st.PrivateDescription = config.Description

err = invariant(
err = invariantf(
config.Serialize != nil,
fmt.Sprintf(`%v must provide "serialize" function. If this custom Scalar is `+
`%v must provide "serialize" function. If this custom Scalar is `+
`also used as an input type, ensure "parseValue" and "parseLiteral" `+
`functions are also provided.`, st),
`functions are also provided.`, st,
)
if err != nil {
st.err = err
return st
}
if config.ParseValue != nil || config.ParseLiteral != nil {
err = invariant(
err = invariantf(
config.ParseValue != nil && config.ParseLiteral != nil,
fmt.Sprintf(`%v must provide both "parseValue" and "parseLiteral" functions.`, st),
`%v must provide both "parseValue" and "parseLiteral" functions.`, st,
)
if err != nil {
st.err = err
Expand Down Expand Up @@ -482,20 +482,20 @@ func defineInterfaces(ttype *Object, interfaces []*Interface) ([]*Interface, err
return ifaces, nil
}
for _, iface := range interfaces {
err := invariant(
err := invariantf(
iface != nil,
fmt.Sprintf(`%v may only implement Interface types, it cannot implement: %v.`, ttype, iface),
`%v may only implement Interface types, it cannot implement: %v.`, ttype, iface,
)
if err != nil {
return ifaces, err
}
if iface.ResolveType != nil {
err = invariant(
err = invariantf(
iface.ResolveType != nil,
fmt.Sprintf(`Interface Type %v does not provide a "resolveType" function `+
`Interface Type %v does not provide a "resolveType" function `+
`and implementing Type %v does not provide a "isTypeOf" `+
`function. There is no way to resolve this implementing type `+
`during execution.`, iface, ttype),
`during execution.`, iface, ttype,
)
if err != nil {
return ifaces, err
Expand All @@ -518,9 +518,9 @@ func defineFieldMap(ttype Named, fields interface{}) (FieldDefinitionMap, error)

resultFieldMap := FieldDefinitionMap{}

err := invariant(
err := invariantf(
len(fieldMap) > 0,
fmt.Sprintf(`%v fields must be an object with field names as keys or a function which return such an object.`, ttype),
`%v fields must be an object with field names as keys or a function which return such an object.`, ttype,
)
if err != nil {
return resultFieldMap, err
Expand All @@ -530,9 +530,9 @@ func defineFieldMap(ttype Named, fields interface{}) (FieldDefinitionMap, error)
if field == nil {
continue
}
err = invariant(
err = invariantf(
field.Type != nil,
fmt.Sprintf(`%v.%v field type must be Output Type but got: %v.`, ttype, fieldName, field.Type),
`%v.%v field type must be Output Type but got: %v.`, ttype, fieldName, field.Type,
)
if err != nil {
return resultFieldMap, err
Expand All @@ -558,16 +558,16 @@ func defineFieldMap(ttype Named, fields interface{}) (FieldDefinitionMap, error)
if err != nil {
return resultFieldMap, err
}
err = invariant(
err = invariantf(
arg != nil,
fmt.Sprintf(`%v.%v args must be an object with argument names as keys.`, ttype, fieldName),
`%v.%v args must be an object with argument names as keys.`, ttype, fieldName,
)
if err != nil {
return resultFieldMap, err
}
err = invariant(
err = invariantf(
arg.Type != nil,
fmt.Sprintf(`%v.%v(%v:) argument type must be Input Type but got: %v.`, ttype, fieldName, argName, arg.Type),
`%v.%v(%v:) argument type must be Input Type but got: %v.`, ttype, fieldName, argName, arg.Type,
)
if err != nil {
return resultFieldMap, err
Expand Down Expand Up @@ -834,30 +834,30 @@ func NewUnion(config UnionConfig) *Union {
objectType.PrivateDescription = config.Description
objectType.ResolveType = config.ResolveType

err = invariant(
err = invariantf(
len(config.Types) > 0,
fmt.Sprintf(`Must provide Array of types for Union %v.`, config.Name),
`Must provide Array of types for Union %v.`, config.Name,
)
if err != nil {
objectType.err = err
return objectType
}
for _, ttype := range config.Types {
err := invariant(
err := invariantf(
ttype != nil,
fmt.Sprintf(`%v may only contain Object types, it cannot contain: %v.`, objectType, ttype),
`%v may only contain Object types, it cannot contain: %v.`, objectType, ttype,
)
if err != nil {
objectType.err = err
return objectType
}
if objectType.ResolveType == nil {
err = invariant(
err = invariantf(
ttype.IsTypeOf != nil,
fmt.Sprintf(`Union Type %v does not provide a "resolveType" function `+
`Union Type %v does not provide a "resolveType" function `+
`and possible Type %v does not provide a "isTypeOf" `+
`function. There is no way to resolve this possible type `+
`during execution.`, objectType, ttype),
`during execution.`, objectType, ttype,
)
if err != nil {
objectType.err = err
Expand Down Expand Up @@ -958,19 +958,19 @@ func NewEnum(config EnumConfig) *Enum {
func (gt *Enum) defineEnumValues(valueMap EnumValueConfigMap) ([]*EnumValueDefinition, error) {
values := []*EnumValueDefinition{}

err := invariant(
err := invariantf(
len(valueMap) > 0,
fmt.Sprintf(`%v values must be an object with value names as keys.`, gt),
`%v values must be an object with value names as keys.`, gt,
)
if err != nil {
return values, err
}

for valueName, valueConfig := range valueMap {
err := invariant(
err := invariantf(
valueConfig != nil,
fmt.Sprintf(`%v.%v must refer to an object with a "value" key `+
`representing an internal value but got: %v.`, gt, valueName, valueConfig),
`%v.%v must refer to an object with a "value" key `+
`representing an internal value but got: %v.`, gt, valueName, valueConfig,
)
if err != nil {
return values, err
Expand Down Expand Up @@ -1151,9 +1151,9 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
}
resultFieldMap := InputObjectFieldMap{}

err := invariant(
err := invariantf(
len(fieldMap) > 0,
fmt.Sprintf(`%v fields must be an object with field names as keys or a function which return such an object.`, gt),
`%v fields must be an object with field names as keys or a function which return such an object.`, gt,
)
if err != nil {
gt.err = err
Expand All @@ -1168,9 +1168,9 @@ func (gt *InputObject) defineFieldMap() InputObjectFieldMap {
if err != nil {
continue
}
err = invariant(
err = invariantf(
fieldConfig.Type != nil,
fmt.Sprintf(`%v.%v field type must be Input Type but got: %v.`, gt, fieldName, fieldConfig.Type),
`%v.%v field type must be Input Type but got: %v.`, gt, fieldName, fieldConfig.Type,
)
if err != nil {
gt.err = err
Expand Down Expand Up @@ -1231,7 +1231,7 @@ type List struct {
func NewList(ofType Type) *List {
gl := &List{}

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

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

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

}
14 changes: 7 additions & 7 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,9 +667,9 @@ func completeValue(eCtx *ExecutionContext, returnType Type, fieldASTs []*ast.Fie
}

// Not reachable. All possible output types have been considered.
err := invariant(false,
fmt.Sprintf(`Cannot complete value of unexpected type "%v."`, returnType),
)
err := invariantf(false,
`Cannot complete value of unexpected type "%v."`, returnType)

if err != nil {
panic(gqlerrors.FormatError(err))
}
Expand Down Expand Up @@ -780,11 +780,11 @@ func completeListValue(eCtx *ExecutionContext, returnType *List, fieldASTs []*as
if info.ParentType != nil {
parentTypeName = info.ParentType.Name()
}
err := invariant(
err := invariantf(
resultVal.IsValid() && resultVal.Type().Kind() == reflect.Slice,
fmt.Sprintf("User Error: expected iterable, but did not find one "+
"for field %v.%v.", parentTypeName, info.FieldName),
)
"User Error: expected iterable, but did not find one "+
"for field %v.%v.", parentTypeName, info.FieldName)

if err != nil {
panic(gqlerrors.FormatError(err))
}
Expand Down
14 changes: 7 additions & 7 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,10 @@ func typeMapReducer(schema *Schema, typeMap TypeMap, objectType Type) (TypeMap,
}

if mappedObjectType, ok := typeMap[objectType.Name()]; ok {
err := invariant(
err = invariantf(
mappedObjectType == objectType,
fmt.Sprintf(`Schema must contain unique named types but contains multiple types named "%v".`, objectType.Name()),
)
`Schema must contain unique named types but contains multiple types named "%v".`, objectType.Name())

if err != nil {
return typeMap, err
}
Expand Down Expand Up @@ -408,11 +408,11 @@ func assertObjectImplementsInterface(schema *Schema, object *Object, iface *Inte
ifaceField := ifaceFieldMap[fieldName]

// Assert interface field exists on object.
err := invariant(
err := invariantf(
objectField != nil,
fmt.Sprintf(`"%v" expects field "%v" but "%v" does not `+
`provide it.`, iface, fieldName, object),
)
`"%v" expects field "%v" but "%v" does not `+
`provide it.`, iface, fieldName, object)

if err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions values.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,10 @@ func invariant(condition bool, message string) error {
}
return nil
}

func invariantf(condition bool, format string, a ...interface{}) error {
if !condition {
return gqlerrors.NewFormattedError(fmt.Sprintf(format, a...))
}
return nil
}

0 comments on commit b16d2fd

Please sign in to comment.