Skip to content

Commit

Permalink
return body with error
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnewhall committed Jul 1, 2022
1 parent 8ce01b3 commit 6ff0b6c
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion nzbget.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const (
DefaultTimeout = 1 * time.Minute
)

// maxBodyError limits the size of the body that gets appended to an error.
// ie. if a bad URL is used, the body content that shows the error gets appended to the error.
// sometimes the body cotent is huge, so we limit it to 1000 charaters.
const maxBodyError = 1000

// Config is the input data needed to return a NZBGet struct.
// This is setup to allow you to easily pass this data in from a config file.
type Config struct {
Expand Down Expand Up @@ -102,7 +107,7 @@ func (n *NZBGet) GetInto(method string, output interface{}, args ...interface{})

tee := io.TeeReader(resp.Body, &buf)
if err := json.DecodeClientResponse(tee, &output); err != nil {
return buf.Len(), fmt.Errorf("parsing response: %w", err)
return buf.Len(), fmt.Errorf("parsing response: %w: %s", err, limitBuf(&buf))
}

return buf.Len(), nil
Expand All @@ -124,3 +129,12 @@ func (c *client) Do(req *http.Request) (*http.Response, error) {

return resp, nil
}

func limitBuf(buf *bytes.Buffer) string {
str := buf.String()
if len(str) > maxBodyError {
return str[:maxBodyError]
}

return str
}

0 comments on commit 6ff0b6c

Please sign in to comment.