iflytek/astron-agent · critical

database config is nil or dbType is empty

Error message

database config is nil or dbType is empty

What it means

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.

Solutions

  1. Ensure the config file contains a non-empty database.dbType (e.g. mysql) and is actually loaded before NewDatabase
  2. Check the env/flag that supplies the config path is set at startup
  3. Fix the YAML/JSON structure so dbType sits under the expected DataBase key
  4. Guard tests to construct a full config instead of passing nil

Example fix

// before
database:
  dbType: ""
// after
database:
  dbType: "mysql"
  userName: ...
  password: ...
  url: ...
Defensive patterns

Strategy: validation

Validate before calling

if conf == nil || strings.TrimSpace(conf.DataBase.DBType) == "" { return errors.New("database.dbType must be set before NewDatabase") }

Type guard

func configValid(conf *config.Config) bool { return conf != nil && conf.DataBase != nil && conf.DataBase.DBType != "" }

Try / catch

db, err := NewDatabase(conf); if err != nil { log.Fatalf("init database: %v", err) }

Prevention

When it happens

Trigger: initHandler or another caller passes a nil *config.Config, or the loaded config file lacks database.dbType (missing/empty key).

Common situations: 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.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of iflytek/astron-agent@5e758547a8 (2026-09-12). Data as JSON: /api/errors/2b50526ace452678. Report an issue: GitHub.

Appendix: source

Thrown at core/tenant/tools/database/database.go:27

	"tenant/config"

	mysql "github.com/go-sql-driver/mysql"
)

type DBType string

const (
	MYSQL DBType = "mysql"
)

type Database struct {
	mysql *sql.DB
}

func NewDatabase(conf *config.Config) (*Database, error) {
	if conf == nil || len(conf.DataBase.DBType) == 0 {
		return nil, errors.New("database config is nil or dbType is empty")
	}
	dbType := DBType(conf.DataBase.DBType)
	db := &Database{}
	switch dbType {
	case MYSQL:
		err := db.buildMysql(conf)
		if err != nil {
			return nil, err
		}
		return db, nil
	default:
		return nil, fmt.Errorf("unsupported dbType: %s", conf.DataBase.DBType)
	}
}

func (db *Database) buildMysql(conf *config.Config) error {
	dsn, parsedDsn, err := parseMysqlConfig(conf)
	if err != nil {

View on GitHub (pinned to 5e758547a8)