iflytek/astron-agent · error

insert app data, data must not been nil

Error message

insert app data, data must not been nil

What it means

AppDao.Insert refuses to run the INSERT statement when the *models.App argument is nil. The DAO has no way to build column values from a nil row, so it fails fast with this error instead of panicking on a nil pointer dereference inside Exec.

Solutions

  1. Check that the *models.App is non-nil before calling SaveApp/Insert and return a validation error to the caller instead.
  2. Fix the upstream construction path so a valid App is always produced or the error is propagated before reaching the DAO.
  3. In the caller (SaveApp), add an early nil check and skip both the app Insert and the auth Insert in the same transaction.
  4. Log the request that produced the nil model to find the deserialization/construction bug.

Example fix

// before
var app *models.App
json.Unmarshal(body, &app) // app stays nil on failure
id, err := appDao.Insert(app, tx)
// after
if app == nil {
    return 0, fmt.Errorf("app payload is required")
}
id, err := appDao.Insert(app, tx)
Defensive patterns

Strategy: validation

Validate before calling

func validApp(a *models.App) bool { return a != nil && a.AppId != "" }

Type guard

if app, ok := data.(*models.App); !ok || app == nil {
    return fmt.Errorf("app payload missing")
}

Try / catch

if err != nil {
    if strings.Contains(err.Error(), "data must not been nil") {
        return fmt.Errorf("invalid request: app payload required: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling Insert(nil, tx) directly, or indirectly via SaveApp when the constructed/decoded App model is nil (e.g. a JSON unmarshal into *models.App produced nil and was passed through).

Common situations: A request body failed to deserialize into *models.App and the handler passed the nil pointer down to SaveApp; a factory function returned nil on an error path that was ignored; code initialized var app *models.App but never assigned it.

Related errors


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

Appendix: source

Thrown at core/tenant/internal/dao/app_dao.go:48

                   (%s)
                 VALUES (?,?,?,?,?,?,?,?,?,?,?)`, sqlField)
	updateSql := `UPDATE tb_app SET %s `
	selectSql := fmt.Sprintf(`SELECT %s FROM tb_app `, sqlField)
	countSql := `SELECT count(1) from tb_app `

	return &AppDao{
			db:        db,
			insertSql: insertSql,
			updateSql: updateSql,
			selectSql: selectSql,
			countSql:  countSql,
		},
		nil
}

func (dao *AppDao) Insert(data *models.App, tx *sql.Tx) (int64, error) {
	if data == nil {
		return 0, fmt.Errorf("insert app data, data must not been nil")
	}
	log.Printf("insert app sql is %s", dao.insertSql)
	if tx == nil {
		result, err := dao.db.GetMysql().Exec(dao.insertSql,
			data.AppId, data.AppName, data.DevId, data.ChannelId, data.Source, data.IsDisable, data.Desc, data.IsDelete, data.CreateTime, data.UpdateTime, data.Extend)
		if err != nil {
			log.Printf("insert app error: %v", err)
			return 0, err
		}
		return result.LastInsertId()
	}
	result, err := tx.Exec(dao.insertSql,
		data.AppId, data.AppName, data.DevId, data.ChannelId, data.Source, data.IsDisable, data.Desc,
		data.IsDelete, data.CreateTime, data.UpdateTime, data.Extend)
	if err != nil {
		log.Printf("insert app error: %v", err)
		return 0, err
	}

View on GitHub (pinned to 5e758547a8)