read on closed response body error #551

Closed
opened 2021-10-29 19:14:19 +00:00 by kamandroid · 4 comments

I want to extract the body of the gitea.Response , however it appears closed
"read on closed response body error"

For example:

  1. data, gitResp, err := client.MigrateRepo(opts)

  2. Receive an 409 Conflict but was unable to get the message of why it failed

  3. If i use the REST endpoints I get a response

{
    "message": "The repository with the same name already exists.",
    "url": "http://gitea-http:3000/api/swagger"
}
  1. Figured i could read the response body i would get that info

Any insight on? Thanks

I want to extract the body of the gitea.Response , however it appears closed "read on closed response body error" For example: 1. `data, gitResp, err := client.MigrateRepo(opts)` 2. Receive an 409 Conflict but was unable to get the message of why it failed 3. If i use the REST endpoints I get a response ``` { "message": "The repository with the same name already exists.", "url": "http://gitea-http:3000/api/swagger" } ``` 4. Figured i could read the response body i would get that info Any insight on? Thanks
Author

I changed the code on my local and able to get the error cause ... rather than the "conflict"

  1. On the client.go file applied a small change on func getResponse
func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, *Response, error) {
	resp, err := c.doRequest(method, path, header, body)
	if err != nil {
		return nil, nil, err
	}
	defer resp.Body.Close()

	// check for errors
	data, err := statusCodeToErr(resp)
	if err != nil {
		return data, resp, errors.New(string(data))
	}
  1. As you can see on the statusCodeToErr , if an err as occurred then the data contains the details of that error
I changed the code on my local and able to get the error cause ... rather than the "conflict" 1. On the client.go file applied a small change on func getResponse ``` func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, *Response, error) { resp, err := c.doRequest(method, path, header, body) if err != nil { return nil, nil, err } defer resp.Body.Close() // check for errors data, err := statusCodeToErr(resp) if err != nil { return data, resp, errors.New(string(data)) } ``` 2. As you can see on the statusCodeToErr , if an err as occurred then the data contains the details of that error
Owner

The resp.body has been consumed in internal. Should we expose that?

The `resp.body` has been consumed in internal. Should we expose that?
lunny added the
kind/proposal
label 2021-10-30 04:13:23 +00:00
Author

two choices (imho)

  1. if you provide a http.Response then the caller assumes they also have the resp.body. so it should be exposed

  2. the other alternative not provide the http.Response ( sdk has abstracted that away ). so now the caller is only provided the data and error

for example:

data, err := client.MigrateRepo(opts)

two choices (imho) 1. if you provide a http.Response then the caller assumes they also have the resp.body. so it should be exposed 2. the other alternative not provide the http.Response ( sdk has abstracted that away ). so now the caller is only provided the data and error for example: data, err := client.MigrateRepo(opts)
Owner

I like the first one.

I like the first one.
6543 closed this issue 2022-04-28 02:56:34 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: gitea/go-sdk#551
No description provided.