{"record":{"id":"2b50526ace452678","repo":"iflytek/astron-agent","slug":"database-config-is-nil-or-dbtype-is-empty","errorCode":null,"errorMessage":"database config is nil or dbType is empty","messagePattern":"database config is nil or dbType is empty","errorType":"console","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"core/tenant/tools/database/database.go","lineNumber":27,"sourceCode":"\n\t\"tenant/config\"\n\n\tmysql \"github.com/go-sql-driver/mysql\"\n)\n\ntype DBType string\n\nconst (\n\tMYSQL DBType = \"mysql\"\n)\n\ntype Database struct {\n\tmysql *sql.DB\n}\n\nfunc NewDatabase(conf *config.Config) (*Database, error) {\n\tif conf == nil || len(conf.DataBase.DBType) == 0 {\n\t\treturn nil, errors.New(\"database config is nil or dbType is empty\")\n\t}\n\tdbType := DBType(conf.DataBase.DBType)\n\tdb := &Database{}\n\tswitch dbType {\n\tcase MYSQL:\n\t\terr := db.buildMysql(conf)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\treturn db, nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"unsupported dbType: %s\", conf.DataBase.DBType)\n\t}\n}\n\nfunc (db *Database) buildMysql(conf *config.Config) error {\n\tdsn, parsedDsn, err := parseMysqlConfig(conf)\n\tif err != nil {","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/tenant/tools/database/database.go#L9-L45","documentation":"NewDatabase validates the tenant service config before opening any DB connection. If the config pointer is nil or DataBase.DBType is an empty string, it cannot know which driver to build (mysql, etc.) and returns this error instead of panicking on a nil dereference.","triggerScenarios":"initHandler or another caller passes a nil *config.Config, or the loaded config file lacks database.dbType (missing/empty key).","commonSituations":"Config file not loaded (empty config struct), YAML/JSON key misspelled or nested at wrong level, service started without the config env/flag, or a unit test passing nil config.","solutions":["Ensure the config file contains a non-empty database.dbType (e.g. mysql) and is actually loaded before NewDatabase","Check the env/flag that supplies the config path is set at startup","Fix the YAML/JSON structure so dbType sits under the expected DataBase key","Guard tests to construct a full config instead of passing nil"],"exampleFix":"// before\ndatabase:\n  dbType: \"\"\n// after\ndatabase:\n  dbType: \"mysql\"\n  userName: ...\n  password: ...\n  url: ...","handlingStrategy":"validation","validationCode":"if conf == nil || strings.TrimSpace(conf.DataBase.DBType) == \"\" { return errors.New(\"database.dbType must be set before NewDatabase\") }","typeGuard":"func configValid(conf *config.Config) bool { return conf != nil && conf.DataBase != nil && conf.DataBase.DBType != \"\" }","tryCatchPattern":"db, err := NewDatabase(conf); if err != nil { log.Fatalf(\"init database: %v\", err) }","preventionTips":["Fail fast at startup if required config keys are absent","Validate config files in CI with a schema check","Log the config path actually loaded to catch silent fallbacks"],"tags":["go","config","database","startup"],"backgroundTag":"missing-required-config-field","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}