From 6ff0b6c41df588acc1e3aa5dc48bc3cc2c2ee02e Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Thu, 30 Jun 2022 23:44:04 -0700 Subject: [PATCH] return body with error --- nzbget.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/nzbget.go b/nzbget.go index 2d57f2d..4150187 100644 --- a/nzbget.go +++ b/nzbget.go @@ -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 { @@ -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 @@ -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 +}