MCPcopy Create free account
hub / github.com/bytebase/bytebase / readBytesAndClose

Function readBytesAndClose

backend/plugin/db/elasticsearch/sync.go:390–426  ·  view source on GitHub ↗
(anyResp any)

Source from the content-addressed store, hash-verified

388}
389
390func readBytesAndClose(anyResp any) ([]byte, error) {
391 var body io.ReadCloser
392 var statusCode int
393 var isError bool
394
395 // get body closer and status code.
396 switch resp := anyResp.(type) {
397 case *http.Response:
398 body = resp.Body
399 statusCode = resp.StatusCode
400 isError = resp.StatusCode >= 400
401
402 case *esapi.Response:
403 body = resp.Body
404 statusCode = resp.StatusCode
405 isError = resp.IsError()
406
407 default:
408 return nil, errors.New("not supported response type")
409 }
410
411 // read bytes.
412 bytes, err := io.ReadAll(body)
413 if err != nil {
414 return nil, errors.Wrap(err, "failed to read response body")
415 }
416 if err := body.Close(); err != nil {
417 return nil, errors.Wrap(err, "failed to close response body")
418 }
419
420 // Check for error status codes
421 if isError {
422 return bytes, errors.Errorf("request failed with status code %d: %s", statusCode, string(bytes))
423 }
424
425 return bytes, nil
426}

Callers 2

getHiddenIndicesMethod · 0.85
getIndicesMethod · 0.85

Calls 2

ErrorfMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected