Skip to content

Commit

Permalink
feat: handle warning error from api requests
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Dec 3, 2024
1 parent 0ed026c commit 1adaf63
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 4 additions & 0 deletions internal/apis/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package apis

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"

"github.com/gleich/lumber/v3"
)

var WarningError = errors.New("Warning error when trying to make request. Ignore error.")

// sends a given http.Request and will unmarshal the JSON from the response body and return that as the given type.
func SendRequest[T any](req *http.Request) (T, error) {
var zeroValue T // to be used as "nil" when returning errors
Expand All @@ -34,6 +37,7 @@ func SendRequest[T any](req *http.Request) (T, error) {
resp.StatusCode == http.StatusGatewayTimeout ||
resp.StatusCode == http.StatusInternalServerError {
lumber.Warning(err)
return zeroValue, WarningError
} else {
lumber.Error(err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"
"time"

"github.com/gleich/lcp-v2/internal/apis"
"github.com/gleich/lcp-v2/internal/metrics"
"github.com/gleich/lcp-v2/internal/secrets"
"github.com/gleich/lumber/v3"
Expand Down Expand Up @@ -100,7 +101,7 @@ func (c *Cache[T]) StartPeriodicUpdate(updateFunc func() (T, error), interval ti
defer ticker.Stop()
for range ticker.C {
data, err := updateFunc()
if err != nil {
if err != nil && err != apis.WarningError {
lumber.Error(err, "updating", c.name, "cache failed")
continue
}
Expand Down

0 comments on commit 1adaf63

Please sign in to comment.