refactor(session/store): consolidate session errors and use multi-error wrapping

This commit is contained in:
Trong Huu Nguyen
2023-02-21 10:06:29 +01:00
parent 17f39f8c5f
commit db391a9e44
4 changed files with 4 additions and 9 deletions

View File

@@ -2,7 +2,6 @@ package session
import (
"context"
"errors"
"fmt"
"time"
@@ -11,10 +10,6 @@ import (
"github.com/nais/wonderwall/pkg/config"
)
var (
ErrKeyNotFound = errors.New("key not found")
)
type Store interface {
Write(ctx context.Context, key string, value *EncryptedData, expiration time.Duration) error
Read(ctx context.Context, key string) (*EncryptedData, error)

View File

@@ -26,7 +26,7 @@ func (s *memorySessionStore) Read(_ context.Context, key string) (*EncryptedData
data, ok := s.sessions[key]
if !ok {
return nil, fmt.Errorf("%w: no such session: %s", ErrKeyNotFound, key)
return nil, fmt.Errorf("%w: no such session: %s", ErrNotFound, key)
}
return data, nil

View File

@@ -33,7 +33,7 @@ func (s *redisSessionStore) Read(ctx context.Context, key string) (*EncryptedDat
}
if errors.Is(err, redis.Nil) {
return nil, fmt.Errorf("%w: %s", ErrKeyNotFound, err.Error())
return nil, fmt.Errorf("%w: %w", ErrNotFound, err)
}
return nil, err
@@ -59,7 +59,7 @@ func (s *redisSessionStore) Delete(ctx context.Context, keys ...string) error {
}
if errors.Is(err, redis.Nil) {
return fmt.Errorf("%w: %s", ErrKeyNotFound, err.Error())
return fmt.Errorf("%w: %w", ErrNotFound, err)
}
return err

View File

@@ -87,6 +87,6 @@ func del(t *testing.T, store session.Store, key string) {
result, err := store.Read(context.Background(), key)
assert.Error(t, err)
assert.ErrorIs(t, err, session.ErrKeyNotFound)
assert.ErrorIs(t, err, session.ErrNotFound)
assert.Nil(t, result)
}