Skip to content

Commit

Permalink
fix(hass): 🐛 actually retrieve and return response errors from HA
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Sep 5, 2024
1 parent 9dbd503 commit f15d77e
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions internal/hass/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ func send[T any](ctx context.Context, client *Client, requestDetails any) (T, er
response T
responseErr sensor.APIError
responseObj *resty.Response
err error
)

if client.endpoint == nil {
Expand All @@ -354,20 +353,15 @@ func send[T any](ctx context.Context, client *Client, requestDetails any) (T, er
slog.String("body", string(req.RequestBody())),
slog.Time("sent_at", time.Now()))

responseObj, err = requestObj.SetBody(req.RequestBody()).Post("")
responseObj, _ = requestObj.SetBody(req.RequestBody()).Post("") //nolint:errcheck // error is checked with responseObj.IsError()
case GetRequest:
client.logger.
LogAttrs(ctx, logging.LevelTrace,
"Sending request.",
slog.String("method", "GET"),
slog.Time("sent_at", time.Now()))

responseObj, err = requestObj.Get("")
}

// If the client fails to send the request, return a wrapped error.
if err != nil {
return response, &sensor.APIError{Code: responseObj.StatusCode(), Message: responseObj.Status()}
responseObj, _ = requestObj.Get("") //nolint:errcheck // error is checked with responseObj.IsError()
}

client.logger.
Expand All @@ -380,7 +374,7 @@ func send[T any](ctx context.Context, client *Client, requestDetails any) (T, er
slog.String("body", string(responseObj.Body())))

if responseObj.IsError() {
return response, &responseErr
return response, &sensor.APIError{Code: responseObj.StatusCode(), Message: responseObj.Status()}
}

return response, nil
Expand Down

0 comments on commit f15d77e

Please sign in to comment.