From 5d0f4b10322e3301b83eacef6dbe10183a003e62 Mon Sep 17 00:00:00 2001 From: Simone Vellei Date: Mon, 20 Nov 2023 17:51:29 +0100 Subject: [PATCH] Fix Handle error message (#3) * chore: update restclientgo v1.1.0 * chore: handle error message --- go.mod | 2 +- go.sum | 4 ++-- qdrantgo.go | 2 +- response/collectionCollectInfo.go | 8 +++++++- response/collectionCreate.go | 8 +++++++- response/collectionDelete.go | 8 +++++++- response/collectionList.go | 8 +++++++- response/collectionUpdate.go | 8 +++++++- response/indexCreate.go | 8 +++++++- response/pointDelete.go | 8 +++++++- response/pointGet.go | 8 +++++++- response/pointSearch.go | 8 +++++++- response/pointUpsert.go | 8 +++++++- response/pointsGet.go | 8 +++++++- response/response.go | 20 ++++++++++++++++---- 15 files changed, 97 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index b133384..b92b6e1 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/henomis/qdrant-go go 1.20 -require github.com/henomis/restclientgo v1.0.5 +require github.com/henomis/restclientgo v1.1.0 diff --git a/go.sum b/go.sum index 828b9bc..236a407 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -github.com/henomis/restclientgo v1.0.5 h1:xMuznJLagE8nGrmFPyBkzsDztJm2A7uMLNGMBY5iWSg= -github.com/henomis/restclientgo v1.0.5/go.mod h1:xIeTCu2ZstvRn0fCukNpzXLN3m/kRTU0i0RwAbv7Zug= +github.com/henomis/restclientgo v1.1.0 h1:qNhBpTwYXuwfy6SL2EWjbfTrlUw70PZYmqQ4jewdu5E= +github.com/henomis/restclientgo v1.1.0/go.mod h1:xIeTCu2ZstvRn0fCukNpzXLN3m/kRTU0i0RwAbv7Zug= diff --git a/qdrantgo.go b/qdrantgo.go index 92a116d..5ad4ec0 100644 --- a/qdrantgo.go +++ b/qdrantgo.go @@ -18,7 +18,7 @@ type Client struct { func New(endpoint, apiKey string) *Client { - restClient := restclientgo.New(endpoint) + restClient := restclientgo.New(endpoint).WithDecodeOnError(true) if len(apiKey) > 0 { restClient.SetRequestModifier(func(req *http.Request) *http.Request { diff --git a/response/collectionCollectInfo.go b/response/collectionCollectInfo.go index 79b80e7..9f25459 100644 --- a/response/collectionCollectInfo.go +++ b/response/collectionCollectInfo.go @@ -73,5 +73,11 @@ func (c *CollectionCollectInfo) AcceptContentType() string { } func (c *CollectionCollectInfo) Decode(body io.Reader) error { - return json.NewDecoder(body).Decode(c) + err := json.NewDecoder(body).Decode(c) + if err != nil { + return err + } + + c.Response.SetStatusMessage() + return nil } diff --git a/response/collectionCreate.go b/response/collectionCreate.go index 40b0c90..880a25e 100644 --- a/response/collectionCreate.go +++ b/response/collectionCreate.go @@ -15,5 +15,11 @@ func (c *CollectionCreate) AcceptContentType() string { } func (c *CollectionCreate) Decode(body io.Reader) error { - return json.NewDecoder(body).Decode(c) + err := json.NewDecoder(body).Decode(c) + if err != nil { + return err + } + + c.Response.SetStatusMessage() + return nil } diff --git a/response/collectionDelete.go b/response/collectionDelete.go index 3caf581..e5b7f33 100644 --- a/response/collectionDelete.go +++ b/response/collectionDelete.go @@ -15,5 +15,11 @@ func (c *CollectionDelete) AcceptContentType() string { } func (c *CollectionDelete) Decode(body io.Reader) error { - return json.NewDecoder(body).Decode(c) + err := json.NewDecoder(body).Decode(c) + if err != nil { + return err + } + + c.Response.SetStatusMessage() + return nil } diff --git a/response/collectionList.go b/response/collectionList.go index 110571e..07e3464 100644 --- a/response/collectionList.go +++ b/response/collectionList.go @@ -23,5 +23,11 @@ func (c *CollectionList) AcceptContentType() string { } func (c *CollectionList) Decode(body io.Reader) error { - return json.NewDecoder(body).Decode(c) + err := json.NewDecoder(body).Decode(c) + if err != nil { + return err + } + + c.Response.SetStatusMessage() + return nil } diff --git a/response/collectionUpdate.go b/response/collectionUpdate.go index 64e9ebc..bf4ab71 100644 --- a/response/collectionUpdate.go +++ b/response/collectionUpdate.go @@ -15,5 +15,11 @@ func (c *CollectionUpdate) AcceptContentType() string { } func (c *CollectionUpdate) Decode(body io.Reader) error { - return json.NewDecoder(body).Decode(c) + err := json.NewDecoder(body).Decode(c) + if err != nil { + return err + } + + c.Response.SetStatusMessage() + return nil } diff --git a/response/indexCreate.go b/response/indexCreate.go index 9548fae..bad5473 100644 --- a/response/indexCreate.go +++ b/response/indexCreate.go @@ -15,5 +15,11 @@ func (c *IndexCreate) AcceptContentType() string { } func (c *IndexCreate) Decode(body io.Reader) error { - return json.NewDecoder(body).Decode(c) + err := json.NewDecoder(body).Decode(c) + if err != nil { + return err + } + + c.Response.SetStatusMessage() + return nil } diff --git a/response/pointDelete.go b/response/pointDelete.go index a55843c..601e730 100644 --- a/response/pointDelete.go +++ b/response/pointDelete.go @@ -15,5 +15,11 @@ func (p *PointDelete) AcceptContentType() string { } func (p *PointDelete) Decode(body io.Reader) error { - return json.NewDecoder(body).Decode(p) + err := json.NewDecoder(body).Decode(p) + if err != nil { + return err + } + + p.Response.SetStatusMessage() + return nil } diff --git a/response/pointGet.go b/response/pointGet.go index 4f733eb..d0f2a92 100644 --- a/response/pointGet.go +++ b/response/pointGet.go @@ -21,5 +21,11 @@ func (p *PointGet) AcceptContentType() string { } func (p *PointGet) Decode(body io.Reader) error { - return json.NewDecoder(body).Decode(p) + err := json.NewDecoder(body).Decode(p) + if err != nil { + return err + } + + p.Response.SetStatusMessage() + return nil } diff --git a/response/pointSearch.go b/response/pointSearch.go index 9be87d2..30d1481 100644 --- a/response/pointSearch.go +++ b/response/pointSearch.go @@ -23,5 +23,11 @@ func (p *PointSearch) AcceptContentType() string { } func (p *PointSearch) Decode(body io.Reader) error { - return json.NewDecoder(body).Decode(p) + err := json.NewDecoder(body).Decode(p) + if err != nil { + return err + } + + p.Response.SetStatusMessage() + return nil } diff --git a/response/pointUpsert.go b/response/pointUpsert.go index 94fadf2..53a73e6 100644 --- a/response/pointUpsert.go +++ b/response/pointUpsert.go @@ -27,5 +27,11 @@ func (p *PointUpsert) AcceptContentType() string { } func (p *PointUpsert) Decode(body io.Reader) error { - return json.NewDecoder(body).Decode(p) + err := json.NewDecoder(body).Decode(p) + if err != nil { + return err + } + + p.Response.SetStatusMessage() + return nil } diff --git a/response/pointsGet.go b/response/pointsGet.go index b48371d..837f936 100644 --- a/response/pointsGet.go +++ b/response/pointsGet.go @@ -21,5 +21,11 @@ func (p *PointsGet) AcceptContentType() string { } func (p *PointsGet) Decode(body io.Reader) error { - return json.NewDecoder(body).Decode(p) + err := json.NewDecoder(body).Decode(p) + if err != nil { + return err + } + + p.Response.SetStatusMessage() + return nil } diff --git a/response/response.go b/response/response.go index f072f0b..9db2f9f 100644 --- a/response/response.go +++ b/response/response.go @@ -7,10 +7,11 @@ import ( ) type Response struct { - Code int `json:"-"` - Time float64 `json:"time"` - Status string `json:"status"` - RawBody *string `json:"-"` + Code int `json:"-"` + Time float64 `json:"time"` + RawStatus any `json:"status"` + Status string `json:"-"` + RawBody *string `json:"-"` } type Detail struct { @@ -43,3 +44,14 @@ func (r *Response) SetBody(body io.Reader) error { func (r *Response) SetHeaders(headers restclientgo.Headers) error { return nil } + +func (r *Response) SetStatusMessage() { + switch status := r.RawStatus.(type) { + case string: + r.Status = status + case map[string]interface{}: + if errorMessage, ok := status["error"].(string); ok { + r.Status = errorMessage + } + } +}