Skip to content

Commit 6ff0b6c

Browse files
committed
return body with error
1 parent 8ce01b3 commit 6ff0b6c

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

nzbget.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ const (
1818
DefaultTimeout = 1 * time.Minute
1919
)
2020

21+
// maxBodyError limits the size of the body that gets appended to an error.
22+
// ie. if a bad URL is used, the body content that shows the error gets appended to the error.
23+
// sometimes the body cotent is huge, so we limit it to 1000 charaters.
24+
const maxBodyError = 1000
25+
2126
// Config is the input data needed to return a NZBGet struct.
2227
// This is setup to allow you to easily pass this data in from a config file.
2328
type Config struct {
@@ -102,7 +107,7 @@ func (n *NZBGet) GetInto(method string, output interface{}, args ...interface{})
102107

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

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

125130
return resp, nil
126131
}
132+
133+
func limitBuf(buf *bytes.Buffer) string {
134+
str := buf.String()
135+
if len(str) > maxBodyError {
136+
return str[:maxBodyError]
137+
}
138+
139+
return str
140+
}

0 commit comments

Comments
 (0)