Skip to content

Commit 95f5dcc

Browse files
committed
minor: renamed errors
* Renamed errors * Upgraded dependencies
1 parent 2fe0393 commit 95f5dcc

File tree

9 files changed

+43
-30
lines changed

9 files changed

+43
-30
lines changed

errors.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package go_databases
33
import "errors"
44

55
var (
6-
AlreadyConnectedError = errors.New("connection to database already established")
7-
FailedToConnectError = errors.New("failed to connect to database")
8-
FailedToPingError = errors.New("failed to ping database")
9-
NotConnectedError = errors.New("connection to database not established")
10-
FailedToDisconnectError = errors.New("failed to disconnect from database")
6+
ErrAlreadyConnected = errors.New("connection to database already established")
7+
ErrConnectionFailed = errors.New("failed to connect to database")
8+
ErrPingFailed = errors.New("failed to ping database")
9+
ErrNotConnected = errors.New("connection to database not established")
10+
ErrFailedToDisconnect = errors.New("failed to disconnect from database")
1111
)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.23.4
44

55
require (
66
github.com/go-redis/redis/v8 v8.11.5
7-
github.com/ralvarezdev/go-logger v0.1.0
7+
github.com/ralvarezdev/go-logger v0.2.2
88
go.mongodb.org/mongo-driver v1.17.1
99
golang.org/x/net v0.33.0
1010
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
2424
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
2525
github.com/ralvarezdev/go-logger v0.1.0 h1:i2AI1nlxU6Hizvk75Vc8wtFydiVrqIeeRbJwiuO/69A=
2626
github.com/ralvarezdev/go-logger v0.1.0/go.mod h1:v5OvFrkS+wsYNTCVegXWiRhBtcYrQJr4LDMDntvpAos=
27+
github.com/ralvarezdev/go-logger v0.2.2 h1:j58spza2L6s+vAibifHSuUPrXm0+6nWZWR3SOg8ihBk=
28+
github.com/ralvarezdev/go-logger v0.2.2/go.mod h1:v5OvFrkS+wsYNTCVegXWiRhBtcYrQJr4LDMDntvpAos=
2729
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
2830
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
2931
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=

mongodb/collection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (c *Collection) createIndexes(collection *mongo.Collection) (err error) {
5252
context.Background(), *index,
5353
)
5454
if err != nil {
55-
return fmt.Errorf(FailedToCreateIndexError, *index, err)
55+
return fmt.Errorf(ErrFailedToCreateIndex, *index, err)
5656
}
5757
}
5858
}

mongodb/connection.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ type (
3232
)
3333

