Skip to content

Commit 56d3b0d

Browse files
authored
Merge pull request #2 from mbaraa/chore/remove-errorx
Chore: Remove errorx
2 parents 83540c2 + 49d7323 commit 56d3b0d

File tree

8 files changed

+23
-44
lines changed

8 files changed

+23
-44
lines changed

db/base_db.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func NewBaseDB[T AllowedModels](db *gorm.DB) UnsafeCRUDRepo[T] {
1919
// the new object is a pointer, so it updates the object's id after creation
2020
func (b *BaseDB[T]) Add(obj *T) error {
2121
if obj == nil {
22-
return ErrNilObject.NewWithNoMessage()
22+
return ErrNilObject
2323
}
2424

2525
err := b.db.
@@ -37,7 +37,7 @@ func (b *BaseDB[T]) Add(obj *T) error {
3737
// AddMany is same as Add but for numerous objects
3838
func (b *BaseDB[T]) AddMany(objs []*T) error {
3939
if len(objs) == 0 {
40-
return ErrNilObject.NewWithNoMessage()
40+
return ErrNilObject
4141
}
4242

4343
err := b.db.
@@ -81,7 +81,7 @@ func (b *BaseDB[T]) Get(id uint) (T, error) {
8181
// which uses a given search condition and retrieves every record with the given condition
8282
func (b *BaseDB[T]) GetByConds(conds ...any) ([]T, error) {
8383
if !checkConds(conds...) {
84-
return nil, ErrInvalidWhereConditions.NewWithNoMessage()
84+
return nil, ErrInvalidWhereConditions
8585
}
8686

8787
var foundRecords []T
@@ -92,7 +92,7 @@ func (b *BaseDB[T]) GetByConds(conds ...any) ([]T, error) {
9292
Error
9393

9494
if err != nil || len(foundRecords) == 0 {
95-
return nil, ErrRecordNotFound.NewWithNoMessage()
95+
return nil, ErrRecordNotFound
9696
}
9797

9898
return foundRecords, nil
@@ -124,11 +124,11 @@ func (b *BaseDB[T]) Count() int64 {
124124
// and gives it its id(in case searching condition weren't using id)
125125
func (b *BaseDB[T]) Update(obj *T, conds ...any) error {
126126
if obj == nil {
127-
return ErrNilObject.NewWithNoMessage()
127+
return ErrNilObject
128128
}
129129
if len(conds) > 1 {
130130
if !checkConds(conds...) {
131-
return ErrInvalidWhereConditions.NewWithNoMessage()
131+
return ErrInvalidWhereConditions
132132
}
133133
} else {
134134
conds = []any{"id = ?", (*obj).GetId()}

db/errors.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
package db
22

33
import (
4-
"dankmuzikk/errors"
5-
6-
goerrors "errors"
4+
"errors"
75

86
"github.com/go-sql-driver/mysql"
97
)
108

11-
var DbErrNamespace = errors.DankMuzikkErrNamespace.NewSubNamespace("database error")
12-
139
var (
14-
ErrInvalidModel = DbErrNamespace.NewType("model does not implement the `AllowedModel` interface")
15-
ErrNilObject = DbErrNamespace.NewType("object's pointer is nil")
16-
ErrEmptySlice = DbErrNamespace.NewType("slice is nil or empty")
17-
ErrInvalidWhereConditions = DbErrNamespace.NewType("invalid where conditions")
18-
ErrRecordNotFound = DbErrNamespace.NewType("no records were found")
19-
ErrRecordExists = goerrors.New("record exists in table")
10+
ErrNilObject = errors.New("db: object's pointer is nil")
11+
ErrEmptySlice = errors.New("db: slice is nil or empty")
12+
ErrInvalidWhereConditions = errors.New("db: invalid where conditions")
13+
ErrRecordNotFound = errors.New("db: no records were found")
14+
ErrRecordExists = errors.New("db: record exists in table")
2015
)
2116

2217
func tryWrapMySqlError(err error) error {

errors/errors.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ go 1.22
44

55
require (
66
github.com/a-h/templ v0.2.663
7+
github.com/go-sql-driver/mysql v1.8.1
78
github.com/gocolly/colly v1.2.0
89
github.com/golang-jwt/jwt/v4 v4.5.0
910
github.com/google/uuid v1.6.0
10-
github.com/joomcode/errorx v1.1.1
11+
golang.org/x/crypto v0.22.0
1112
golang.org/x/oauth2 v0.19.0
1213
google.golang.org/api v0.175.0
1314
gorm.io/driver/mysql v1.5.6
@@ -27,7 +28,6 @@ require (
2728
github.com/felixge/httpsnoop v1.0.4 // indirect
2829
github.com/go-logr/logr v1.4.1 // indirect
2930
github.com/go-logr/stdr v1.2.2 // indirect
30-
github.com/go-sql-driver/mysql v1.8.1 // indirect
3131
github.com/gobwas/glob v0.2.3 // indirect
3232
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3333
github.com/golang/protobuf v1.5.4 // indirect
@@ -44,7 +44,6 @@ require (
4444
go.opentelemetry.io/otel v1.25.0 // indirect
4545
go.opentelemetry.io/otel/metric v1.25.0 // indirect
4646
go.opentelemetry.io/otel/trace v1.25.0 // indirect
47-
golang.org/x/crypto v0.22.0 // indirect
4847
golang.org/x/net v0.24.0 // indirect
4948
golang.org/x/sys v0.19.0 // indirect
5049
golang.org/x/text v0.14.0 // indirect

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
8686
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
8787
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
8888
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
89-
github.com/joomcode/errorx v1.1.1 h1:/LFG/qSk1gUTuZjs+qlyOJEpcVjD9DXgBNFhdZkQrjY=
90-
github.com/joomcode/errorx v1.1.1/go.mod h1:eQzdtdlNyN7etw6YCS4W4+lu442waxZYw5yvz0ULrRo=
9189
github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8NzpJ3o=
9290
github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak=
9391
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -98,7 +96,6 @@ github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d/go.mod h1:uugorj
9896
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9997
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
10098
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
101-
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
10299
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
103100
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
104101
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=

services/jwt/errors.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package jwt
22

33
import (
4-
"dankmuzikk/errors"
4+
"errors"
55
)
66

7-
var JwtErrNamespace = errors.DankMuzikkErrNamespace.NewSubNamespace("jwt error")
8-
97
var (
10-
ErrEmptyToken = JwtErrNamespace.NewType("empty token")
11-
ErrInvalidToken = JwtErrNamespace.NewType("invalid token")
12-
ErrExpiredToken = JwtErrNamespace.NewType("expired token")
13-
ErrInvalidSubject = JwtErrNamespace.NewType("invalid token's subject")
8+
ErrEmptyToken = errors.New("jwt: empty token")
9+
ErrInvalidToken = errors.New("jwt: invalid token")
10+
ErrExpiredToken = errors.New("jwt: expired token")
11+
ErrInvalidSubject = errors.New("jwt: invalid token's subject")
1412
)

services/jwt/jwt_impl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ func (s *JWTImpl[T]) Validate(token string, subject Subject) error {
5858
// Decode decodes the given token using the set JWT secret
5959
func (s *JWTImpl[T]) Decode(token string, subject Subject) (Claims[T], error) {
6060
if len(token) == 0 {
61-
return Claims[T]{}, ErrEmptyToken.NewWithNoMessage()
61+
return Claims[T]{}, ErrEmptyToken
6262
}
6363

6464
claims := Claims[T]{}
6565

6666
_, err := jwt.ParseWithClaims(token, &claims, func(token *jwt.Token) (interface{}, error) {
6767
if claims.Subject != subject {
68-
return nil, ErrInvalidSubject.NewWithNoMessage()
68+
return nil, ErrInvalidSubject
6969
}
7070
if claims.ExpiresAt.Time.Before(time.Now().UTC()) {
71-
return nil, ErrExpiredToken.NewWithNoMessage()
71+
return nil, ErrExpiredToken
7272
}
7373

7474
return []byte(config.Env().JwtSecret), nil

services/login/google.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"time"
1515

1616
"github.com/google/uuid"
17-
"github.com/joomcode/errorx"
1817
)
1918

2019
var (
@@ -66,7 +65,7 @@ func (g *GoogleLoginService) Login(state, code string) (string, error) {
6665
}
6766

6867
account, err := g.accountRepo.GetByConds("email = ? AND is_o_auth = 1", googleUser.Email)
69-
if errorx.IsOfType(err, db.ErrRecordNotFound) || len(account) == 0 {
68+
if errors.Is(err, db.ErrRecordNotFound) || len(account) == 0 {
7069
return g.Signup(googleUser)
7170
}
7271
if err != nil {

0 commit comments

Comments
 (0)