{"record":{"id":"73456c8c64a27f68","repo":"iflytek/astron-agent","slug":"insert-auth-data-data-must-not-been-nil","errorCode":null,"errorMessage":"insert auth data,data must not been nil","messagePattern":"insert auth data,data must not been nil","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/tenant/internal/dao/auth_dao.go","lineNumber":49,"sourceCode":"\tupdateSql := `UPDATE tb_auth  SET  %s `\n\tselectSql := fmt.Sprintf(`SELECT %s FROM tb_auth `, sqlField)\n\tcountSql := `SELECT count(1) from tb_auth `\n\treturn &AuthDao{\n\t\tdb:        db,\n\t\tinsertSql: insertSql,\n\t\tupdateSql: updateSql,\n\t\tselectSql: selectSql,\n\t\tcountSql:  countSql,\n\t}, nil\n}\n\nfunc (dao *AuthDao) BeginTx() (*sql.Tx, error) {\n\treturn dao.db.GetMysql().Begin()\n}\n\nfunc (dao *AuthDao) Insert(data *models.Auth, tx *sql.Tx) (int64, error) {\n\tif data == nil {\n\t\treturn 0, fmt.Errorf(\"insert auth data,data must not been nil\")\n\t}\n\tlog.Printf(\"insert auth sql is %s\", dao.insertSql)\n\tif tx == nil {\n\t\tresult, err := dao.db.GetMysql().Exec(dao.insertSql, //\n\t\t\tdata.AppId, data.ApiKey, data.ApiSecret, data.Source, data.IsDelete,\n\t\t\tdata.CreateTime, data.UpdateTime, data.Extend)\n\t\tif err != nil {\n\t\t\tlog.Printf(\"insert auth error: %v\", err)\n\t\t\treturn 0, err\n\t\t}\n\t\treturn result.RowsAffected()\n\t}\n\tresult, err := tx.Exec(dao.insertSql, //\n\t\tdata.AppId, data.ApiKey, data.ApiSecret, data.Source, data.IsDelete,\n\t\tdata.CreateTime, data.UpdateTime, data.Extend)\n\tif err != nil {\n\t\tlog.Printf(\"insert auth error: %v\", err)\n\t\treturn 0, err","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/tenant/internal/dao/auth_dao.go#L31-L67","documentation":"AuthDao.Insert refuses to execute its INSERT when the *models.Auth argument is nil. A nil Auth has no AppId/ApiKey/ApiSecret values, so the DAO fails fast rather than dereferencing nil fields in Exec.","triggerScenarios":"Calling Insert(nil, tx), or via SaveApp when the Auth record paired with the app was never constructed (nil pointer passed into the transaction flow).","commonSituations":"SaveApp builds the app row but the credentials-generation step silently returned nil auth; JSON binding to *models.Auth failed and the nil pointer flowed through; refactoring moved Auth construction behind a branch that was not taken.","solutions":["Validate that *models.Auth is non-nil (and ApiKey/ApiSecret non-empty) before calling SaveApp/Insert.","Ensure the credentials-generation step always returns a valid Auth or a propagated error so nil never reaches the DAO.","In SaveApp, bail out of the transaction early with a clear error if auth is nil so the app insert is rolled back too.","Log the caller path that produced the nil Auth to locate the construction bug."],"exampleFix":"// before\nid, err := authDao.Insert(auth, tx) // auth may be nil\n// after\nif auth == nil {\n    return 0, fmt.Errorf(\"auth credentials are required\")\n}\nid, err := authDao.Insert(auth, tx)","handlingStrategy":"validation","validationCode":"func validAuth(a *models.Auth) bool { return a != nil && a.AppId != \"\" && a.ApiKey != \"\" && a.ApiSecret != \"\" }","typeGuard":"if auth == nil {\n    return fmt.Errorf(\"auth credentials required\")\n}","tryCatchPattern":"if err != nil {\n    if strings.Contains(err.Error(), \"data must not been nil\") {\n        return fmt.Errorf(\"auth credentials missing for app save: %w\", err)\n    }\n    return err\n}","preventionTips":["Generate AppId/ApiKey/ApiSecret in one constructor that cannot return (nil, nil).","Validate credentials non-empty before SaveApp.","Roll back the app insert if auth construction fails by using the shared transaction."],"tags":["go","database","null-check"],"backgroundTag":"null-argument","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}