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

@ -3,12 +3,11 @@ package whisparr
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/url"
"path"
"github.com/rs/zerolog/log"
"github.com/autobrr/autobrr/pkg/errors"
)
func (c *client) get(endpoint string) (*http.Response, error) {
@ -18,8 +17,7 @@ func (c *client) get(endpoint string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, reqUrl, http.NoBody)
if err != nil {
log.Error().Err(err).Msgf("whisparr client request error : %v", reqUrl)
return nil, err
return nil, errors.Wrap(err, "could not build request")
}
if c.config.BasicAuth {
@ -31,8 +29,7 @@ func (c *client) get(endpoint string) (*http.Response, error) {
res, err := c.http.Do(req)
if err != nil {
log.Error().Err(err).Msgf("whisparr client request error : %v", reqUrl)
return nil, err
return nil, errors.Wrap(err, "could not make request: %+v", req)
}
if res.StatusCode == http.StatusUnauthorized {
@ -49,14 +46,12 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
jsonData, err := json.Marshal(data)
if err != nil {
log.Error().Err(err).Msgf("whisparr client could not marshal data: %v", reqUrl)
return nil, err
return nil, errors.Wrap(err, "could not marshal data: %+v", data)
}
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(jsonData))
if err != nil {
log.Error().Err(err).Msgf("whisparr client request error: %v", reqUrl)
return nil, err
return nil, errors.Wrap(err, "could not build request")
}
if c.config.BasicAuth {
@ -69,16 +64,13 @@ func (c *client) post(endpoint string, data interface{}) (*http.Response, error)
res, err := c.http.Do(req)
if err != nil {
log.Error().Err(err).Msgf("whisparr client request error: %v", reqUrl)
return nil, err
return nil, errors.Wrap(err, "could not make request: %+v", req)
}
// validate response
if res.StatusCode == http.StatusUnauthorized {
log.Error().Err(err).Msgf("whisparr client bad request: %v", reqUrl)
return nil, errors.New("unauthorized: bad credentials")
} else if res.StatusCode != http.StatusOK {
log.Error().Err(err).Msgf("whisparr client request error: %v", reqUrl)
return nil, errors.New("whisparr: bad request")
}