read on closed response body error #551

Closed
opened 7 months ago 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
Poster

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 7 months ago
Poster

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 referenced this issue from a commit 3 weeks ago
6543 closed this issue 3 weeks ago
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date

No due date set.

Dependencies

No dependencies set.

Loading…
There is no content yet.