Skip to content

Commit 689648a

Browse files
henrybarretogustavosbarreto
authored andcommitted
refactor(pkg): simplify error checking on internal http client
1 parent fe2fa0d commit 689648a

File tree

7 files changed

+34
-52
lines changed

7 files changed

+34
-52
lines changed

pkg/api/internalclient/billing.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (c *client) BillingReport(ctx context.Context, tenant string, action string
2525
SetQueryParam("action", action).
2626
Post(c.config.EnterpriseBaseURL + "/internal/billing/report")
2727

28-
return NewError(res, err)
28+
return HasError(res, err)
2929
}
3030

3131
func (c *client) BillingEvaluate(ctx context.Context, tenantID string) (*models.BillingEvaluation, error) {
@@ -37,8 +37,8 @@ func (c *client) BillingEvaluate(ctx context.Context, tenantID string) (*models.
3737
SetHeader("X-Tenant-ID", tenantID).
3838
SetResult(&eval).
3939
Post(c.config.EnterpriseBaseURL + "/internal/billing/evaluate")
40-
if HasError(resp, err) {
41-
return nil, NewError(resp, err)
40+
if err := HasError(resp, err); err != nil {
41+
return nil, err
4242
}
4343

4444
return eval, nil

pkg/api/internalclient/device.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (c *client) DevicesOffline(ctx context.Context, uid string) error {
3838
SetPathParam("uid", uid).
3939
Post(c.config.APIBaseURL + "/internal/devices/{uid}/offline")
4040

41-
return NewError(res, err)
41+
return HasError(res, err)
4242
}
4343

4444
func (c *client) DevicesHeartbeat(ctx context.Context, uid string) error {
@@ -56,8 +56,8 @@ func (c *client) Lookup(ctx context.Context, lookup map[string]string) (string,
5656
SetQueryParams(lookup).
5757
SetResult(&device).
5858
Get(c.config.APIBaseURL + "/internal/lookup")
59-
if HasError(resp, err) {
60-
return "", NewError(resp, err)
59+
if err := HasError(resp, err); err != nil {
60+
return "", err
6161
}
6262

6363
return device.UID, nil
@@ -73,8 +73,8 @@ func (c *client) DeviceLookup(ctx context.Context, tenantID, name string) (*mode
7373
SetQueryParam("name", name).
7474
SetResult(&device).
7575
Get(c.config.APIBaseURL + "/internal/device/lookup")
76-
if HasError(resp, err) {
77-
return nil, NewError(resp, err)
76+
if err := HasError(resp, err); err != nil {
77+
return nil, err
7878
}
7979

8080
return device, nil
@@ -88,8 +88,8 @@ func (c *client) ListDevices(ctx context.Context) ([]models.Device, error) {
8888
SetContext(ctx).
8989
SetResult(&list).
9090
Get(c.config.APIBaseURL + "/api/devices")
91-
if HasError(resp, err) {
92-
return nil, NewError(resp, err)
91+
if err := HasError(resp, err); err != nil {
92+
return nil, err
9393
}
9494

9595
return list, nil
@@ -102,8 +102,8 @@ func (c *client) GetDevice(ctx context.Context, uid string) (*models.Device, err
102102
SetContext(ctx).
103103
SetResult(&device).
104104
Get(c.config.APIBaseURL + "/api/devices/{uid}")
105-
if HasError(resp, err) {
106-
return nil, NewError(resp, err)
105+
if err := HasError(resp, err); err != nil {
106+
return nil, err
107107
}
108108

109109
return device, nil
@@ -129,8 +129,8 @@ func (c *client) LookupWebEndpoints(ctx context.Context, address string) (*WebEn
129129
SetPathParam("address", address).
130130
SetResult(&endpoint).
131131
Get(c.config.EnterpriseBaseURL + "/internal/web-endpoints/{address}")
132-
if HasError(resp, err) {
133-
return nil, NewError(resp, err)
132+
if err := HasError(resp, err); err != nil {
133+
return nil, err
134134
}
135135

136136
return endpoint, nil

pkg/api/internalclient/errors.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,8 @@ func (e *Error) Error() string {
2020
return fmt.Sprintf("code=%d, message=%s", e.Code, e.Message)
2121
}
2222

23-
// NewError creates a new error based on the response from the Resty HTTP client.
24-
// If the response indicates an error (status code >= 400), it returns a custom Error
25-
// containing the status code and message. If there was an error during the request,
26-
// it joins that error with a predefined ErrRequestFailed. If there are no errors,
27-
// it returns nil.
28-
func NewError(resp *resty.Response, err error) error {
23+
// HasError checks if there was an error in the HTTP response or if the provided error is not nil.
24+
func HasError(resp *resty.Response, err error) error {
2925
if err != nil {
3026
return errors.Join(ErrRequestFailed, err)
3127
}
@@ -38,17 +34,3 @@ func NewError(resp *resty.Response, err error) error {
3834

3935
return nil
4036
}
41-
42-
// HasError checks if there was an error in the HTTP response or if the provided error is not nil.
43-
// It returns true if there was an error, otherwise false.
44-
func HasError(resp *resty.Response, err error) bool {
45-
if err != nil {
46-
return true
47-
}
48-
49-
if resp.IsError() {
50-
return true
51-
}
52-
53-
return false
54-
}

pkg/api/internalclient/firewall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ func (c *client) FirewallEvaluate(ctx context.Context, lookup map[string]string)
1818
SetQueryParams(lookup).
1919
Get(c.config.EnterpriseBaseURL + "/internal/firewall/rules/evaluate")
2020

21-
return NewError(resp, err)
21+
return HasError(resp, err)
2222
}

pkg/api/internalclient/namespace.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func (c *client) NamespaceLookup(ctx context.Context, tenant string) (*models.Na
2727
SetPathParam("tenant", tenant).
2828
SetResult(namespace).
2929
Get(c.config.APIBaseURL + "/api/namespaces/{tenant}")
30-
if HasError(resp, err) {
31-
return nil, NewError(resp, err)
30+
if err := HasError(resp, err); err != nil {
31+
return nil, err
3232
}
3333

3434
return namespace, nil

pkg/api/internalclient/session.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (c *client) SessionCreate(ctx context.Context, session requests.SessionCrea
4949
SetBody(session).
5050
Post(c.config.APIBaseURL + "/internal/sessions")
5151

52-
return NewError(resp, err)
52+
return HasError(resp, err)
5353
}
5454

5555
func (c *client) SessionAsAuthenticated(ctx context.Context, uid string) error {
@@ -62,7 +62,7 @@ func (c *client) SessionAsAuthenticated(ctx context.Context, uid string) error {
6262
}).
6363
Patch(c.config.APIBaseURL + "/internal/sessions/{uid}")
6464

65-
return NewError(resp, err)
65+
return HasError(resp, err)
6666
}
6767

6868
func (c *client) FinishSession(ctx context.Context, uid string) error {
@@ -72,7 +72,7 @@ func (c *client) FinishSession(ctx context.Context, uid string) error {
7272
SetPathParam("uid", uid).
7373
Post(c.config.APIBaseURL + "/internal/sessions/{uid}/finish")
7474

75-
return NewError(resp, err)
75+
return HasError(resp, err)
7676
}
7777

7878
func (c *client) KeepAliveSession(ctx context.Context, uid string) error {
@@ -82,7 +82,7 @@ func (c *client) KeepAliveSession(ctx context.Context, uid string) error {
8282
SetPathParam("uid", uid).
8383
Post(c.config.APIBaseURL + "/internal/sessions/{uid}/keepalive")
8484

85-
return NewError(resp, err)
85+
return HasError(resp, err)
8686
}
8787

8888
func (c *client) UpdateSession(ctx context.Context, uid string, model *models.SessionUpdate) error {
@@ -95,7 +95,7 @@ func (c *client) UpdateSession(ctx context.Context, uid string, model *models.Se
9595
SetBody(model).
9696
Patch(c.config.APIBaseURL + "/internal/sessions/{tenant}")
9797

98-
return NewError(res, err)
98+
return HasError(res, err)
9999
}
100100

101101
func (c *client) EventSessionStream(ctx context.Context, uid string) (*websocket.Conn, error) {
@@ -113,7 +113,7 @@ func (c *client) EventSessionStream(ctx context.Context, uid string) (*websocket
113113
nil,
114114
)
115115
if err != nil {
116-
return nil, NewError(nil, err)
116+
return nil, HasError(nil, err)
117117
}
118118

119119
return connection, nil
@@ -128,8 +128,8 @@ func (c *client) SaveSession(ctx context.Context, uid string, seat int) error {
128128
"seat": strconv.Itoa(seat),
129129
}).
130130
Post(c.config.EnterpriseBaseURL + "/internal/sessions/{uid}/records/{seat}")
131-
if HasError(resp, err) {
132-
return NewError(resp, err)
131+
if err := HasError(resp, err); err != nil {
132+
return err
133133
}
134134

135135
if resp.StatusCode() == http.StatusNotAcceptable {
@@ -143,5 +143,5 @@ func (c *client) SaveSession(ctx context.Context, uid string, seat int) error {
143143
return nil
144144
}
145145

146-
return NewError(resp, err)
146+
return HasError(resp, err)
147147
}

pkg/api/internalclient/sshkey.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func (c *client) GetPublicKey(ctx context.Context, fingerprint, tenant string) (
3030
}).
3131
SetResult(&pubKey).
3232
Get(c.config.APIBaseURL + "/internal/sshkeys/public-keys/{fingerprint}/{tenant}")
33-
if HasError(resp, err) {
34-
return nil, NewError(resp, err)
33+
if err := HasError(resp, err); err != nil {
34+
return nil, err
3535
}
3636

3737
return pubKey, nil
@@ -50,8 +50,8 @@ func (c *client) EvaluateKey(ctx context.Context, fingerprint string, dev *model
5050
SetBody(dev).
5151
SetResult(&evaluate).
5252
Post(c.config.APIBaseURL + "/internal/sshkeys/public-keys/evaluate/{fingerprint}/{username}")
53-
if HasError(resp, err) {
54-
return false, NewError(resp, err)
53+
if err := HasError(resp, err); err != nil {
54+
return false, err
5555
}
5656

5757
return *evaluate, nil
@@ -65,8 +65,8 @@ func (c *client) CreatePrivateKey(ctx context.Context) (*models.PrivateKey, erro
6565
SetContext(ctx).
6666
SetResult(&privKey).
6767
Post(c.config.APIBaseURL + "/internal/sshkeys/private-keys")
68-
if HasError(resp, err) {
69-
return nil, NewError(resp, err)
68+
if err := HasError(resp, err); err != nil {
69+
return nil, err
7070
}
7171

7272
return privKey, nil

0 commit comments

Comments
 (0)