Skip to content

Commit 12f1c8d

Browse files
authored
fix: Unmarshal resp if possible (#148)
* handle unmarshalerr first * return query Error if possible * rename the wrap * trigger ci * fix typo * fix typo * again * fix test
1 parent 2f404a0 commit 12f1c8d

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

client.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,21 @@ func (c *APIClient) doRequest(ctx context.Context, method, path string, req inte
273273
return errors.Wrap(ErrReadResponse, err.Error())
274274
}
275275

276+
// unmarshal response body to retrieve the possible error code & message before handing
277+
// http status err.
278+
var unmarshalErr error
279+
if resp != nil {
280+
contentType := httpResp.Header.Get("Content-Type")
281+
if strings.HasPrefix(contentType, "application/json") {
282+
if unmarshalErr = json.Unmarshal(httpRespBody, &resp); unmarshalErr != nil {
283+
unmarshalErr = errors.Wrap(unmarshalErr, "failed to unmarshal response body")
284+
}
285+
}
286+
}
287+
if respHeaders != nil {
288+
*respHeaders = httpResp.Header
289+
}
290+
276291
if httpResp.StatusCode == http.StatusUnauthorized {
277292
if c.authMethod() == AuthMethodAccessToken && i < maxRetries {
278293
// retry with a rotated access token
@@ -288,18 +303,7 @@ func (c *APIClient) doRequest(ctx context.Context, method, path string, req inte
288303
return NewAPIError("unexpected HTTP StatusCode", httpResp.StatusCode, httpRespBody)
289304
}
290305

291-
if resp != nil {
292-
contentType := httpResp.Header.Get("Content-Type")
293-
if strings.HasPrefix(contentType, "application/json") {
294-
if err := json.Unmarshal(httpRespBody, &resp); err != nil {
295-
return errors.Wrap(err, "failed to unmarshal response body")
296-
}
297-
}
298-
}
299-
if respHeaders != nil {
300-
*respHeaders = httpResp.Header
301-
}
302-
return nil
306+
return unmarshalErr
303307
}
304308
return errors.Errorf("failed to do request after %d retries", maxRetries)
305309
}
@@ -507,9 +511,6 @@ func (c *APIClient) startQueryRequest(ctx context.Context, request *QueryRequest
507511
return c.doRequest(ctx, "POST", path, request, c.NeedSticky(), &resp, &respHeaders)
508512
}, Query,
509513
)
510-
if err != nil {
511-
return nil, errors.Wrap(err, "failed to do query request")
512-
}
513514

514515
if len(resp.NodeID) != 0 {
515516
c.NodeID = resp.NodeID
@@ -522,6 +523,11 @@ func (c *APIClient) startQueryRequest(ctx context.Context, request *QueryRequest
522523
if len(respHeaders) > 0 && len(respHeaders.Get(DatabendRouteHintHeader)) > 0 {
523524
c.routeHint = respHeaders.Get(DatabendRouteHintHeader)
524525
}
526+
if resp.Error != nil {
527+
return nil, errors.Wrap(resp.Error, "query error")
528+
} else if err != nil {
529+
return nil, errors.Wrap(err, "failed to do query request")
530+
}
525531
return &resp, nil
526532
}
527533

@@ -550,7 +556,9 @@ func (c *APIClient) PollQuery(ctx context.Context, nextURI string) (*QueryRespon
550556
// e.g. transaction state need to be updated if commit fail
551557
c.applySessionState(&result)
552558
c.trackStats(&result)
553-
if err != nil {
559+
if result.Error != nil {
560+
return nil, errors.Wrap(result.Error, "query error")
561+
} else if err != nil {
554562
return nil, errors.Wrap(err, "failed to query page")
555563
}
556564
return &result, nil

errors.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package godatabend
22

33
import (
4-
"fmt"
5-
64
"github.com/pkg/errors"
75
)
86

@@ -11,14 +9,3 @@ var (
119
ErrNoLastInsertID = errors.New("no LastInsertId available")
1210
ErrNoRowsAffected = errors.New("no RowsAffected available")
1311
)
14-
15-
// Error contains parsed information about server error
16-
type Error struct {
17-
Code int
18-
Message string
19-
}
20-
21-
// Error implements the interface error
22-
func (e *Error) Error() string {
23-
return fmt.Sprintf("Code: %d, Message: %s", e.Code, e.Message)
24-
}

0 commit comments

Comments
 (0)