fix🐛: report a table the generator cannot read

Preview, GenCode, GenApiToFile and GenMenuAndApi read a table's
configuration with tab, _ := table.Get(...), so an id that matched nothing,
or a failed query, carried on with an empty table. GenCode and Preview then
refused it as having no primary key, and GenMenuAndApi went on to seed menus
and APIs for it.

Each now answers with the lookup's error.
This commit is contained in:
zhangwenjian
2026-09-26 14:17:22 +08:00
parent 83064f0b37
commit d84e30afb8
2 changed files with 48 additions and 4 deletions
+24 -4
View File
@@ -124,7 +124,12 @@ func (e Gen) Preview(c *gin.Context) {
return
}
tab, _ := table.Get(db, false)
tab, err := table.Get(db, false)
if err != nil {
log.Errorf("get table error, %s", err.Error())
e.Error(500, err, fmt.Sprintf("读取表配置失败!错误详情:%s", err.Error()))
return
}
// MLTBName (table_name with underscores turned to dashes) is a gorm:"-"
// field - table.Get never fills it in, so every template that reads it
// (the .vue/.ts import paths, e.g. "@/api/{PackageName}/{MLTBName}")
@@ -179,7 +184,12 @@ func (e Gen) GenCode(c *gin.Context) {
}
table.TableId = id
tab, _ := table.Get(db, false)
tab, err := table.Get(db, false)
if err != nil {
log.Errorf("get table error, %s", err.Error())
e.Error(500, err, fmt.Sprintf("读取表配置失败!错误详情:%s", err.Error()))
return
}
if !e.NOActionsGen(c, tab) {
return
@@ -207,7 +217,12 @@ func (e Gen) GenApiToFile(c *gin.Context) {
}
table.TableId = id
tab, _ := table.Get(db, false)
tab, err := table.Get(db, false)
if err != nil {
log.Errorf("get table error, %s", err.Error())
e.Error(500, err, fmt.Sprintf("读取表配置失败!错误详情:%s", err.Error()))
return
}
if !e.genApiToFile(c, tab) {
return
}
@@ -387,7 +402,12 @@ func (e Gen) GenMenuAndApi(c *gin.Context) {
}
table.TableId = id
tab, _ := table.Get(e.Orm, true)
tab, err := table.Get(e.Orm, true)
if err != nil {
e.Logger.Errorf("get table error, %s", err.Error())
e.Error(500, err, fmt.Sprintf("读取表配置失败!错误详情:%s", err.Error()))
return
}
tab.MLTBName = strings.Replace(tab.TBName, "_", "-", -1)
Mmenu := dto.SysMenuInsertReq{}
+24
View File
@@ -191,3 +191,27 @@ func TestGenCodeAnswersOnceWhenGenerationFails(t *testing.T) {
t.Errorf("responses %+v; want exactly one, a 500", bodies)
}
}
// Every handler that reads a table's configuration by id used to discard the
// lookup's error and carry on with an empty table: GenCode and Preview then
// refused it as having no primary key, and GenMenuAndApi went on to seed
// menus and APIs for it.
func TestGeneratorHandlersReportAMissingTable(t *testing.T) {
genWorkspace(t)
db := genDB(t)
missing := gin.Params{{Key: "tableId", Value: "987654"}}
for name, h := range map[string]func(*gin.Context){
"GenCode": Gen{}.GenCode,
"Preview": Gen{}.Preview,
"GenApiToFile": Gen{}.GenApiToFile,
"GenMenuAndApi": Gen{}.GenMenuAndApi,
} {
t.Run(name, func(t *testing.T) {
bodies := runGen(t, db, h, missing)
if len(bodies) != 1 || bodies[0].Code != 500 || !strings.Contains(bodies[0].Msg, "读取表配置失败") {
t.Errorf("responses %+v; want one 500 saying the table could not be read", bodies)
}
})
}
}