Skip to content

Commit cd4e043

Browse files
committed
refactor: use gorm.ErrRecordNotFound for proper error handling
1 parent 7679816 commit cd4e043

File tree

8 files changed

+38
-37
lines changed

8 files changed

+38
-37
lines changed

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ const PrefixJtiBlacklist string = "gorest-blacklist-jti:"
368368
### Key Constants (database)
369369

370370
```go
371-
const RecordNotFound string = "record not found"
371+
// Use errors.Is(err, gorm.ErrRecordNotFound) instead of string matching.
372372
var RedisConnTTL int // context deadline in seconds for Redis connections
373373
```
374374

database/dbConnect.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ import (
3535
opts "go.mongodb.org/mongo-driver/mongo/options"
3636
)
3737

38-
// RecordNotFound is the error message for record not found.
39-
const RecordNotFound string = "record not found"
40-
4138
// dbClient holds the gorm DB connection instance.
4239
var dbClient *gorm.DB
4340

handler/auth.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
log "github.com/sirupsen/logrus"
12+
"gorm.io/gorm"
1213

1314
"github.com/pilinux/argon2"
1415
"github.com/pilinux/crypt"
@@ -48,7 +49,7 @@ func CreateUserAuth(auth model.Auth) (httpResponse model.HTTPResponse, httpStatu
4849
// email must be unique
4950
err := db.Where("email = ?", auth.Email).First(&auth).Error
5051
if err != nil {
51-
if err.Error() != database.RecordNotFound {
52+
if !errors.Is(err, gorm.ErrRecordNotFound) {
5253
// db read error
5354
log.WithError(err).Error("error code: 1002.1")
5455
httpResponse.Message = "internal server error"
@@ -68,7 +69,7 @@ func CreateUserAuth(auth model.Auth) (httpResponse model.HTTPResponse, httpStatu
6869
if !config.IsCipher() {
6970
err := db.Where("email_hash IS NOT NULL AND email_hash != ?", "").First(&auth).Error
7071
if err != nil {
71-
if err.Error() != database.RecordNotFound {
72+
if !errors.Is(err, gorm.ErrRecordNotFound) {
7273
// db read error
7374
log.WithError(err).Error("error code: 1002.2")
7475
httpResponse.Message = "internal server error"
@@ -106,7 +107,7 @@ func CreateUserAuth(auth model.Auth) (httpResponse model.HTTPResponse, httpStatu
106107
// email must be unique
107108
err = db.Where("email_hash = ?", authFinal.EmailHash).First(&auth).Error
108109
if err != nil {
109-
if err.Error() != database.RecordNotFound {
110+
if !errors.Is(err, gorm.ErrRecordNotFound) {
110111
// db read error
111112
log.WithError(err).Error("error code: 1002.4")
112113
httpResponse.Message = "internal server error"
@@ -205,7 +206,7 @@ func UpdateEmail(claims middleware.MyCustomClaims, req model.TempEmail) (httpRes
205206
// step 2: verify that this email is not registered to anyone
206207
_, err := service.GetUserByEmail(req.Email, false)
207208
if err != nil {
208-
if err.Error() != database.RecordNotFound {
209+
if !errors.Is(err, gorm.ErrRecordNotFound) {
209210
// db read error
210211
log.WithError(err).Error("error code: 1003.21")
211212
httpResponse.Message = "internal server error"
@@ -267,7 +268,7 @@ func UpdateEmail(claims middleware.MyCustomClaims, req model.TempEmail) (httpRes
267268
tEmailDB := model.TempEmail{}
268269
err = db.Where("id_auth = ?", claims.AuthID).First(&tEmailDB).Error
269270
if err != nil {
270-
if err.Error() != database.RecordNotFound {
271+
if !errors.Is(err, gorm.ErrRecordNotFound) {
271272
// db read error
272273
log.WithError(err).Error("error code: 1003.61")
273274
httpResponse.Message = "internal server error"

handler/login.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package handler
22

33
import (
44
"encoding/base64"
5+
"errors"
56
"net/http"
67
"strings"
78

89
"github.com/pilinux/argon2"
910
log "github.com/sirupsen/logrus"
11+
"gorm.io/gorm"
1012

1113
"github.com/pilinux/gorest/config"
1214
"github.com/pilinux/gorest/database"
@@ -31,7 +33,7 @@ func Login(payload model.AuthPayload) (httpResponse model.HTTPResponse, httpStat
3133

3234
v, err := service.GetUserByEmail(payload.Email, false)
3335
if err != nil {
34-
if err.Error() != database.RecordNotFound {
36+
if !errors.Is(err, gorm.ErrRecordNotFound) {
3537
// db read error
3638
log.WithError(err).Error("error code: 1013.1")
3739
httpResponse.Message = "internal server error"
@@ -88,7 +90,7 @@ func Login(payload model.AuthPayload) (httpResponse model.HTTPResponse, httpStat
8890
// have the user configured 2FA
8991
err := db.Where("id_auth = ?", v.AuthID).First(&twoFA).Error
9092
if err != nil {
91-
if err.Error() != database.RecordNotFound {
93+
if !errors.Is(err, gorm.ErrRecordNotFound) {
9294
// db read error
9395
log.WithError(err).Error("error code: 1013.3")
9496
httpResponse.Message = "internal server error"

handler/passwordReset.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/mediocregopher/radix/v4"
1414
"github.com/pilinux/argon2"
1515
log "github.com/sirupsen/logrus"
16+
"gorm.io/gorm"
1617

1718
"github.com/pilinux/gorest/config"
1819
"github.com/pilinux/gorest/database"
@@ -38,7 +39,7 @@ func PasswordForgot(authPayload model.AuthPayload) (httpResponse model.HTTPRespo
3839
// find user
3940
v, err := service.GetUserByEmail(authPayload.Email, true)
4041
if err != nil {
41-
if err.Error() != database.RecordNotFound {
42+
if !errors.Is(err, gorm.ErrRecordNotFound) {
4243
// db read error
4344
log.WithError(err).Error("error code: 1030.1")
4445
httpResponse.Message = "internal server error"
@@ -173,7 +174,7 @@ func PasswordRecover(authPayload model.AuthPayload) (httpResponse model.HTTPResp
173174
isEmail := lib.ValidateEmail(data.value)
174175
if isEmail {
175176
if err := db.Where("email = ?", data.value).First(&auth).Error; err != nil {
176-
if err.Error() != database.RecordNotFound {
177+
if !errors.Is(err, gorm.ErrRecordNotFound) {
177178
// db read error
178179
log.WithError(err).Error("error code: 1021.4")
179180
httpResponse.Message = "internal server error"
@@ -189,7 +190,7 @@ func PasswordRecover(authPayload model.AuthPayload) (httpResponse model.HTTPResp
189190
}
190191
if !isEmail {
191192
if err := db.Where("email_hash = ?", data.value).First(&auth).Error; err != nil {
192-
if err.Error() != database.RecordNotFound {
193+
if !errors.Is(err, gorm.ErrRecordNotFound) {
193194
// db read error
194195
log.WithError(err).Error("error code: 1021.5")
195196
httpResponse.Message = "internal server error"
@@ -214,7 +215,7 @@ func PasswordRecover(authPayload model.AuthPayload) (httpResponse model.HTTPResp
214215
// is user account protected by 2FA
215216
err := db.Where("id_auth = ?", auth.AuthID).First(&twoFA).Error
216217
if err != nil {
217-
if err.Error() != database.RecordNotFound {
218+
if !errors.Is(err, gorm.ErrRecordNotFound) {
218219
// db read error
219220
log.WithError(err).Error("error code: 1021.6")
220221
httpResponse.Message = "internal server error"
@@ -473,7 +474,7 @@ func PasswordUpdate(claims middleware.MyCustomClaims, authPayload model.AuthPayl
473474
if configSecurity.Must2FA == config.Activated {
474475
err := db.Where("id_auth = ?", claims.AuthID).First(&twoFA).Error
475476
if err != nil {
476-
if err.Error() != database.RecordNotFound {
477+
if !errors.Is(err, gorm.ErrRecordNotFound) {
477478
// db read error
478479
log.WithError(err).Error("error code: 1026.3")
479480
httpResponse.Message = "internal server error"

handler/twoFA.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handler
33
import (
44
"encoding/base64"
55
"encoding/hex"
6+
"errors"
67
"net/http"
78
"os"
89
"strings"
@@ -12,6 +13,7 @@ import (
1213
"github.com/pilinux/argon2"
1314
"github.com/pilinux/crypt"
1415
log "github.com/sirupsen/logrus"
16+
"gorm.io/gorm"
1517

1618
"github.com/pilinux/gorest/config"
1719
"github.com/pilinux/gorest/database"
@@ -53,11 +55,11 @@ func Setup2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload) (
5355
// is 2FA disabled/never configured before
5456
db := database.GetDB()
5557
twoFA := model.TwoFA{}
56-
// err == RecordNotFound => never configured before
58+
// err == gorm.ErrRecordNotFound => never configured before
5759
// err == nil => 2FA disabled
5860
err := db.Where("id_auth = ?", claims.AuthID).First(&twoFA).Error
5961
if err != nil {
60-
if err.Error() != database.RecordNotFound {
62+
if !errors.Is(err, gorm.ErrRecordNotFound) {
6163
// db read error
6264
log.WithError(err).Error("error code: 1051.1")
6365
httpResponse.Message = "internal server error"
@@ -78,7 +80,7 @@ func Setup2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload) (
7880
v := model.Auth{}
7981
err = db.Where("auth_id = ?", claims.AuthID).First(&v).Error
8082
if err != nil {
81-
if err.Error() != database.RecordNotFound {
83+
if !errors.Is(err, gorm.ErrRecordNotFound) {
8284
// db read error
8385
log.WithError(err).Error("error code: 1051.2")
8486
httpResponse.Message = "internal server error"
@@ -287,7 +289,7 @@ func Activate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload
287289
available := false
288290
err = db.Where("id_auth = ?", claims.AuthID).First(&twoFA).Error
289291
if err != nil {
290-
if err.Error() != database.RecordNotFound {
292+
if !errors.Is(err, gorm.ErrRecordNotFound) {
291293
// db read error
292294
log.WithError(err).Error("error code: 1052.41")
293295
httpResponse.Message = "internal server error"
@@ -515,7 +517,7 @@ func Validate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPayload
515517
twoFA := model.TwoFA{}
516518
// no record in DB!
517519
if err := db.Where("id_auth = ?", claims.AuthID).First(&twoFA).Error; err != nil {
518-
if err.Error() != database.RecordNotFound {
520+
if !errors.Is(err, gorm.ErrRecordNotFound) {
519521
// db read error
520522
log.WithError(err).Error("error code: 1053.31")
521523
httpResponse.Message = "internal server error"
@@ -678,7 +680,7 @@ func Deactivate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPaylo
678680
db := database.GetDB()
679681
v := model.Auth{}
680682
if err := db.Where("auth_id = ?", claims.AuthID).First(&v).Error; err != nil {
681-
if err.Error() != database.RecordNotFound {
683+
if !errors.Is(err, gorm.ErrRecordNotFound) {
682684
// db read error
683685
log.WithError(err).Error("error code: 1054.1")
684686
httpResponse.Message = "internal server error"
@@ -710,7 +712,7 @@ func Deactivate2FA(claims middleware.MyCustomClaims, authPayload model.AuthPaylo
710712

711713
err = db.Where("id_auth = ?", v.AuthID).First(&twoFA).Error
712714
if err != nil {
713-
if err.Error() != database.RecordNotFound {
715+
if !errors.Is(err, gorm.ErrRecordNotFound) {
714716
// db read error
715717
log.WithError(err).Error("error code: 1054.3")
716718
httpResponse.Message = "internal server error"
@@ -829,7 +831,7 @@ func CreateBackup2FA(claims middleware.MyCustomClaims, authPayload model.AuthPay
829831
db := database.GetDB()
830832
v := model.Auth{}
831833
if err := db.Where("auth_id = ?", claims.AuthID).First(&v).Error; err != nil {
832-
if err.Error() != database.RecordNotFound {
834+
if !errors.Is(err, gorm.ErrRecordNotFound) {
833835
// db read error
834836
log.WithError(err).Error("error code: 1055.1")
835837
httpResponse.Message = "internal server error"

handler/verification.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/mediocregopher/radix/v4"
1111
"github.com/pilinux/argon2"
1212
log "github.com/sirupsen/logrus"
13+
"gorm.io/gorm"
1314

1415
"github.com/pilinux/gorest/config"
1516
"github.com/pilinux/gorest/database"
@@ -84,7 +85,7 @@ func VerifyEmail(payload model.AuthPayload) (httpResponse model.HTTPResponse, ht
8485
isEmail := lib.ValidateEmail(data.value)
8586
if isEmail {
8687
if err := db.Where("email = ?", data.value).First(&auth).Error; err != nil {
87-
if err.Error() != database.RecordNotFound {
88+
if !errors.Is(err, gorm.ErrRecordNotFound) {
8889
// db read error
8990
log.WithError(err).Error("error code: 1061.5")
9091
httpResponse.Message = "internal server error"
@@ -101,7 +102,7 @@ func VerifyEmail(payload model.AuthPayload) (httpResponse model.HTTPResponse, ht
101102
}
102103
if !isEmail {
103104
if err := db.Where("email_hash = ?", data.value).First(&auth).Error; err != nil {
104-
if err.Error() != database.RecordNotFound {
105+
if !errors.Is(err, gorm.ErrRecordNotFound) {
105106
// db read error
106107
log.WithError(err).Error("error code: 1061.7")
107108
httpResponse.Message = "internal server error"
@@ -155,7 +156,7 @@ func CreateVerificationEmail(payload model.AuthPayload) (httpResponse model.HTTP
155156

156157
v, err := service.GetUserByEmail(payload.Email, true)
157158
if err != nil {
158-
if err.Error() != database.RecordNotFound {
159+
if !errors.Is(err, gorm.ErrRecordNotFound) {
159160
// db read error
160161
log.WithError(err).Error("error code: 1062.1")
161162
httpResponse.Message = "internal server error"
@@ -279,7 +280,7 @@ func VerifyUpdatedEmail(payload model.AuthPayload) (httpResponse model.HTTPRespo
279280
if isEmail {
280281
// check 'temp_emails' with the email in plaintext
281282
if err := db.Where("email = ?", data.value).First(&tempEmail).Error; err != nil {
282-
if err.Error() != database.RecordNotFound {
283+
if !errors.Is(err, gorm.ErrRecordNotFound) {
283284
// db read error
284285
log.WithError(err).Error("error code: 1063.5")
285286
httpResponse.Message = "internal server error"
@@ -298,7 +299,7 @@ func VerifyUpdatedEmail(payload model.AuthPayload) (httpResponse model.HTTPRespo
298299
if !isEmail {
299300
// check 'temp_emails' with hash of the email
300301
if err := db.Where("email_hash = ?", data.value).First(&tempEmail).Error; err != nil {
301-
if err.Error() != database.RecordNotFound {
302+
if !errors.Is(err, gorm.ErrRecordNotFound) {
302303
// db read error
303304
log.WithError(err).Error("error code: 1063.6")
304305
httpResponse.Message = "internal server error"
@@ -321,7 +322,7 @@ func VerifyUpdatedEmail(payload model.AuthPayload) (httpResponse model.HTTPRespo
321322
err := db.Where("email = ?", tempEmail.Email).First(&auth).Error
322323

323324
if err != nil {
324-
if err.Error() != database.RecordNotFound {
325+
if !errors.Is(err, gorm.ErrRecordNotFound) {
325326
// db read error
326327
log.WithError(err).Error("error code: 1063.71")
327328
httpResponse.Message = "internal server error"
@@ -339,7 +340,7 @@ func VerifyUpdatedEmail(payload model.AuthPayload) (httpResponse model.HTTPRespo
339340
err := db.Where("email_hash = ?", tempEmail.EmailHash).First(&auth).Error
340341

341342
if err != nil {
342-
if err.Error() != database.RecordNotFound {
343+
if !errors.Is(err, gorm.ErrRecordNotFound) {
343344
// db read error
344345
log.WithError(err).Error("error code: 1063.72")
345346
httpResponse.Message = "internal server error"
@@ -356,7 +357,7 @@ func VerifyUpdatedEmail(payload model.AuthPayload) (httpResponse model.HTTPRespo
356357

357358
// fetch auth data
358359
if err := db.Where("auth_id = ?", tempEmail.IDAuth).First(&auth).Error; err != nil {
359-
if err.Error() != database.RecordNotFound {
360+
if !errors.Is(err, gorm.ErrRecordNotFound) {
360361
// db read error
361362
log.WithError(err).Error("error code: 1063.73")
362363
httpResponse.Message = "internal server error"
@@ -423,7 +424,7 @@ func GetUnverifiedEmail(claims middleware.MyCustomClaims) (httpResponse model.HT
423424
// check 'temp_emails'
424425
err := db.Where("id_auth = ?", claims.AuthID).First(&tempEmail).Error
425426
if err != nil {
426-
if err.Error() != database.RecordNotFound {
427+
if !errors.Is(err, gorm.ErrRecordNotFound) {
427428
// db read error
428429
log.WithError(err).Error("error code: 1064.1")
429430
httpResponse.Message = "internal server error"
@@ -487,7 +488,7 @@ func ResendVerificationCodeToModifyActiveEmail(claims middleware.MyCustomClaims)
487488
// check 'temp_emails'
488489
err := db.Where("id_auth = ?", claims.AuthID).First(&tempEmail).Error
489490
if err != nil {
490-
if err.Error() != database.RecordNotFound {
491+
if !errors.Is(err, gorm.ErrRecordNotFound) {
491492
// db read error
492493
log.WithError(err).Error("error code: 1065.1")
493494
httpResponse.Message = "internal server error"

llms.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,6 @@ gconfig.IsPasswordRecoverCodeUUIDv4() // EMAIL_PASS_RECOVER_USE_UUIDv4 (bool c
580580
```go
581581
import gdb "github.com/pilinux/gorest/database"
582582

583-
// Constants
584-
const gdb.RecordNotFound = "record not found"
585-
586583
// Variables
587584
var gdb.RedisConnTTL int // context deadline in seconds for Redis connections (set by InitRedis)
588585

0 commit comments

Comments
 (0)