From d84e30afb861c005457d0f2ba1ca930f339bf838 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 26 Sep 2026 14:17:22 +0800 Subject: [PATCH] =?UTF-8?q?fix=F0=9F=90=9B:=20report=20a=20table=20the=20g?= =?UTF-8?q?enerator=20cannot=20read?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- app/other/apis/tools/gen.go | 28 +++++++++++++++++++++---- app/other/apis/tools/gen_errors_test.go | 24 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/app/other/apis/tools/gen.go b/app/other/apis/tools/gen.go index ddab2feb..4bd9094d 100644 --- a/app/other/apis/tools/gen.go +++ b/app/other/apis/tools/gen.go @@ -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{} diff --git a/app/other/apis/tools/gen_errors_test.go b/app/other/apis/tools/gen_errors_test.go index 8fe42b40..4df190f8 100644 --- a/app/other/apis/tools/gen_errors_test.go +++ b/app/other/apis/tools/gen_errors_test.go @@ -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) + } + }) + } +}