feat(logging); improve messages and errors (#336)

* feat(logger): add module context

* feat(logger): change errors package

* feat(logger): update tests
This commit is contained in:
Ludvig Lundgren 2022-07-05 13:31:44 +02:00 committed by GitHub
parent 95471a4cf7
commit 0e88117702
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
69 changed files with 1172 additions and 957 deletions

View file

@ -2,10 +2,13 @@ package btn
import (
"context"
"io"
"log"
"net/http"
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/jsonrpc"
"golang.org/x/time/rate"
@ -23,6 +26,8 @@ type Client struct {
Ratelimiter *rate.Limiter
APIKey string
Headers http.Header
Log *log.Logger
}
func NewClient(url string, apiKey string) BTNClient {
@ -41,6 +46,10 @@ func NewClient(url string, apiKey string) BTNClient {
Ratelimiter: rate.NewLimiter(rate.Every(150*time.Hour), 1), // 150 rpcRequest every 1 hour
}
if c.Log == nil {
c.Log = log.New(io.Discard, "", log.LstdFlags)
}
return c
}
@ -48,11 +57,11 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
ctx := context.Background()
err := c.Ratelimiter.Wait(ctx) // This is a blocking call. Honors the rate limit
if err != nil {
return nil, err
return nil, errors.Wrap(err, "error waiting for ratelimiter")
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "could not make request")
}
return resp, nil
}