# [PATCH 3/3] Return CURL error message when object transfer fails

6 messages from 2005-09-26 to 2005-09-27. Participants: Nick Hengeveld, Junio C Hamano.
Thread: https://gitlist.dev/t/1951

## Nick Hengeveld, 2005-09-26 17:52

Subject: [PATCH 3/3] Return CURL error message when object transfer fails
Message-ID: <20050926175211.GD9410@reactrix.com>
URL: https://gitlist.dev/e/20050926175211.GD9410%40reactrix.com

```

Return CURL error message when object transfer fails

Signed-off-by: Nick Hengeveld <nickh@reactrix.com>


---

It might be better to extend this to all places that curl_easy_perform
is called, rather than just in fetch_object.

 http-fetch.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

330da7634e6a707779dcc8648841f501d2a47568
diff --git a/http-fetch.c b/http-fetch.c
--- a/http-fetch.c
+++ b/http-fetch.c
@@ -19,6 +19,7 @@
 static CURL *curl;
 static struct curl_slist *no_pragma_header;
 static struct curl_slist *no_range_header;
+static char curl_errorstr[CURL_ERROR_SIZE];
 
 static char *initial_base;
 
@@ -389,6 +390,7 @@ int fetch_object(struct alt_base *repo, 
 	curl_easy_setopt(curl, CURLOPT_FILE, NULL);
 	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
 	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
+	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
 
 	url = xmalloc(strlen(repo->base) + 50);
 	strcpy(url, repo->base);
@@ -448,7 +450,7 @@ int fetch_object(struct alt_base *repo, 
 	curl_result = curl_easy_perform(curl);
 	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_range_header);
 	if (curl_result != 0) {
-		return -1;
+		return error(curl_errorstr);
 	}
 
 	fchmod(local, 0444);

```

## Junio C Hamano, 2005-09-26 21:22

Subject: Re: [PATCH 3/3] Return CURL error message when object transfer fails
Message-ID: <7v4q87ed73.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7v4q87ed73.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <20050926175211.GD9410@reactrix.com>

```
Nick Hengeveld <nickh@reactrix.com> writes:

> It might be better to extend this to all places that curl_easy_perform
> is called, rather than just in fetch_object.

Sounds like a good idea.  Also if you happen to know if this
option is not available in older versions of the library, it
might not hurt to guard it with "#if LIBCURL_VERSION_NUM" like
we do with other options.

```

## Nick Hengeveld, 2005-09-27 00:12

Subject: Re: [PATCH 3/3] Return CURL error message when object transfer fails
Message-ID: <20050927001241.GB15615@reactrix.com>
URL: https://gitlist.dev/e/20050927001241.GB15615%40reactrix.com
In-Reply-To: <7v4q87ed73.fsf@assigned-by-dhcp.cox.net>

```
On Mon, Sep 26, 2005 at 02:22:56PM -0700, Junio C Hamano wrote:

> Sounds like a good idea.  Also if you happen to know if this
> option is not available in older versions of the library, it
> might not hurt to guard it with "#if LIBCURL_VERSION_NUM" like
> we do with other options.

I don't see any indication that CURLOPT_ERRORBUFFER is a new feature.
The curl_easy_strerror() function is new as of 7.12.0 which is why
I elected to use the CURLOPT_ERRORBUFFER option instead.

-- 
For a successful technology, reality must take precedence over public
relations, for nature cannot be fooled.

```

## Junio C Hamano, 2005-09-27 05:51

Subject: Re: [PATCH 3/3] Return CURL error message when object transfer fails
Message-ID: <7v3bnr3vo5.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7v3bnr3vo5.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <20050927001241.GB15615@reactrix.com>

```
Nick Hengeveld <nickh@reactrix.com> writes:

> I don't see any indication that CURLOPT_ERRORBUFFER is a new feature.
> The curl_easy_strerror() function is new as of 7.12.0 which is why
> I elected to use the CURLOPT_ERRORBUFFER option instead.

If that is the case, I'll take your patch verbatim.  Thanks.

I just needed to get the feel that you did things that way,
knowing what to and what not to worry about; I am no cURL
expert.

```

## Junio C Hamano, 2005-09-27 06:13

Subject: Re: [PATCH 3/3] Return CURL error message when object transfer fails
Message-ID: <7vpsqv2g3p.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vpsqv2g3p.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <7v3bnr3vo5.fsf@assigned-by-dhcp.cox.net>

```
Junio C Hamano <junkio@cox.net> writes:

> If that is the case, I'll take your patch verbatim.  Thanks.

Oops.  I spoke too fast.  That "sounds like a good idea" of mine
was a response to your:

> It might be better to extend this to all places that curl_easy_perform
> is called, rather than just in fetch_object.

... so the patch still needed some fixups.  It had minor
dependencies on the previous patches in the series as well, so I
tried to fix them up myself.

Could you take a look at it and see if the following is good
enough, please?

------------
Subject: [PATCH] Return CURL error message when object transfer fails
From: Nick Hengeveld <nickh@reactrix.com>
Date: 1127757131 -0700

Return CURL error message when object transfer fails

[jc: added similar curl_errorstr errors to places where we
 use curl_easy_perform() to run fetch that _must_ succeed.]

Signed-off-by: Nick Hengeveld <nickh@reactrix.com>
Signed-off-by: Junio C Hamano <junkio@cox.net>

---

 http-fetch.c |   24 ++++++++++++++++--------
 1 files changed, 16 insertions(+), 8 deletions(-)

4f1da6322aa5d11091005b23716bcc3c65151a32
diff --git a/http-fetch.c b/http-fetch.c
--- a/http-fetch.c
+++ b/http-fetch.c
@@ -15,6 +15,7 @@
 
 static CURL *curl;
 static struct curl_slist *no_pragma_header;
+static char curl_errorstr[CURL_ERROR_SIZE];
 
 static char *initial_base;
 
@@ -112,10 +113,12 @@ static int fetch_index(struct alt_base *
 	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
 	curl_easy_setopt(curl, CURLOPT_URL, url);
 	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
+	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
 	
 	if (curl_easy_perform(curl)) {
 		fclose(indexfile);
-		return error("Unable to get pack index %s", url);
+		return error("Unable to get pack index %s\n%s", url,
+			     curl_errorstr);
 	}
 
 	fclose(indexfile);
@@ -264,10 +267,10 @@ static int fetch_indices(struct alt_base
 	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
 	curl_easy_setopt(curl, CURLOPT_URL, url);
 	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
+	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
 	
-	if (curl_easy_perform(curl)) {
-		return -1;
-	}
+	if (curl_easy_perform(curl))
+		return error("%s", curl_errorstr);
 
 	while (i < buffer.posn) {
 		switch (data[i]) {
@@ -327,10 +330,12 @@ static int fetch_pack(struct alt_base *r
 	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
 	curl_easy_setopt(curl, CURLOPT_URL, url);
 	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
-	
+	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
+
 	if (curl_easy_perform(curl)) {
 		fclose(packfile);
-		return error("Unable to get pack file %s", url);
+		return error("Unable to get pack file %s\n%s", url,
+			     curl_errorstr);
 	}
 
 	fclose(packfile);
@@ -373,6 +378,7 @@ int fetch_object(struct alt_base *repo, 
 	curl_easy_setopt(curl, CURLOPT_FILE, NULL);
 	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
 	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, no_pragma_header);
+	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
 
 	url = xmalloc(strlen(repo->base) + 50);
 	strcpy(url, repo->base);
@@ -388,7 +394,7 @@ int fetch_object(struct alt_base *repo, 
 
 	if (curl_easy_perform(curl)) {
 		unlink(filename);
-		return -1;
+		return error("%s", curl_errorstr);
 	}
 
 	fchmod(local, 0444);
@@ -453,6 +459,7 @@ int fetch_ref(char *ref, unsigned char *
         curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
 	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
+	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errorstr);
 
         url = xmalloc(strlen(base) + 6 + strlen(ref));
         strcpy(url, base);
@@ -464,7 +471,8 @@ int fetch_ref(char *ref, unsigned char *
         curl_easy_setopt(curl, CURLOPT_URL, url);
 
         if (curl_easy_perform(curl))
-                return error("Couldn't get %s for %s\n", url, ref);
+                return error("Couldn't get %s for %s\n%s",
+			     url, ref, curl_errorstr);
 
         hex[40] = '\0';
         get_sha1_hex(hex, sha1);

```

## Nick Hengeveld, 2005-09-27 16:07

Subject: Re: [PATCH 3/3] Return CURL error message when object transfer fails
Message-ID: <20050927160740.GB1377@reactrix.com>
URL: https://gitlist.dev/e/20050927160740.GB1377%40reactrix.com
In-Reply-To: <7vpsqv2g3p.fsf@assigned-by-dhcp.cox.net>

```
On Mon, Sep 26, 2005 at 11:13:14PM -0700, Junio C Hamano wrote:

> ... so the patch still needed some fixups.  It had minor
> dependencies on the previous patches in the series as well, so I
> tried to fix them up myself.
> 
> Could you take a look at it and see if the following is good
> enough, please?

Looks great, thanks.

> ------------
> Subject: [PATCH] Return CURL error message when object transfer fails
> From: Nick Hengeveld <nickh@reactrix.com>
> Date: 1127757131 -0700
> 
> Return CURL error message when object transfer fails
> 
> [jc: added similar curl_errorstr errors to places where we
>  use curl_easy_perform() to run fetch that _must_ succeed.]
> 
> Signed-off-by: Nick Hengeveld <nickh@reactrix.com>
> Signed-off-by: Junio C Hamano <junkio@cox.net>

-- 
For a successful technology, reality must take precedence over public
relations, for nature cannot be fooled.

```
