diff --git a/errors.go b/errors.go index b9253b5..78c36b6 100644 --- a/errors.go +++ b/errors.go @@ -4,5 +4,7 @@ import ( "errors" ) -var errNotSetProvider = errors.New("Not setted a session provider") -var errEmptySessionID = errors.New("Empty session id") +var ( + ErrNotSetProvider = errors.New("Not setted a session provider") + ErrEmptySessionID = errors.New("Empty session id") +) diff --git a/providers/memcache/errors.go b/providers/memcache/errors.go index ed90133..1be6f65 100644 --- a/providers/memcache/errors.go +++ b/providers/memcache/errors.go @@ -2,6 +2,8 @@ package memcache import "errors" -var errConfigServerListEmpty = errors.New("Config ServerList must not be empty") -var errConfigMaxIdleConnsZero = errors.New("Config MaxIdleConns must be more than 0") -var errExpirationIsTooBig = errors.New("Expiration duration should be a 32 bits value") +var ( + ErrConfigServerListEmpty = errors.New("Config ServerList must not be empty") + ErrConfigMaxIdleConnsZero = errors.New("Config MaxIdleConns must be more than 0") + ErrExpirationIsTooBig = errors.New("Expiration duration should be a 32 bits value") +) diff --git a/providers/memcache/provider.go b/providers/memcache/provider.go index 3c23d46..f37c738 100644 --- a/providers/memcache/provider.go +++ b/providers/memcache/provider.go @@ -32,10 +32,10 @@ func releaseItem(item *memcache.Item) { // New returns a new memcache provider configured func New(cfg Config) (*Provider, error) { if len(cfg.ServerList) == 0 { - return nil, errConfigServerListEmpty + return nil, ErrConfigServerListEmpty } if cfg.MaxIdleConns <= 0 { - return nil, errConfigMaxIdleConnsZero + return nil, ErrConfigMaxIdleConnsZero } db := memcache.New(cfg.ServerList...) @@ -85,7 +85,7 @@ func (p *Provider) Get(id []byte) ([]byte, error) { func (p *Provider) Save(id, data []byte, expiration time.Duration) error { mcExpiration := int32(expiration.Seconds()) if mcExpiration > math.MaxInt32 { - return errExpirationIsTooBig + return ErrExpirationIsTooBig } item := acquireItem() @@ -114,7 +114,7 @@ func (p *Provider) Regenerate(id, newID []byte, expiration time.Duration) error if item != nil { // Exist mcExpiration := int32(expiration.Seconds()) if mcExpiration > math.MaxInt32 { - return errExpirationIsTooBig + return ErrExpirationIsTooBig } newItem := acquireItem() diff --git a/providers/mysql/errors.go b/providers/mysql/errors.go index 6dde803..4230bb9 100644 --- a/providers/mysql/errors.go +++ b/providers/mysql/errors.go @@ -2,5 +2,7 @@ package mysql import "errors" -var errConfigHostEmpty = errors.New("Config Host must not be empty") -var errConfigPortZero = errors.New("Config Port must be more than 0") +var ( + ErrConfigHostEmpty = errors.New("Config Host must not be empty") + ErrConfigPortZero = errors.New("Config Port must be more than 0") +) diff --git a/providers/mysql/provider.go b/providers/mysql/provider.go index dd7453c..9fcd412 100644 --- a/providers/mysql/provider.go +++ b/providers/mysql/provider.go @@ -27,10 +27,10 @@ var ( // New returns a new configured mysql provider func New(cfg Config) (*Provider, error) { if cfg.Host == "" { - return nil, errConfigHostEmpty + return nil, ErrConfigHostEmpty } if cfg.Port == 0 { - return nil, errConfigPortZero + return nil, ErrConfigPortZero } providerCfg := sql.ProviderConfig{ diff --git a/providers/postgre/errors.go b/providers/postgre/errors.go index 8a69e7c..e855b2f 100644 --- a/providers/postgre/errors.go +++ b/providers/postgre/errors.go @@ -2,5 +2,7 @@ package postgre import "errors" -var errConfigHostEmpty = errors.New("Config Host must not be empty") -var errConfigPortZero = errors.New("Config Port must be more than 0") +var ( + ErrConfigHostEmpty = errors.New("Config Host must not be empty") + ErrConfigPortZero = errors.New("Config Port must be more than 0") +) diff --git a/providers/postgre/provider.go b/providers/postgre/provider.go index ef863c5..20a09f8 100644 --- a/providers/postgre/provider.go +++ b/providers/postgre/provider.go @@ -26,10 +26,10 @@ var ( // New returns a new configured postgres provider func New(cfg Config) (*Provider, error) { if cfg.Host == "" { - return nil, errConfigHostEmpty + return nil, ErrConfigHostEmpty } if cfg.Port == 0 { - return nil, errConfigPortZero + return nil, ErrConfigPortZero } providerCfg := sql.ProviderConfig{ diff --git a/providers/redis/errors.go b/providers/redis/errors.go index 2a414fe..335be6a 100644 --- a/providers/redis/errors.go +++ b/providers/redis/errors.go @@ -5,10 +5,11 @@ import ( "fmt" ) -var errConfigAddrEmpty = errors.New("Config Addr must not be empty") - -var errConfigMasterNameEmpty = errors.New("Config MasterName must not be empty") +var ( + ErrConfigAddrEmpty = errors.New("Config Addr must not be empty") + ErrConfigMasterNameEmpty = errors.New("Config MasterName must not be empty") +) -func errRedisConnection(err error) error { - return fmt.Errorf("Redis connection error: %v", err) +func newErrRedisConnection(err error) error { + return fmt.Errorf("redis connection error: %w", err) } diff --git a/providers/redis/provider.go b/providers/redis/provider.go index f943145..598c3ba 100644 --- a/providers/redis/provider.go +++ b/providers/redis/provider.go @@ -13,7 +13,7 @@ var all = []byte("*") // New returns a new configured redis provider func New(cfg Config) (*Provider, error) { if cfg.Addr == "" { - return nil, errConfigAddrEmpty + return nil, ErrConfigAddrEmpty } if cfg.Logger != nil { @@ -43,7 +43,7 @@ func New(cfg Config) (*Provider, error) { }) if err := db.Ping(context.Background()).Err(); err != nil { - return nil, errRedisConnection(err) + return nil, newErrRedisConnection(err) } p := &Provider{ @@ -57,7 +57,7 @@ func New(cfg Config) (*Provider, error) { // NewFailover returns a new redis provider using sentinel to determine the redis server to connect to. func NewFailover(cfg FailoverConfig) (*Provider, error) { if cfg.MasterName == "" { - return nil, errConfigMasterNameEmpty + return nil, ErrConfigMasterNameEmpty } if cfg.Logger != nil { @@ -89,7 +89,7 @@ func NewFailover(cfg FailoverConfig) (*Provider, error) { }) if err := db.Ping(context.Background()).Err(); err != nil { - return nil, errRedisConnection(err) + return nil, newErrRedisConnection(err) } p := &Provider{ @@ -103,7 +103,7 @@ func NewFailover(cfg FailoverConfig) (*Provider, error) { // NewFailoverCluster returns a new redis provider using a group of sentinels to determine the redis server to connect to. func NewFailoverCluster(cfg FailoverConfig) (*Provider, error) { if cfg.MasterName == "" { - return nil, errConfigMasterNameEmpty + return nil, ErrConfigMasterNameEmpty } if cfg.Logger != nil { @@ -137,7 +137,7 @@ func NewFailoverCluster(cfg FailoverConfig) (*Provider, error) { }) if err := db.Ping(context.Background()).Err(); err != nil { - return nil, errRedisConnection(err) + return nil, newErrRedisConnection(err) } p := &Provider{ diff --git a/providers/sqlite3/errors.go b/providers/sqlite3/errors.go index 7c50f2a..f1b59c9 100644 --- a/providers/sqlite3/errors.go +++ b/providers/sqlite3/errors.go @@ -2,4 +2,4 @@ package sqlite3 import "errors" -var errConfigDBPathEmpty = errors.New("Config DBPath must not be empty") +var ErrConfigDBPathEmpty = errors.New("Config DBPath must not be empty") diff --git a/providers/sqlite3/provider.go b/providers/sqlite3/provider.go index c3ba462..ec276c4 100644 --- a/providers/sqlite3/provider.go +++ b/providers/sqlite3/provider.go @@ -26,7 +26,7 @@ var ( // New returns a new configured sqlite3 provider func New(cfg Config) (*Provider, error) { if cfg.DBPath == "" { - return nil, errConfigDBPathEmpty + return nil, ErrConfigDBPathEmpty } providerCfg := sql.ProviderConfig{ diff --git a/session.go b/session.go index c0071df..ea48660 100644 --- a/session.go +++ b/session.go @@ -146,7 +146,7 @@ func (s *Session) getSessionID(ctx *fasthttp.RequestCtx) []byte { // if it does not exist, it will be generated func (s *Session) Get(ctx *fasthttp.RequestCtx) (*Store, error) { if s.provider == nil { - return nil, errNotSetProvider + return nil, ErrNotSetProvider } newUser := false @@ -155,7 +155,7 @@ func (s *Session) Get(ctx *fasthttp.RequestCtx) (*Store, error) { if len(id) == 0 { id = s.config.SessionIDGeneratorFunc() if len(id) == 0 { - return nil, errEmptySessionID + return nil, ErrEmptySessionID } newUser = true @@ -185,7 +185,7 @@ func (s *Session) Get(ctx *fasthttp.RequestCtx) (*Store, error) { // For avoid it, defer this function in your request handler func (s *Session) Save(ctx *fasthttp.RequestCtx, store *Store) error { if s.provider == nil { - return errNotSetProvider + return ErrNotSetProvider } id := store.GetSessionID() @@ -216,7 +216,7 @@ func (s *Session) Save(ctx *fasthttp.RequestCtx, store *Store) error { // Regenerate generates a new session id to the current user func (s *Session) Regenerate(ctx *fasthttp.RequestCtx) error { if s.provider == nil { - return errNotSetProvider + return ErrNotSetProvider } id := s.getSessionID(ctx) @@ -224,7 +224,7 @@ func (s *Session) Regenerate(ctx *fasthttp.RequestCtx) error { newID := s.config.SessionIDGeneratorFunc() if len(newID) == 0 { - return errEmptySessionID + return ErrEmptySessionID } providerExpiration := expiration @@ -244,7 +244,7 @@ func (s *Session) Regenerate(ctx *fasthttp.RequestCtx) error { // Destroy destroys the session of the current user func (s *Session) Destroy(ctx *fasthttp.RequestCtx) error { if s.provider == nil { - return errNotSetProvider + return ErrNotSetProvider } sessionID := s.getSessionID(ctx) diff --git a/session_test.go b/session_test.go index c384929..d5859d4 100644 --- a/session_test.go +++ b/session_test.go @@ -251,8 +251,8 @@ func TestSession_GetErrNotProvider(t *testing.T) { store, err := s.Get(ctx) - if err != errNotSetProvider { - t.Errorf("Expected error: %v", errNotSetProvider) + if err != ErrNotSetProvider { + t.Errorf("Expected error: %v", ErrNotSetProvider) } if store != nil { @@ -272,8 +272,8 @@ func TestSession_GetErrEmptySessionID(t *testing.T) { store, err := s.Get(ctx) - if err != errEmptySessionID { - t.Errorf("Expected error: %v", errEmptySessionID) + if err != ErrEmptySessionID { + t.Errorf("Expected error: %v", ErrEmptySessionID) } if store != nil { @@ -371,8 +371,8 @@ func TestSession_RegenerateErrNotProvider(t *testing.T) { s := New(Config{}) ctx := new(fasthttp.RequestCtx) - if err := s.Regenerate(ctx); err != errNotSetProvider { - t.Errorf("Expected error: %v", errNotSetProvider) + if err := s.Regenerate(ctx); err != ErrNotSetProvider { + t.Errorf("Expected error: %v", ErrNotSetProvider) } } @@ -387,8 +387,8 @@ func TestSession_RegenerateErrEmptySessionID(t *testing.T) { ctx := new(fasthttp.RequestCtx) ctx.Request.Header.SetCookie(s.config.CookieName, "d32r2f2ecev") - if err := s.Regenerate(ctx); err != errEmptySessionID { - t.Errorf("Expected error: %v", errEmptySessionID) + if err := s.Regenerate(ctx); err != ErrEmptySessionID { + t.Errorf("Expected error: %v", ErrEmptySessionID) } } @@ -429,8 +429,8 @@ func TestSession_DestroyErrNotProvider(t *testing.T) { err := s.Destroy(ctx) - if err != errNotSetProvider { - t.Errorf("Expected error: %v", errNotSetProvider) + if err != ErrNotSetProvider { + t.Errorf("Expected error: %v", ErrNotSetProvider) } }