Skip to content

Commit ab123ef

Browse files
committed
fix: use errors.Is for sql.ErrNoRows checks across codebase
Replaces direct == comparison with errors.Is to correctly handle wrapped errors from database drivers.
1 parent fb405c5 commit ab123ef

4 files changed

Lines changed: 23 additions & 21 deletions

File tree

admin/handler.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,7 @@ func (h *Handler) FetchOpenAIResponsesModels(c *gin.Context) {
14021402
if req.AccountID > 0 && req.APIKey == "" {
14031403
row, err := h.db.GetAccountByID(c.Request.Context(), req.AccountID)
14041404
if err != nil {
1405-
if err == sql.ErrNoRows {
1405+
if errors.Is(err, sql.ErrNoRows) {
14061406
writeError(c, http.StatusNotFound, "账号不存在")
14071407
return
14081408
}
@@ -1468,7 +1468,7 @@ func (h *Handler) UpdateOpenAIResponsesAccount(c *gin.Context) {
14681468
defer cancel()
14691469
row, err := h.db.GetAccountByID(ctx, id)
14701470
if err != nil {
1471-
if err == sql.ErrNoRows {
1471+
if errors.Is(err, sql.ErrNoRows) {
14721472
writeError(c, http.StatusNotFound, "账号不存在")
14731473
return
14741474
}
@@ -1533,7 +1533,7 @@ func (h *Handler) UpdateOpenAIResponsesAccount(c *gin.Context) {
15331533
}
15341534

15351535
if err := h.db.UpdateOpenAIResponsesAccount(ctx, id, name, credentials, req.ProxyURL); err != nil {
1536-
if err == sql.ErrNoRows {
1536+
if errors.Is(err, sql.ErrNoRows) {
15371537
writeError(c, http.StatusNotFound, "账号不存在")
15381538
return
15391539
}
@@ -2313,7 +2313,7 @@ func (h *Handler) DeleteAccount(c *gin.Context) {
23132313

23142314
// 软删除:保留账号数据与事件记录,但从运行时池和 active 列表中移除。
23152315
if err := h.db.SoftDeleteAccount(ctx, id); err != nil {
2316-
if err == sql.ErrNoRows {
2316+
if errors.Is(err, sql.ErrNoRows) {
23172317
writeError(c, http.StatusNotFound, "账号不存在")
23182318
return
23192319
}
@@ -2377,7 +2377,7 @@ func (h *Handler) ToggleAccountEnabled(c *gin.Context) {
23772377
defer cancel()
23782378

23792379
if err := h.db.SetAccountEnabled(ctx, id, *req.Enabled); err != nil {
2380-
if err == sql.ErrNoRows {
2380+
if errors.Is(err, sql.ErrNoRows) {
23812381
writeError(c, http.StatusNotFound, "账号不存在")
23822382
return
23832383
}
@@ -3307,7 +3307,7 @@ func (h *Handler) UpdateAPIKey(c *gin.Context) {
33073307
defer cancel()
33083308
row, err := h.db.GetAPIKeyByID(ctx, id)
33093309
if err != nil {
3310-
if err == sql.ErrNoRows {
3310+
if errors.Is(err, sql.ErrNoRows) {
33113311
writeError(c, http.StatusNotFound, "API Key 不存在")
33123312
return
33133313
}
@@ -4368,7 +4368,7 @@ func (h *Handler) GetAccountAuthJSON(c *gin.Context) {
43684368

43694369
row, err := h.db.GetAccountByID(ctx, id)
43704370
if err != nil {
4371-
if err == sql.ErrNoRows {
4371+
if errors.Is(err, sql.ErrNoRows) {
43724372
writeError(c, http.StatusNotFound, "账号不存在")
43734373
return
43744374
}
@@ -4768,7 +4768,7 @@ func (h *Handler) DeleteProxy(c *gin.Context) {
47684768
defer cancel()
47694769

47704770
if err := h.db.DeleteProxy(ctx, id); err != nil {
4771-
if err == sql.ErrNoRows {
4771+
if errors.Is(err, sql.ErrNoRows) {
47724772
writeError(c, http.StatusNotFound, "代理不存在")
47734773
return
47744774
}
@@ -4804,7 +4804,7 @@ func (h *Handler) UpdateProxy(c *gin.Context) {
48044804
defer cancel()
48054805

48064806
if err := h.db.UpdateProxy(ctx, id, req.URL, req.Label, req.Enabled); err != nil {
4807-
if err == sql.ErrNoRows {
4807+
if errors.Is(err, sql.ErrNoRows) {
48084808
writeError(c, http.StatusNotFound, "代理不存在")
48094809
return
48104810
}

admin/image_studio.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"database/sql"
77
"encoding/base64"
88
"encoding/json"
9+
"errors"
910
"fmt"
1011
"image"
1112
_ "image/gif"
@@ -137,7 +138,7 @@ func (h *Handler) UpdateImagePromptTemplate(c *gin.Context) {
137138
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
138139
defer cancel()
139140
existing, err := h.db.GetImagePromptTemplate(ctx, id)
140-
if err == sql.ErrNoRows {
141+
if errors.Is(err, sql.ErrNoRows) {
141142
writeError(c, http.StatusNotFound, "模板不存在")
142143
return
143144
}
@@ -343,7 +344,7 @@ func (h *Handler) GetImageGenerationJob(c *gin.Context) {
343344
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
344345
defer cancel()
345346
job, err := h.db.GetImageGenerationJob(ctx, id)
346-
if err == sql.ErrNoRows {
347+
if errors.Is(err, sql.ErrNoRows) {
347348
writeError(c, http.StatusNotFound, "任务不存在")
348349
return
349350
}
@@ -415,7 +416,7 @@ func (h *Handler) GetImageAssetFile(c *gin.Context) {
415416
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
416417
defer cancel()
417418
asset, err := h.db.GetImageAsset(ctx, id)
418-
if err == sql.ErrNoRows {
419+
if errors.Is(err, sql.ErrNoRows) {
419420
writeError(c, http.StatusNotFound, "图片不存在")
420421
return
421422
}
@@ -449,7 +450,7 @@ func (h *Handler) GetSignedImageAssetFile(c *gin.Context) {
449450
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
450451
defer cancel()
451452
asset, err := h.db.GetImageAsset(ctx, id)
452-
if err == sql.ErrNoRows {
453+
if errors.Is(err, sql.ErrNoRows) {
453454
writeError(c, http.StatusNotFound, "图片不存在")
454455
return
455456
}
@@ -603,7 +604,7 @@ func (h *Handler) DeleteImageAsset(c *gin.Context) {
603604
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
604605
defer cancel()
605606
asset, err := h.db.GetImageAsset(ctx, id)
606-
if err == sql.ErrNoRows {
607+
if errors.Is(err, sql.ErrNoRows) {
607608
writeError(c, http.StatusNotFound, "图片不存在")
608609
return
609610
}
@@ -981,13 +982,13 @@ func (h *Handler) upscaleImageJobAsset(ctx context.Context, jobID int64, assetIn
981982
func (h *Handler) resolveImageJobAPIKey(ctx context.Context, id int64) (*database.APIKeyRow, error) {
982983
if id > 0 {
983984
key, err := h.db.GetAPIKeyByID(ctx, id)
984-
if err == sql.ErrNoRows {
985+
if errors.Is(err, sql.ErrNoRows) {
985986
return nil, fmt.Errorf("API Key 不存在")
986987
}
987988
return key, err
988989
}
989990
key, err := h.db.FirstAPIKey(ctx)
990-
if err == sql.ErrNoRows {
991+
if errors.Is(err, sql.ErrNoRows) {
991992
return nil, nil
992993
}
993994
return key, err

database/models.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package database
33
import (
44
"context"
55
"database/sql"
6+
"errors"
67
"time"
78
)
89

@@ -134,7 +135,7 @@ func (db *DB) GetModelRegistrySyncState(ctx context.Context) (*ModelRegistrySync
134135
err := db.conn.QueryRowContext(ctx, `
135136
SELECT source_url, last_synced_at FROM model_registry_sync WHERE id = 1
136137
`).Scan(&sourceURL, &syncedRaw)
137-
if err == sql.ErrNoRows {
138+
if errors.Is(err, sql.ErrNoRows) {
138139
return nil, nil
139140
}
140141
if err != nil {

database/postgres.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ func (db *DB) GetSystemSettings(ctx context.Context) (*SystemSettings, error) {
11671167
&s.UsageLogFlushIntervalSeconds, &s.StreamFlushPolicy, &s.StreamFlushIntervalMS,
11681168
&s.ImageStorageConfig,
11691169
)
1170-
if err == sql.ErrNoRows {
1170+
if errors.Is(err, sql.ErrNoRows) {
11711171
return nil, nil
11721172
}
11731173
s.SiteName = NormalizeSiteName(s.SiteName)
@@ -1423,7 +1423,7 @@ func (db *DB) UpdateProxy(ctx context.Context, id int64, urlValue *string, label
14231423
if urlValue == nil && label == nil && enabled == nil {
14241424
var exists int
14251425
if err := db.conn.QueryRowContext(ctx, `SELECT 1 FROM proxies WHERE id = $1`, id).Scan(&exists); err != nil {
1426-
if err == sql.ErrNoRows {
1426+
if errors.Is(err, sql.ErrNoRows) {
14271427
return sql.ErrNoRows
14281428
}
14291429
return err
@@ -3105,7 +3105,7 @@ func (db *DB) GetAccountByID(ctx context.Context, id int64) (*AccountRow, error)
31053105
&updatedAtRaw,
31063106
)
31073107
if err != nil {
3108-
if err == sql.ErrNoRows {
3108+
if errors.Is(err, sql.ErrNoRows) {
31093109
return nil, sql.ErrNoRows
31103110
}
31113111
return nil, fmt.Errorf("查询账号失败: %w", err)
@@ -3176,7 +3176,7 @@ func (db *DB) UpdateAccountSchedulerConfig(ctx context.Context, id int64, scoreB
31763176
}
31773177
var exists int
31783178
if err := tx.QueryRowContext(ctx, query, id).Scan(&exists); err != nil {
3179-
if err == sql.ErrNoRows {
3179+
if errors.Is(err, sql.ErrNoRows) {
31803180
return sql.ErrNoRows
31813181
}
31823182
return err

0 commit comments

Comments
 (0)