1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2025-02-02 15:07:59 -05:00

Added descriptive error messages for discourse

This commit is contained in:
マリウス 2023-01-05 21:19:47 -05:00
parent 0c5be5df5f
commit acfb0ece2a
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F

View File

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strings"
"github.com/hashicorp/go-retryablehttp" "github.com/hashicorp/go-retryablehttp"
) )
@ -145,6 +146,10 @@ func (c *Client) NewRequest(
return req, nil return req, nil
} }
type ErrorBody struct {
Errors []string `json:"errors"`
}
func (c *Client) Do( func (c *Client) Do(
ctx context.Context, ctx context.Context,
req *http.Request, req *http.Request,
@ -180,8 +185,15 @@ func (c *Client) Do(
if res.StatusCode < http.StatusOK || if res.StatusCode < http.StatusOK ||
res.StatusCode > http.StatusNoContent { res.StatusCode > http.StatusNoContent {
var errbody ErrorBody
if err := json.Unmarshal(body, &errbody); err == nil {
return &RequestError{
Err: errors.New(strings.Join(errbody.Errors, "\n")),
}
}
return &RequestError{ return &RequestError{
Err: errors.New("Non-2xx status code"), Err: errors.New(string(body)),
} }
} }