Skip to content

Commit

Permalink
Merge pull request #267 from mrhenry/lazy-invariant
Browse files Browse the repository at this point in the history
Don't format the error message unless it is really needed
  • Loading branch information
chris-ramon authored Jan 7, 2018
2 parents 77b1ae5 + d951837 commit 363d9c3
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 @@ -499,20 +499,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 @@ -527,9 +527,9 @@ func defineInterfaces(ttype *Object, interfaces []*Interface) ([]*Interface, err
func defineFieldMap(ttype Named, fieldMap Fields) (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 @@ -539,9 +539,9 @@ func defineFieldMap(ttype Named, fieldMap Fields) (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 @@ -567,16 +567,16 @@ func defineFieldMap(ttype Named, fieldMap Fields) (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 @@ -856,30 +856,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 @@ -980,19 +980,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 @@ -1173,9 +1173,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 @@ -1190,9 +1190,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 @@ -1253,7 +1253,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 @@ -1306,7 +1306,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 @@ -1333,8 +1333,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 @@ -668,9 +668,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 @@ -781,11 +781,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 363d9c3

Please sign in to comment.