friendly invalid field error message

This commit is contained in:
Jinzhu 2020-08-25 18:18:16 +08:00
parent 3dfa8a66f1
commit 0f3201e73b
1 changed files with 9 additions and 9 deletions

View File

@ -336,7 +336,7 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, gl gue
primarySchema, foreignSchema = schema, relation.FieldSchema primarySchema, foreignSchema = schema, relation.FieldSchema
) )
reguessOrErr := func(err string, args ...interface{}) { reguessOrErr := func() {
switch gl { switch gl {
case guessHas: case guessHas:
schema.guessRelation(relation, field, guessEmbeddedHas) schema.guessRelation(relation, field, guessEmbeddedHas)
@ -345,7 +345,7 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, gl gue
case guessBelongs: case guessBelongs:
schema.guessRelation(relation, field, guessEmbeddedBelongs) schema.guessRelation(relation, field, guessEmbeddedBelongs)
default: default:
schema.err = fmt.Errorf(err, args...) schema.err = fmt.Errorf("invalid field found for struct %v's field %v, need to define a foreign key for relations or it need to implement the Valuer/Scanner interface", schema, field.Name)
} }
} }
@ -354,7 +354,7 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, gl gue
if field.OwnerSchema != nil { if field.OwnerSchema != nil {
primarySchema, foreignSchema = field.OwnerSchema, relation.FieldSchema primarySchema, foreignSchema = field.OwnerSchema, relation.FieldSchema
} else { } else {
reguessOrErr("failed to guess %v's relations with %v's field %v, guess level: %v", relation.FieldSchema, schema, field.Name, gl) reguessOrErr()
return return
} }
case guessBelongs: case guessBelongs:
@ -363,7 +363,7 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, gl gue
if field.OwnerSchema != nil { if field.OwnerSchema != nil {
primarySchema, foreignSchema = relation.FieldSchema, field.OwnerSchema primarySchema, foreignSchema = relation.FieldSchema, field.OwnerSchema
} else { } else {
reguessOrErr("failed to guess %v's relations with %v's field %v, guess level: %v", relation.FieldSchema, schema, field.Name, gl) reguessOrErr()
return return
} }
} }
@ -373,7 +373,7 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, gl gue
if f := foreignSchema.LookUpField(foreignKey); f != nil { if f := foreignSchema.LookUpField(foreignKey); f != nil {
foreignFields = append(foreignFields, f) foreignFields = append(foreignFields, f)
} else { } else {
reguessOrErr("unsupported relations %v for %v on field %v with foreign keys %v", relation.FieldSchema, schema, field.Name, relation.foreignKeys) reguessOrErr()
return return
} }
} }
@ -392,7 +392,7 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, gl gue
} }
if len(foreignFields) == 0 { if len(foreignFields) == 0 {
reguessOrErr("failed to guess %v's relations with %v's field %v, guess level: %v", relation.FieldSchema, schema, field.Name, gl) reguessOrErr()
return return
} else if len(relation.primaryKeys) > 0 { } else if len(relation.primaryKeys) > 0 {
for idx, primaryKey := range relation.primaryKeys { for idx, primaryKey := range relation.primaryKeys {
@ -400,11 +400,11 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, gl gue
if len(primaryFields) < idx+1 { if len(primaryFields) < idx+1 {
primaryFields = append(primaryFields, f) primaryFields = append(primaryFields, f)
} else if f != primaryFields[idx] { } else if f != primaryFields[idx] {
reguessOrErr("unsupported relations %v for %v on field %v with primary keys %v", relation.FieldSchema, schema, field.Name, relation.primaryKeys) reguessOrErr()
return return
} }
} else { } else {
reguessOrErr("unsupported relations %v for %v on field %v with primary keys %v", relation.FieldSchema, schema, field.Name, relation.primaryKeys) reguessOrErr()
return return
} }
} }
@ -414,7 +414,7 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, gl gue
} else if len(primarySchema.PrimaryFields) == len(foreignFields) { } else if len(primarySchema.PrimaryFields) == len(foreignFields) {
primaryFields = append(primaryFields, primarySchema.PrimaryFields...) primaryFields = append(primaryFields, primarySchema.PrimaryFields...)
} else { } else {
reguessOrErr("unsupported relations %v for %v on field %v", relation.FieldSchema, schema, field.Name) reguessOrErr()
return return
} }
} }