mirror of
https://github.com/nais/wonderwall.git
synced 2026-02-14 17:49:54 +00:00
refactor(session/store): consolidate session errors and use multi-error wrapping
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user