noctx icon indicating copy to clipboard operation
noctx copied to clipboard

False positive on returning a Request?

Open lazyfrosch opened this issue 4 years ago • 0 comments

I'm always interested how to improve my code, but this one is a bit irritating to me. Maybe I'm wrong in doing so, but it seems like detection is too strict here.

In my API client, I have a helper function to abstract creating the basic request. Usually I then add headers or other stuff afterwards, before executing the request.

func (a *Api) NewApiRequest(method string, path string, body io.Reader) (req *http.Request, err error) {
	req, err = http.NewRequestWithContext(a.Context, method, a.BaseURL+path, body)
	if err != nil {
		return
	}

	req.SetBasicAuth(a.Username, a.Password)

	return
}

Now when using the function, noctx complains:

	req, err := a.NewApiRequest("POST", "/upload", nil) // should rewrite http.NewRequestWithContext or add (*Request).WithContext (noctx)
	if err != nil {
		return fmt.Errorf("could not create request: %w", err)
	}

    // Adding more to the request

	data, err := a.RequestApi(req, http.StatusCreated)
	if err != nil {
		return fmt.Errorf("could not create upload: %w", err)
	}

What do you think about it? For now I just ignored noctx here.

lazyfrosch avatar Nov 04 '20 16:11 lazyfrosch