Skip to content

Commit

Permalink
add custom errors for cache
Browse files Browse the repository at this point in the history
  • Loading branch information
kkumar-gcc committed Oct 5, 2024
1 parent c2381bf commit e02f2ef
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
12 changes: 4 additions & 8 deletions cache/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/goravel/framework/contracts/cache"
"github.com/goravel/framework/contracts/config"
"github.com/goravel/framework/errors"
)

//go:generate mockery --name=Driver
Expand All @@ -30,17 +31,12 @@ func (d *DriverImpl) New(store string) (cache.Driver, error) {
case "custom":
return d.custom(store)
default:
return nil, fmt.Errorf("invalid driver: %s, only support memory, custom\n", driver)
return nil, errors.ErrCacheDriverNotSupported.Args(driver)
}
}

func (d *DriverImpl) memory() (cache.Driver, error) {
memory, err := NewMemory(d.config)
if err != nil {
return nil, fmt.Errorf("init memory driver error: %v", err)
}

return memory, nil
return NewMemory(d.config)
}

func (d *DriverImpl) custom(store string) (cache.Driver, error) {
Expand All @@ -51,5 +47,5 @@ func (d *DriverImpl) custom(store string) (cache.Driver, error) {
return custom()
}

return nil, fmt.Errorf("%s doesn't implement contracts/cache/store\n", store)
return nil, errors.ErrCacheStoreContractNotFulfilled.Args(store)
}
8 changes: 4 additions & 4 deletions cache/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cache

import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"
Expand All @@ -11,6 +10,7 @@ import (

contractscache "github.com/goravel/framework/contracts/cache"
"github.com/goravel/framework/contracts/config"
"github.com/goravel/framework/errors"
)

type Memory struct {
Expand All @@ -37,7 +37,7 @@ func (r *Memory) Add(key string, value any, t time.Duration) bool {
return !loaded
}

// Decrement Decrement the value of an item in the cache.
// Decrement decrements the value of an item in the cache.
func (r *Memory) Decrement(key string, value ...int64) (int64, error) {
if len(value) == 0 {
value = append(value, 1)
Expand All @@ -55,7 +55,7 @@ func (r *Memory) Decrement(key string, value ...int64) (int64, error) {
case *int32:
return int64(atomic.AddInt32(nv, int32(-value[0]))), nil
default:
return 0, fmt.Errorf("value type of %s is not *atomic.Int64 or *int64 or *atomic.Int32 or *int32", key)
return 0, errors.ErrCacheMemoryInvalidIntValueType.Args(key)
}
}

Expand Down Expand Up @@ -155,7 +155,7 @@ func (r *Memory) Increment(key string, value ...int64) (int64, error) {
case *int32:
return int64(atomic.AddInt32(nv, int32(value[0]))), nil
default:
return 0, fmt.Errorf("value type of %s is not *atomic.Int64 or *int64 or *atomic.Int32 or *int32", key)
return 0, errors.ErrCacheMemoryInvalidIntValueType.Args(key)
}
}

Expand Down
9 changes: 9 additions & 0 deletions cache/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/goravel/framework/cache/console"
contractsconsole "github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/errors"
)

const Binding = "goravel.cache"
Expand All @@ -14,7 +15,15 @@ type ServiceProvider struct {
func (database *ServiceProvider) Register(app foundation.Application) {
app.Singleton(Binding, func(app foundation.Application) (any, error) {
config := app.MakeConfig()
if config == nil {
return nil, errors.ErrConfigFacadeNotSet.SetModule(errors.ModuleCache)
}

log := app.MakeLog()
if log == nil {
return nil, errors.ErrLogFacadeNotSet.SetModule(errors.ModuleCache)
}

store := config.GetString("cache.default")

return NewApplication(config, log, store)
Expand Down
15 changes: 10 additions & 5 deletions errors/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ var (
ErrJSONParserNotSet = New("JSON parser is not initialized")
ErrCacheFacadeNotSet = New("cache facade is not initialized")
ErrOrmFacadeNotSet = New("orm facade is not initialized")
ErrLogFacadeNotSet = New("log facade is not initialized")

ErrCacheSupportRequired = New("cache support is required")
ErrCacheForeverFailed = New("cache forever is failed")

ErrSessionNotFound = New("session [%s] not found", ModuleSession)
ErrSessionDriverIsNotSet = New("driver is not set", ModuleSession)
ErrSessionDriverNotSupported = New("driver [%s] not supported", ModuleSession)
ErrSessionDriverAlreadyExists = New("driver [%s] already exists")
ErrSessionDriverExtensionFailed = New("failed to extend session [%s] driver [%v]", ModuleSession)
ErrSessionNotFound = New("session [%s] not found")
ErrSessionDriverIsNotSet = New("session driver is not set")
ErrSessionDriverNotSupported = New("session driver [%s] not supported")
ErrSessionDriverAlreadyExists = New("session driver [%s] already exists")
ErrSessionDriverExtensionFailed = New("session failed to extend session [%s] driver [%v]")

ErrAuthRefreshTimeExceeded = New("authentication refresh time limit exceeded")
ErrAuthTokenExpired = New("authentication token has expired")
Expand All @@ -24,4 +25,8 @@ var (
ErrAuthInvalidClaims = New("authentication token contains invalid claims")
ErrAuthInvalidToken = New("authentication token is invalid")
ErrAuthInvalidKey = New("authentication key is invalid")

ErrCacheDriverNotSupported = New("invalid driver: %s, only support memory, custom")
ErrCacheStoreContractNotFulfilled = New("%s doesn't implement contracts/cache/store")
ErrCacheMemoryInvalidIntValueType = New("value type of %s is not *atomic.Int64 or *int64 or *atomic.Int32 or *int32")
)
1 change: 1 addition & 0 deletions errors/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package errors
var (
ModuleSession = "session"
ModuleAuth = "auth"
ModuleCache = "cache"
)
9 changes: 0 additions & 9 deletions session/errors.go

This file was deleted.

0 comments on commit e02f2ef

Please sign in to comment.