apache/answer · error
config not found by id: %d
Error message
config not found by id: %d
What it means
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.
Source
Thrown at internal/repo/config/config_repo.go:65
func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Config, err error) {
cacheKey := fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, id)
cacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey)
if err == nil && exist && len(cacheData) > 0 {
c = &entity.Config{}
c.BuildByJSON([]byte(cacheData))
if c.ID > 0 {
return c, nil
}
}
c = &entity.Config{}
exist, err = cr.data.DB.Context(ctx).ID(id).Get(c)
if err != nil {
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if !exist {
return nil, fmt.Errorf("config not found by id: %d", id)
}
// update cache
if err := cr.data.Cache.SetString(ctx, cacheKey, c.JsonString(), constant.ConfigCacheTime); err != nil {
log.Error(err)
}
return c, nil
}
func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error) {
cacheKey := constant.ConfigKEY2ContentCacheKeyPrefix + key
cacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey)
if err == nil && exist && len(cacheData) > 0 {
c = &entity.Config{}
c.BuildByJSON([]byte(cacheData))
if c.ID > 0 {
return c, nil
}View on GitHub (pinned to 3b9f137061)
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.
Example fix
// before
if !exist {
return nil, fmt.Errorf("config not found by id: %d", id)
}
// after
if !exist {
return nil, fmt.Errorf("config not found by id: %d", id) // caller: map to errors.NotFound
} Defensive patterns
Strategy: validation
Validate before calling
var count int64
db.Model(&entity.Config{}).Where("id = ?", id).Count(&count)
if count == 0 {
return fmt.Errorf("config id %d not present; check the config table", id)
} Type guard
func hasConfig(configs []entity.Config, id int64) bool {
for _, c := range configs {
if c.ID == id {
return true
}
}
return false
} Try / catch
cfg, err := configRepo.GetConfigByID(ctx, id)
if err != nil {
if strings.Contains(err.Error(), "config not found by id") {
return http.StatusNotFound, "config does not exist"
}
return http.StatusInternalServerError, err.Error()
} Prevention
- 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.
When it happens
Trigger: Calling GetConfigByID with a config ID that does not exist in the config table (wrong ID, config deleted, ID from another environment).
Common situations: 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.
Understand the failure class
Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.
Related errors
- config not found by key: %s
- get config failed: %w
- connect database failed: %w
- get config failed: %w
- %s activity not exist
AI-assisted analysis of apache/answer@3b9f137061 (2026-09-05).
Data as JSON: /api/errors/54bcce4ae5fd529b.
Report an issue: GitHub.