Billionmail/BillionMail · error
failed to get contacts trend: %w
Error message
failed to get contacts trend: %w
What it means
GetContactsTrend queries aggregate contact trend statistics grouped by date. When the underlying PostgreSQL query fails (syntax, connection, bad column, driver error), the db.Scan error is wrapped with this message so the caller knows the trend report could not be produced.
Source
Thrown at core/internal/service/contact/contact.go:307
var trends []*ContactTrend
db := g.DB().Model("bm_contacts").
Fields(
"to_char(to_timestamp(create_time), 'YYYY-MM') as month",
"SUM(CASE WHEN active = 1 THEN 1 ELSE 0 END) as subscribe_count",
"SUM(CASE WHEN active = 0 THEN 1 ELSE 0 END) as unsubscribe_count",
).
Where("create_time BETWEEN ? AND ?", startTime.Unix(), endTime.Unix()).
Group("month").
Order("month ASC")
if groupId != 0 {
db = db.Where("group_id = ?", groupId)
}
if err := db.Scan(&trends); err != nil {
return nil, fmt.Errorf("failed to get contacts trend: %w", err)
}
if trends == nil {
return make([]*ContactTrend, 0), nil
}
return trends, nil
}
func UpdateContactsGroups(ctx context.Context, emails []string, status int, newGroupIds []int) (int, error) {
var existingContacts []struct {
Email string `json:"email"`
Attribs map[string]string `json:"attribs"`
Status int `json:"status"`
}
err := g.DB().Model("bm_contacts").View on GitHub (pinned to fc36c76c05)
Solutions
- Inspect the wrapped cause (%w) in logs to see the actual SQL error
- Run database migrations to ensure the contacts/trend tables and columns exist
- Verify DB connectivity and credentials (see InitDatabase errors)
- Add an index on the trend time column / narrow the queried date range to avoid timeouts
Defensive patterns
Strategy: try-catch
Validate before calling
if err := db.Ping(); err != nil { return fmt.Errorf("db unreachable before trend query: %w", err) } Try / catch
trends, err := contact.GetContactsTrend(from, to, groupId)
if err != nil {
var cause error
if errors.As(err, &cause) || true { log.Errorf("trend query: %v", err) }
return emptyTrendFallback()
} Prevention
- Run migrations before serving stats endpoints
- Narrow date ranges and index the time column
- Log the wrapped root cause for diagnosis
- Monitor DB connection health
When it happens
Trigger: Calling GetContactsTrend with a time range / group filter when the DB is unreachable, the contacts table or required columns (created_at, group_id) are missing or renamed, or the query times out on very large datasets.
Common situations: Fresh install where migrations haven't created the tables; schema drift after an upgrade; DB credentials/network changed; filtering on a huge table without an index causing statement timeout.
Understand the failure class
Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.
Related errors
- failed to get all emails: %w
- failed to get all mailboxes: %w
- Failed to get the total number of exception recipients: %w
- Failed to get the exception recipient list: %w
- failed to load API templates: %v
AI-assisted analysis of Billionmail/BillionMail@fc36c76c05 (2026-09-05).
Data as JSON: /api/errors/64279e1abc00844d.
Report an issue: GitHub.