@@ -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
0 commit comments