Re: [PATCH] http: display the response body on POST failure

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2024-05-20 at 21:09:26, Boris Mbarga via GitGitGadget wrote:
> From: elhmn <elhmn@xxxxxxxxxx>
> 
> When Git sends a GET request and receives an HTTP code indicating
> failure (and that failure does not indicate an authentication problem),
> it shows the body of the response, i.e. the error message.
> The same is not true for POST requests. However, it would be good to show
> those error messages e.g. in the case of "429 Too many requests", because
> the user might otherwise be left puzzled about the reason why their clone
> did not work.

I think this is a good idea (and not just because I talked about it with
Boris).  If a user gets an error response, we want them to be able to
receive a helpful response from the server telling them what might have
gone wrong.  While many of us know full well what a 429 response means,
it isn't immediately obvious to many users (judging from my experience
on Stack Overflow), and we might still want to tell users why they got a
429 (too many requests from their IP, network, token, application or
integration, etc.).

> This patch aligns the way POST requests are handled with the GET request
> handling.
> 
> Signed-off-by: elhmn <elhmn@xxxxxxxxxx>

I believe the policy is to put your real name here and in the commit. We
know what it is already from the headers.  You can probably do `git -c
user.name="Boris Mbarga" commit --amend --reset-author --signoff` to fix
that.

> diff --git a/http.c b/http.c
> index 3d80bd6116e..2017e909054 100644
> --- a/http.c
> +++ b/http.c
> @@ -1419,6 +1419,7 @@ struct active_request_slot *get_active_slot(void)
>  		newslot->curl = NULL;
>  		newslot->in_use = 0;
>  		newslot->next = NULL;
> +		newslot->errstr = NULL;
>  
>  		slot = active_queue_head;
>  		if (!slot) {
> diff --git a/http.h b/http.h
> index 3af19a8bf53..cb542c62933 100644
> --- a/http.h
> +++ b/http.h
> @@ -30,6 +30,7 @@ struct active_request_slot {
>  	void *callback_data;
>  	void (*callback_func)(void *data);
>  	struct active_request_slot *next;
> +	struct strbuf *errstr;
>  };
>  
>  struct buffer {
> diff --git a/remote-curl.c b/remote-curl.c
> index 0b6d7815fdd..9b2a41b2451 100644
> --- a/remote-curl.c
> +++ b/remote-curl.c
> @@ -804,8 +804,11 @@ static size_t rpc_in(char *ptr, size_t eltsize,
>  	if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
>  			      &response_code) != CURLE_OK)
>  		return size;
> -	if (response_code >= 300)
> +	if (response_code >= 300) {
> +		strbuf_reset(data->slot->errstr);
> +		strbuf_add(data->slot->errstr, ptr, size);
>  		return size;
> +	}
>  	if (size)
>  		data->rpc->any_written = 1;
>  	if (data->check_pktline)
> @@ -837,6 +840,8 @@ static int run_slot(struct active_request_slot *slot,
>  				strbuf_addch(&msg, ' ');
>  				strbuf_addstr(&msg, curl_errorstr);
>  			}
> +			if (slot->errstr && slot->errstr->len)
> +				error(_("%s"), slot->errstr->buf);
>  		}
>  		error(_("RPC failed; %s"), msg.buf);

I think we should print the "RPC failed" message as well, and first.
That message, even if not super helpful, contains the status code and
some other information, which might be more helpful than what the remote
server actually said.

We might also want to print "remote:" in front of the error string, so
that the user knows it comes from the server instead of Git.  That can
be helpful when searching for it online.
-- 
brian m. carlson (they/them or he/him)
Toronto, Ontario, CA

Attachment: signature.asc
Description: PGP signature


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux