Skip to content

Commit

Permalink
fix: make errors public
Browse files Browse the repository at this point in the history
  • Loading branch information
savsgio committed Mar 16, 2023
1 parent 6e7a6ed commit 32b16ca
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 46 deletions.
6 changes: 4 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
8 changes: 5 additions & 3 deletions providers/memcache/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
8 changes: 4 additions & 4 deletions providers/memcache/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions providers/mysql/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
4 changes: 2 additions & 2 deletions providers/mysql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
6 changes: 4 additions & 2 deletions providers/postgre/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
4 changes: 2 additions & 2 deletions providers/postgre/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
11 changes: 6 additions & 5 deletions providers/redis/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 6 additions & 6 deletions providers/redis/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand All @@ -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 {
Expand Down Expand Up @@ -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{
Expand All @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion providers/sqlite3/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion providers/sqlite3/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
12 changes: 6 additions & 6 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -216,15 +216,15 @@ 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)
expiration := s.config.Expiration

newID := s.config.SessionIDGeneratorFunc()
if len(newID) == 0 {
return errEmptySessionID
return ErrEmptySessionID
}

providerExpiration := expiration
Expand All @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 32b16ca

Please sign in to comment.