Skip to content

Commit 9eeb211

Browse files
authored
fix(api): catch db no rows (#2525)
1 parent a428f89 commit 9eeb211

5 files changed

Lines changed: 24 additions & 10 deletions

File tree

.golangci.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,27 @@ linters:
5959
- wsl
6060
- wsl_v5
6161

62+
exclusions:
63+
rules:
64+
- path: '^packages/db/pkg/dberrors/dberrors\.go$'
65+
linters:
66+
- forbidigo
67+
- path: '.*_test\.go$'
68+
text: "Use github.com/e2b-dev/infra/packages/db/pkg/dberrors.IsNotFoundError instead."
69+
linters:
70+
- forbidigo
71+
6272
settings:
6373
forbidigo:
6474
forbid:
6575
- pattern: "^new$"
6676
msg: "Use &Type{} instead."
77+
- pattern: '.*\.ErrNoRows$'
78+
pkg: '^database/sql$'
79+
msg: "Use github.com/e2b-dev/infra/packages/db/pkg/dberrors.IsNotFoundError instead."
80+
- pattern: '.*\.ErrNoRows$'
81+
pkg: '^github\.com/jackc/pgx/v5$'
82+
msg: "Use github.com/e2b-dev/infra/packages/db/pkg/dberrors.IsNotFoundError instead."
6783
# - pattern: os\.Getenv
6884
# msg: "Add your field to the configuration model instead."
6985
- pattern: 'zap\.New$'

packages/api/internal/cache/snapshots/snapshot_cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package snapshotcache
22

33
import (
44
"context"
5-
"database/sql"
65
"errors"
76
"fmt"
87
"time"
@@ -11,6 +10,7 @@ import (
1110
"go.opentelemetry.io/otel"
1211

1312
sqlcdb "github.com/e2b-dev/infra/packages/db/client"
13+
"github.com/e2b-dev/infra/packages/db/pkg/dberrors"
1414
"github.com/e2b-dev/infra/packages/db/queries"
1515
"github.com/e2b-dev/infra/packages/shared/pkg/cache"
1616
)
@@ -79,7 +79,7 @@ func (c *SnapshotCache) fetchFromDB(ctx context.Context, sandboxID string) (*Sna
7979

8080
row, err := c.db.GetLastSnapshot(ctx, sandboxID)
8181
if err != nil {
82-
if errors.Is(err, sql.ErrNoRows) {
82+
if dberrors.IsNotFoundError(err) {
8383
return errNotFoundSentinel, nil
8484
}
8585

packages/api/internal/cache/templates/template_build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package templatecache
22

33
import (
44
"context"
5-
"database/sql"
65
"errors"
76
"fmt"
87
"time"
@@ -11,6 +10,7 @@ import (
1110
"github.com/redis/go-redis/v9"
1211

1312
sqlcdb "github.com/e2b-dev/infra/packages/db/client"
13+
"github.com/e2b-dev/infra/packages/db/pkg/dberrors"
1414
"github.com/e2b-dev/infra/packages/db/pkg/types"
1515
"github.com/e2b-dev/infra/packages/db/queries"
1616
"github.com/e2b-dev/infra/packages/shared/pkg/cache"
@@ -76,7 +76,7 @@ func (c *TemplatesBuildCache) fetchFromDB(templateID string, buildID uuid.UUID)
7676
BuildID: buildID,
7777
})
7878
if err != nil {
79-
if errors.Is(err, sql.ErrNoRows) {
79+
if dberrors.IsNotFoundError(err) {
8080
return TemplateBuildInfo{}, ErrTemplateBuildInfoNotFound
8181
}
8282

packages/api/internal/handlers/accesstoken.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package handlers
22

33
import (
4-
"database/sql"
5-
"errors"
64
"fmt"
75
"net/http"
86

@@ -12,6 +10,7 @@ import (
1210
"github.com/e2b-dev/infra/packages/api/internal/api"
1311
"github.com/e2b-dev/infra/packages/auth/pkg/auth"
1412
authqueries "github.com/e2b-dev/infra/packages/db/pkg/auth/queries"
13+
"github.com/e2b-dev/infra/packages/db/pkg/dberrors"
1514
"github.com/e2b-dev/infra/packages/shared/pkg/ginutils"
1615
"github.com/e2b-dev/infra/packages/shared/pkg/keys"
1716
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
@@ -90,7 +89,7 @@ func (a *APIStore) DeleteAccessTokensAccessTokenID(c *gin.Context, accessTokenID
9089
ID: accessTokenIDParsed,
9190
UserID: userID,
9291
})
93-
if errors.Is(err, sql.ErrNoRows) {
92+
if dberrors.IsNotFoundError(err) {
9493
c.String(http.StatusNotFound, "id not found")
9594

9695
return

packages/api/internal/handlers/apikey.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package handlers
22

33
import (
4-
"database/sql"
5-
"errors"
64
"fmt"
75
"net/http"
86
"time"
@@ -15,6 +13,7 @@ import (
1513
"github.com/e2b-dev/infra/packages/api/internal/team"
1614
"github.com/e2b-dev/infra/packages/auth/pkg/auth"
1715
"github.com/e2b-dev/infra/packages/db/pkg/auth/queries"
16+
"github.com/e2b-dev/infra/packages/db/pkg/dberrors"
1817
"github.com/e2b-dev/infra/packages/shared/pkg/ginutils"
1918
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
2019
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
@@ -50,7 +49,7 @@ func (a *APIStore) PatchApiKeysApiKeyID(c *gin.Context, apiKeyID string) {
5049
ID: apiKeyIDParsed,
5150
TeamID: teamID,
5251
})
53-
if errors.Is(err, sql.ErrNoRows) {
52+
if dberrors.IsNotFoundError(err) {
5453
c.String(http.StatusNotFound, "id not found")
5554

5655
return

0 commit comments

Comments
 (0)