diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-14 10:41:58 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-14 10:41:58 +0300 |
| commit | aad908c3b71f2a99898e0b0820625ddd40a986f3 (patch) | |
| tree | cb229be5fb42d6baa89ee1991640cd07086e879c /internal | |
| parent | d49376f71dd8defebf80f901ecb60ab99f0f4906 (diff) | |
goprecords: wrap LoadRecords errors in LoadAggregates (ask o3)
Use fmt.Errorf("load records: %%w", err) so import/query failures carry
context. Add TestLoadAggregatesWrapsLoadRecordsError.
Made-with: Cursor
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/goprecords/db.go | 3 | ||||
| -rw-r--r-- | internal/goprecords/db_test.go | 26 |
2 files changed, 28 insertions, 1 deletions
diff --git a/internal/goprecords/db.go b/internal/goprecords/db.go index a442dd0..035c13d 100644 --- a/internal/goprecords/db.go +++ b/internal/goprecords/db.go @@ -3,6 +3,7 @@ package goprecords import ( "context" "database/sql" + "fmt" "codeberg.org/snonux/goprecords/internal/storage" _ "modernc.org/sqlite" @@ -33,7 +34,7 @@ func ImportFromDir(ctx context.Context, db *sql.DB, statsDir string) error { func LoadAggregates(ctx context.Context, db *sql.DB) (*Aggregates, error) { records, err := storage.LoadRecords(ctx, db) if err != nil { - return nil, err + return nil, fmt.Errorf("load records: %w", err) } out := &Aggregates{ Host: make(map[string]*HostAggregate), diff --git a/internal/goprecords/db_test.go b/internal/goprecords/db_test.go index 943ee4d..acfe2ca 100644 --- a/internal/goprecords/db_test.go +++ b/internal/goprecords/db_test.go @@ -4,6 +4,7 @@ import ( "context" "os" "path/filepath" + "strings" "testing" ) @@ -236,3 +237,28 @@ func TestLoadAggregatesEmptyDB(t *testing.T) { t.Errorf("expected 0 hosts, got %d", len(aggs.Host)) } } + +func TestLoadAggregatesWrapsLoadRecordsError(t *testing.T) { + tmpDir := t.TempDir() + dbPath := filepath.Join(tmpDir, "test.db") + + db, err := OpenDB(context.Background(), dbPath) + if err != nil { + t.Fatalf("failed to open DB: %v", err) + } + + ctx := context.Background() + if err := CreateSchema(ctx, db); err != nil { + db.Close() + t.Fatalf("failed to create schema: %v", err) + } + db.Close() + + _, err = LoadAggregates(ctx, db) + if err == nil { + t.Fatal("expected error after db closed") + } + if !strings.Contains(err.Error(), "load records:") { + t.Fatalf("expected load records context in error, got %v", err) + } +} |
