Skip to content

Commit 7108cfa

Browse files
henrybarretogustavosbarreto
authored andcommitted
refactor(ssh,pkg): return single error instead of slice on some methods
1 parent d93dc1f commit 7108cfa

File tree

4 files changed

+44
-56
lines changed

4 files changed

+44
-56
lines changed

pkg/api/internalclient/mocks/internalclient.go

Lines changed: 18 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/internalclient/namespace.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import (
1212
type namespaceAPI interface {
1313
// NamespaceLookup retrieves namespace with the specified tenant.
1414
// It returns the namespace and any encountered errors.
15-
NamespaceLookup(ctx context.Context, tenant string) (*models.Namespace, []error)
15+
NamespaceLookup(ctx context.Context, tenant string) (*models.Namespace, error)
1616
// InviteMember sends an invitation to join the namespace with the specified tenant ID to the
1717
// user with the specified id. The job will use the forwarded host to build the invitation link.
1818
// It returns an error if any and panics if the Client has no worker available.
1919
InviteMember(ctx context.Context, tenantID, userID, forwardedHost string) error
2020
}
2121

22-
func (c *client) NamespaceLookup(ctx context.Context, tenant string) (*models.Namespace, []error) {
22+
func (c *client) NamespaceLookup(ctx context.Context, tenant string) (*models.Namespace, error) {
2323
namespace := new(models.Namespace)
2424
res, err := c.http.
2525
R().
@@ -28,11 +28,11 @@ func (c *client) NamespaceLookup(ctx context.Context, tenant string) (*models.Na
2828
SetResult(namespace).
2929
Get(c.Config.APIBaseURL + "/api/namespaces/{tenant}")
3030
if err != nil {
31-
return nil, []error{err}
31+
return nil, err
3232
}
3333

3434
if res.StatusCode() != http.StatusOK {
35-
return nil, []error{err}
35+
return nil, err
3636
}
3737

3838
return namespace, nil

pkg/api/internalclient/session.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ type sessionAPI interface {
2121

2222
// SessionAsAuthenticated marks a session with the specified uid as authenticated.
2323
// It returns a slice of errors encountered during the operation.
24-
SessionAsAuthenticated(ctx context.Context, uid string) []error
24+
SessionAsAuthenticated(ctx context.Context, uid string) error
2525

2626
// FinishSession finishes the session with the specified uid.
2727
// It returns a slice of errors encountered during the operation.
28-
FinishSession(ctx context.Context, uid string) []error
28+
FinishSession(ctx context.Context, uid string) error
2929

3030
// KeepAliveSession sends a keep-alive signal for the session with the specified uid.
3131
// It returns a slice of errors encountered during the operation.
32-
KeepAliveSession(ctx context.Context, uid string) []error
32+
KeepAliveSession(ctx context.Context, uid string) error
3333

3434
// UpdateSession updates some fields of [models.Session] using [models.SessionUpdate].
3535
UpdateSession(ctx context.Context, uid string, model *models.SessionUpdate) error
@@ -52,8 +52,7 @@ func (c *client) SessionCreate(ctx context.Context, session requests.SessionCrea
5252
return err
5353
}
5454

55-
func (c *client) SessionAsAuthenticated(ctx context.Context, uid string) []error {
56-
var errors []error
55+
func (c *client) SessionAsAuthenticated(ctx context.Context, uid string) error {
5756
_, err := c.http.
5857
R().
5958
SetContext(ctx).
@@ -63,38 +62,36 @@ func (c *client) SessionAsAuthenticated(ctx context.Context, uid string) []error
6362
}).
6463
Patch(c.Config.APIBaseURL + "/internal/sessions/{uid}")
6564
if err != nil {
66-
errors = append(errors, err)
65+
return err
6766
}
6867

69-
return errors
68+
return nil
7069
}
7170

72-
func (c *client) FinishSession(ctx context.Context, uid string) []error {
73-
var errors []error
71+
func (c *client) FinishSession(ctx context.Context, uid string) error {
7472
_, err := c.http.
7573
R().
7674
SetContext(ctx).
7775
SetPathParam("uid", uid).
7876
Post(c.Config.APIBaseURL + "/internal/sessions/{uid}/finish")
7977
if err != nil {
80-
errors = append(errors, err)
78+
return err
8179
}
8280

83-
return errors
81+
return nil
8482
}
8583

86-
func (c *client) KeepAliveSession(ctx context.Context, uid string) []error {
87-
var errors []error
84+
func (c *client) KeepAliveSession(ctx context.Context, uid string) error {
8885
_, err := c.http.
8986
R().
9087
SetContext(ctx).
9188
SetPathParam("uid", uid).
9289
Post(c.Config.APIBaseURL + "/internal/sessions/{uid}/keepalive")
9390
if err != nil {
94-
errors = append(errors, err)
91+
return err
9592
}
9693

97-
return errors
94+
return nil
9895
}
9996

10097
func (c *client) UpdateSession(ctx context.Context, uid string, model *models.SessionUpdate) error {

ssh/session/session.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ func NewSession(ctx gliderssh.Context, tunnel *httptunnel.Tunnel, cache cache.Ca
302302
return nil, err
303303
}
304304

305-
namespace, errs := api.NamespaceLookup(ctx, lookupDevice.TenantID)
306-
if len(errs) > 1 {
307-
return nil, errs[0]
305+
namespace, err := api.NamespaceLookup(ctx, lookupDevice.TenantID)
306+
if err != nil {
307+
return nil, err
308308
}
309309

310310
events, err := api.EventSessionStream(ctx, ctx.SessionID())
@@ -682,10 +682,12 @@ func Event[D any](sess *Session, t string, data []byte, seat int) {
682682
}
683683

684684
func (s *Session) KeepAlive() error {
685-
if errs := s.api.KeepAliveSession(context.TODO(), s.UID); len(errs) > 0 {
686-
log.Error(errs[0])
685+
if err := s.api.KeepAliveSession(context.TODO(), s.UID); err != nil {
686+
log.WithError(err).
687+
WithFields(log.Fields{"session": s.UID, "sshid": s.SSHID}).
688+
Error("Error when trying to keep alive the session")
687689

688-
return errs[0]
690+
return err
689691
}
690692

691693
return nil
@@ -739,12 +741,10 @@ func (s *Session) Finish() (err error) {
739741
}
740742
}
741743

742-
if errs := s.api.FinishSession(context.TODO(), s.UID); len(errs) > 0 {
743-
log.WithError(errs[0]).
744+
if err := s.api.FinishSession(context.TODO(), s.UID); err != nil {
745+
log.WithError(err).
744746
WithFields(log.Fields{"session": s.UID, "sshid": s.SSHID}).
745747
Error("Error when trying to finish the session")
746-
747-
err = errs[0]
748748
}
749749

750750
if envs.IsEnterprise() {

0 commit comments

Comments
 (0)