Reputation: 337
Is it possible get error code (like errno
) or error description (like curl_easy_strerror
) when curl_easy_init
fails?
Upvotes: 2
Views: 3873
Reputation: 3032
No. It is not possible to get the reason. From the source code for version 7.58.0 of the library ...
struct Curl_easy *curl_easy_init(void) {
CURLcode result;
struct Curl_easy *data;
// Etc..
result = Curl_open(&data);
if (result) {
DEBUGF(fprintf(stderr, "Error: Curl_open failed\n"));
return NULL;
}
return data;
}
As you can see, if Curl_open(...)
fails, the library just outputs an error and aborts: it doesn't modify any state variables such as errno
which you can later examine to determine the cause of the failure.
However, if the call is failing for you, it may be because curl_global_init()
is failing. curl_easy_init()
calls this automatically if you don't call it yourself beforehand. curl_global_init()
- unlike
curl_easy_init()
- actually does return an error code.
Moral of the story ...
Call curl_global_init(...)
and check its return value before calling curl_easy_init()
. Don't rely on curl_easy_init()
doing it automatically.
This will at least allow you to discern whether curl_global_init(...)
or Curl_open()
is failing.
E.g.
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if (res != 0) {
fprintf("Failed global init ...\n");
exit(1);
}
CURL *curl = curl_easy_init();
if (!curl) {
///
}
// Etc.
curl_easy_cleanup(curl);
curl_global_cleanup();
Upvotes: 6