mirror of
https://github.com/lightninglabs/lightning-terminal.git
synced 2026-08-13 12:33:36 +02:00
session: use error variables
In preparation for having the unit tests pass against a different Store implementation, we standardize some of the errors that get returned.
This commit is contained in:
parent
aa8080da24
commit
5ed5fd25fa
4 changed files with 39 additions and 16 deletions
|
|
@ -866,7 +866,9 @@ func testSessionLinking(net *NetworkHarness, t *harnessTest) {
|
|||
LinkedGroupId: sessResp.Session.GroupId,
|
||||
},
|
||||
)
|
||||
require.ErrorContains(t.t, err, "is still active")
|
||||
require.ErrorContains(
|
||||
t.t, err, session.ErrSessionsInGroupStillActive.Error(),
|
||||
)
|
||||
|
||||
// Revoke the previous one and repeat.
|
||||
_, err = litAutopilotClient.RevokeAutopilotSession(
|
||||
|
|
|
|||
|
|
@ -6,4 +6,16 @@ var (
|
|||
// ErrSessionNotFound is an error returned when we attempt to retrieve
|
||||
// information about a session but it is not found.
|
||||
ErrSessionNotFound = errors.New("session not found")
|
||||
|
||||
// ErrUnknownGroup is returned when an attempt is made to insert a
|
||||
// session and link it to an existing group where the group is not
|
||||
// known.
|
||||
ErrUnknownGroup = errors.New("unknown group")
|
||||
|
||||
// ErrSessionsInGroupStillActive is returned when an attempt is made to
|
||||
// insert a session and link it to a group that still has other active
|
||||
// sessions.
|
||||
ErrSessionsInGroupStillActive = errors.New(
|
||||
"group has active sessions",
|
||||
)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -229,8 +229,9 @@ func (db *BoltStore) NewSession(label string, typ Type, expiry time.Time,
|
|||
if session.ID != session.GroupID {
|
||||
_, err = getKeyForID(sessionBucket, session.GroupID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unknown linked session "+
|
||||
"%x: %w", session.GroupID, err)
|
||||
return fmt.Errorf("%w: unknown linked "+
|
||||
"session %x: %w", ErrUnknownGroup,
|
||||
session.GroupID, err)
|
||||
}
|
||||
|
||||
// Fetch all the session IDs for this group. This will
|
||||
|
|
@ -242,18 +243,22 @@ func (db *BoltStore) NewSession(label string, typ Type, expiry time.Time,
|
|||
return err
|
||||
}
|
||||
|
||||
// Ensure that the all the linked sessions are no longer
|
||||
// active.
|
||||
for _, id := range sessionIDs {
|
||||
sess, err := getSessionByID(sessionBucket, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Ensure that the session is no longer active.
|
||||
if !sess.State.Terminal() {
|
||||
return fmt.Errorf("session (id=%x) "+
|
||||
"in group %x is still active",
|
||||
sess.ID, sess.GroupID)
|
||||
if sess.State.Terminal() {
|
||||
continue
|
||||
}
|
||||
|
||||
return fmt.Errorf("%w: session (id=%x) in "+
|
||||
"group %x is still active",
|
||||
ErrSessionsInGroupStillActive, sess.ID,
|
||||
sess.GroupID)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -630,14 +635,14 @@ func (db *BoltStore) GetGroupID(sessionID ID) (ID, error) {
|
|||
|
||||
sessionIDBkt := idIndex.Bucket(sessionID[:])
|
||||
if sessionIDBkt == nil {
|
||||
return fmt.Errorf("no index entry for session ID: %x",
|
||||
sessionID)
|
||||
return fmt.Errorf("%w: no index entry for session "+
|
||||
"ID: %x", ErrUnknownGroup, sessionID)
|
||||
}
|
||||
|
||||
groupIDBytes := sessionIDBkt.Get(groupIDKey)
|
||||
if len(groupIDBytes) == 0 {
|
||||
return fmt.Errorf("group ID not found for session "+
|
||||
"ID %x", sessionID)
|
||||
return fmt.Errorf("%w: group ID not found for "+
|
||||
"session ID %x", ErrUnknownGroup, sessionID)
|
||||
}
|
||||
|
||||
copy(groupID[:], groupIDBytes)
|
||||
|
|
@ -806,7 +811,7 @@ func addIDToGroupIDPair(sessionBkt *bbolt.Bucket, id, groupID ID) error {
|
|||
func getSessionByID(bucket *bbolt.Bucket, id ID) (*Session, error) {
|
||||
keyBytes, err := getKeyForID(bucket, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("%w: %w", ErrSessionNotFound, err)
|
||||
}
|
||||
|
||||
v := bucket.Get(keyBytes)
|
||||
|
|
|
|||
|
|
@ -22,6 +22,10 @@ func TestBasicSessionStore(t *testing.T) {
|
|||
_ = db.Close()
|
||||
})
|
||||
|
||||
// Try fetch a session that doesn't exist yet.
|
||||
_, err = db.GetSessionByID(ID{1, 3, 4, 4})
|
||||
require.ErrorIs(t, err, ErrSessionNotFound)
|
||||
|
||||
// Reserve a session. This should succeed.
|
||||
s1, err := reserveSession(db, "session 1")
|
||||
require.NoError(t, err)
|
||||
|
|
@ -183,7 +187,7 @@ func TestBasicSessionStore(t *testing.T) {
|
|||
require.Empty(t, sessions)
|
||||
|
||||
_, err = db.GetGroupID(s4.ID)
|
||||
require.ErrorContains(t, err, "no index entry")
|
||||
require.ErrorIs(t, err, ErrUnknownGroup)
|
||||
|
||||
// Only session 1 should remain in this group.
|
||||
sessIDs, err = db.GetSessionIDs(s4.GroupID)
|
||||
|
|
@ -211,7 +215,7 @@ func TestLinkingSessions(t *testing.T) {
|
|||
_, err = reserveSession(
|
||||
db, "session 2", withLinkedGroupID(&groupID),
|
||||
)
|
||||
require.ErrorContains(t, err, "unknown linked session")
|
||||
require.ErrorIs(t, err, ErrUnknownGroup)
|
||||
|
||||
// Create a new session with no previous link.
|
||||
s1 := createSession(t, db, "session 1")
|
||||
|
|
@ -220,7 +224,7 @@ func TestLinkingSessions(t *testing.T) {
|
|||
// session. This should fail due to the first session still being
|
||||
// active.
|
||||
_, err = reserveSession(db, "session 2", withLinkedGroupID(&s1.GroupID))
|
||||
require.ErrorContains(t, err, "is still active")
|
||||
require.ErrorIs(t, err, ErrSessionsInGroupStillActive)
|
||||
|
||||
// Revoke the first session.
|
||||
require.NoError(t, db.ShiftState(s1.ID, StateRevoked))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue