I use curl in a C++ project to upload a file to a service. Since we use our internal company framework classes for reading files we define a READFUNCTION for that. The internal function can throw an exception so I wanted to catch that.
My first thought was to put a try-catch around curl_easy_perform
but I am not sure if that will work.
Instead I read how curl does error handling and I found the ERRORBUFFER. But how to I access it?
// inside some other function
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, (char*) curlErrorBuffer);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_file);
// the READFUNCTION
size_t read_data_from_file(char* buffer, size_t size, size_t nitems, File* file) {
size_t nread = 0;
try {
nread = file->read(buffer, size * nitems);
}
catch (ReadError &e) {
nread = CURL_READFUNC_ABORT;
// How to write to ERRORBUFFER?
// strncpy(ERRORBUFFER, "Failed to read file, " + e.msg, BUF_SIZE);
}
return nread;
}
Is there any way to access the ERRORBUFFER without making it global? Would using try-catch around curl_easy_perform
work too?