{"record":{"id":"54bcce4ae5fd529b","repo":"apache/answer","slug":"config-not-found-by-id-d","errorCode":null,"errorMessage":"config not found by id: %d","messagePattern":"config not found by id: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/repo/config/config_repo.go","lineNumber":65,"sourceCode":"\nfunc (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Config, err error) {\n\tcacheKey := fmt.Sprintf(\"%s%d\", constant.ConfigID2KEYCacheKeyPrefix, id)\n\tcacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey)\n\tif err == nil && exist && len(cacheData) > 0 {\n\t\tc = &entity.Config{}\n\t\tc.BuildByJSON([]byte(cacheData))\n\t\tif c.ID > 0 {\n\t\t\treturn c, nil\n\t\t}\n\t}\n\n\tc = &entity.Config{}\n\texist, err = cr.data.DB.Context(ctx).ID(id).Get(c)\n\tif err != nil {\n\t\treturn nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()\n\t}\n\tif !exist {\n\t\treturn nil, fmt.Errorf(\"config not found by id: %d\", id)\n\t}\n\n\t// update cache\n\tif err := cr.data.Cache.SetString(ctx, cacheKey, c.JsonString(), constant.ConfigCacheTime); err != nil {\n\t\tlog.Error(err)\n\t}\n\treturn c, nil\n}\n\nfunc (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error) {\n\tcacheKey := constant.ConfigKEY2ContentCacheKeyPrefix + key\n\tcacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey)\n\tif err == nil && exist && len(cacheData) > 0 {\n\t\tc = &entity.Config{}\n\t\tc.BuildByJSON([]byte(cacheData))\n\t\tif c.ID > 0 {\n\t\t\treturn c, nil\n\t\t}","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/apache/answer/blob/3b9f1370612e690a0b7f230f05e688930db4c6d3/internal/repo/config/config_repo.go#L47-L83","documentation":"GetConfigByID loads a config row by primary key; when the ID matches no row it returns this error (DB errors are already handled separately as DatabaseError). The function normally also refreshes the config cache, but only after a row is found.","triggerScenarios":"Calling GetConfigByID with a config ID that does not exist in the config table (wrong ID, config deleted, ID from another environment).","commonSituations":"Hardcoded config IDs that differ between dev/staging/prod; config rows dropped by a migration or re-seed; stale admin UI links to removed configs.","solutions":["List available IDs (SELECT id, config_key FROM config;) and use a valid one.","Restore the missing config row via migration/seed data.","Prefer GetConfigByKey with a stable key instead of a volatile numeric ID.","Callers should map this error to a 404-style response rather than a 500."],"exampleFix":"// before\nif !exist {\n    return nil, fmt.Errorf(\"config not found by id: %d\", id)\n}\n// after\nif !exist {\n    return nil, fmt.Errorf(\"config not found by id: %d\", id) // caller: map to errors.NotFound\n}","handlingStrategy":"validation","validationCode":"var count int64\ndb.Model(&entity.Config{}).Where(\"id = ?\", id).Count(&count)\nif count == 0 {\n    return fmt.Errorf(\"config id %d not present; check the config table\", id)\n}","typeGuard":"func hasConfig(configs []entity.Config, id int64) bool {\n    for _, c := range configs {\n        if c.ID == id {\n            return true\n        }\n    }\n    return false\n}","tryCatchPattern":"cfg, err := configRepo.GetConfigByID(ctx, id)\nif err != nil {\n    if strings.Contains(err.Error(), \"config not found by id\") {\n        return http.StatusNotFound, \"config does not exist\"\n    }\n    return http.StatusInternalServerError, err.Error()\n}","preventionTips":["Prefer stable config keys over numeric IDs in code.","Keep config rows in migrations/seeds so every environment has them.","Clear stale admin links pointing at removed configs.","Remember the cache: a deleted config may still be served until ConfigCacheTime expires."],"tags":["database","config","not-found"],"backgroundTag":"record-not-found","analyzedSha":"3b9f1370612e690a0b7f230f05e688930db4c6d3","analyzedAt":"2026-09-05T18:18:39.533Z","contentChangedAt":"2026-09-05T18:18:39.533Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}