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) + } + }) + } +}