3434
// NewDefaultConnectionHandler creates a new connection
35-
func NewDefaultConnectionHandler(config *Config) (*DefaultConnectionHandler, error) {
35+
func NewDefaultConnectionHandler(config *Config) (
36+
*DefaultConnectionHandler,
37+
error,
38+
) {
3639
// Check if the config is nil
3740
if config == nil {
38-
return nil, NilClientError
41+
return nil, ErrNilClient
3942
}
4043

4144
// Set client options
@@ -54,21 +57,21 @@ func NewDefaultConnectionHandler(config *Config) (*DefaultConnectionHandler, err
5457
func (d *DefaultConnectionHandler) Connect() (*mongo.Client, error) {
5558
// Check if the connection is already established
5659
if d.Client != nil {
57-
return d.Client, godatabases.AlreadyConnectedError
60+
return d.Client, godatabases.ErrAlreadyConnected
5861
}
5962

6063
// Connect to MongoDB
6164
client, err := mongo.Connect(d.Ctx, d.ClientOptions)
6265

6366
// Create MongoDB Connection struct
6467
if err != nil {
65-
return nil, godatabases.FailedToConnectError
68+
return nil, godatabases.ErrConnectionFailed
6669
}
6770

6871
// Check the connection
6972
err = client.Ping(context.Background(), nil)
7073
if err != nil {
71-
return nil, godatabases.FailedToPingError
74+
return nil, godatabases.ErrPingFailed
7275
}
7376

7477
// Set client
@@ -81,7 +84,7 @@ func (d *DefaultConnectionHandler) Connect() (*mongo.Client, error) {
8184
func (d *DefaultConnectionHandler) GetClient() (*mongo.Client, error) {
8285
// Check if the connection is established
8386
if d.Client == nil {
84-
return nil, godatabases.NotConnectedError
87+
return nil, godatabases.ErrNotConnected
8588
}
8689

8790
return d.Client, nil
@@ -98,7 +101,7 @@ func (d *DefaultConnectionHandler) Disconnect() {
98101
// Close the connection
99102
d.Cancel()
100103
if err := d.Client.Disconnect(d.Ctx); err != nil {
101-
panic(godatabases.FailedToDisconnectError)
104+
panic(godatabases.ErrFailedToDisconnect)
102105
}
103106
}()
104107
}

mongodb/errors.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package mongodb
33
import "errors"
44

55
var (
6-
FailedToCreateDocumentError = errors.New("failed to create document")
7-
FailedToStartSessionError = errors.New("failed to start session")
8-
FailedToCreateIndexError = "failed to create index '%v': %v"
9-
NilConfigError = errors.New("mongodb connection config cannot be nil")
10-
NilClientError = errors.New("mongodb client cannot be nil")
6+
ErrFailedToCreateDocument = errors.New("failed to create document")
7+
ErrFailedToStartSession = errors.New("failed to start session")
8+
ErrFailedToCreateIndex = "failed to create index '%v': %v"
9+
ErrNilConfig = errors.New("mongodb connection config cannot be nil")
10+
ErrNilClient = errors.New("mongodb client cannot be nil")
1111
)

mongodb/transactions.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ func CreateTransactionOptions() *options.TransactionOptions {
1717
func CreateSession(client *mongo.Client) (mongo.Session, error) {
1818
// Check if the client is nil
1919
if client == nil {
20-
return nil, NilClientError
20+
return nil, ErrNilClient
2121
}
2222

2323
return client.StartSession()
2424
}
2525

2626
// CreateTransaction creates a new transaction
27-
func CreateTransaction(client *mongo.Client, queries func(sc mongo.SessionContext) error) error {
27+
func CreateTransaction(
28+
client *mongo.Client,
29+
queries func(sc mongo.SessionContext) error,
30+
) error {
2831
// Create the session
2932
clientSession, err := CreateSession(client)
3033
if err != nil {
@@ -37,7 +40,9 @@ func CreateTransaction(client *mongo.Client, queries func(sc mongo.SessionContex
3740

3841
// Start the transaction
3942
err = mongo.WithSession(
40-
context.Background(), clientSession, func(sc mongo.SessionContext) error {
43+
context.Background(),
44+
clientSession,
45+
func(sc mongo.SessionContext) error {
4146
if err = clientSession.StartTransaction(transactionOptions); err != nil {
4247
return err
4348
}

redis/connection.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ type (
2929
)
3030

3131
// NewDefaultConnectionHandler creates a new connection
32-
func NewDefaultConnectionHandler(config *Config) (*DefaultConnectionHandler, error) {
32+
func NewDefaultConnectionHandler(config *Config) (
33+
*DefaultConnectionHandler,
34+
error,
35+
) {
3336
// Check if the config is nil
3437
if config == nil {
35-
return nil, NilConfigError
38+
return nil, ErrNilConfig
3639
}
3740

3841
// Define the Redis options
@@ -52,7 +55,7 @@ func NewDefaultConnectionHandler(config *Config) (*DefaultConnectionHandler, err
5255
func (d *DefaultConnectionHandler) Connect() (*redis.Client, error) {
5356
// Check if the connection is already established
5457
if d.Client != nil {
55-
return d.Client, godatabases.AlreadyConnectedError
58+
return d.Client, godatabases.ErrAlreadyConnected
5659
}
5760

5861
// Create a new Redis client
@@ -61,7 +64,7 @@ func (d *DefaultConnectionHandler) Connect() (*redis.Client, error) {
6164
// Ping the Redis server to check the connection
6265
_, err := client.Ping(context.Background()).Result()
6366
if err != nil {
64-
return nil, godatabases.FailedToPingError
67+
return nil, godatabases.ErrPingFailed
6568
}
6669

6770
// Set client
@@ -74,7 +77,7 @@ func (d *DefaultConnectionHandler) Connect() (*redis.Client, error) {
7477
func (d *DefaultConnectionHandler) GetClient() (*redis.Client, error) {
7578
// Check if the connection is established
7679
if d.Client == nil {
77-
return nil, godatabases.NotConnectedError
80+
return nil, godatabases.ErrNotConnected
7881
}
7982

8083
return d.Client, nil
@@ -90,7 +93,7 @@ func (d *DefaultConnectionHandler) Disconnect() {
9093

9194
// Close the connection
9295
if err := d.Client.Close(); err != nil {
93-
panic(godatabases.FailedToDisconnectError)
96+
panic(godatabases.ErrFailedToDisconnect)
9497
}
9598
}()
9699
}

redis/errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import (
55
)
66

77
var (
8-
NilClientError = errors.New("redis client cannot be nil")
9-
NilConfigError = errors.New("redis config cannot be nil")
8+
ErrNilClient = errors.New("redis client cannot be nil")
9+
ErrNilConfig = errors.New("redis config cannot be nil")
1010
)

0 commit comments

Comments
 (0)