Better handling of non-JSON errors

This commit is contained in:
Ian Gulliver
2023-04-26 10:04:06 -07:00
parent 5231a3dd56
commit 8236a8c115
3 changed files with 16 additions and 4 deletions

View File

@@ -2,9 +2,10 @@ package jsrest
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/go-resty/resty/v2"
)
var (
@@ -138,15 +139,15 @@ func ToJSONError(err error) *JSONError {
return je
}
func ReadError(in []byte) error {
func ReadError(resp *resty.Response) error {
jse := &JSONError{}
err := json.Unmarshal(in, jse)
err := json.Unmarshal(resp.Body(), jse)
if err == nil {
return jse
}
return errors.New(string(in)) //nolint:goerr113
return NewHTTPError(resp.StatusCode())
}
type singleUnwrap interface {