Skip to content

Commit

Permalink
adjust err handling and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Jun 11, 2024
1 parent 9dea5b6 commit aba79e1
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions backend/database/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"crypto/sha256"
"encoding/hex"
"errors"
"log"
"log/slog"
"time"

"github.com/GenerateNU/sac/backend/config"
Expand Down Expand Up @@ -85,23 +85,22 @@ func (g *GormCache) queryCallback(db *gorm.DB) {
// get value from cache
hit, err = g.loadCache(db, key)
if err != nil {
log.Printf("load cache failed: %v, hit: %v", err, hit)
slog.Error("load cache failed", "error", err, "hit", hit)
return
}

// hit cache
if hit {
return
}

}

if !hit {
g.queryDB(db)

if enableCache {
if err = g.setCache(db, key); err != nil {
log.Printf("set cache failed: %v", err)
slog.Error("set cache failed", "error", err)
}
}
}
Expand Down Expand Up @@ -159,11 +158,15 @@ func (g *GormCache) setCache(db *gorm.DB, key string) error {
func (g *GormCache) queryDB(db *gorm.DB) {
rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
if err != nil {
db.AddError(err)
if err := db.AddError(err); err != nil {
slog.Error("error encountered while adding error", "error", err)
}
return
}
defer func() {
db.AddError(rows.Close())
if err := db.AddError(rows.Close()); err != nil {
slog.Error("error encountered while closing rows", "error", err)
}
}()
gorm.Scan(rows, db, 0)
}
Expand Down

0 comments on commit aba79e1

Please sign in to comment.