diff --git a/main.cpp b/main.cpp index 71a1e12..4feee29 100644 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,9 @@ +/* Документация cURL: +CURLOPT_FAILONERROR: +https://curl.se/libcurl/c/CURLOPT_FAILONERROR.html +https://curl.se/libcurl/c/libcurl-errors.html#CURLEHTTPRETURNEDERROR +*/ + #include #include #include "histogram.h" @@ -57,13 +63,23 @@ download(const string& address) { curl_easy_setopt(curl, CURLOPT_URL, address.c_str()); + // -----вар 17 + curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); //Превращает HTTP-ошибки (статусы 4xx и 5xx) в ошибки cURL + //------- + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { - cerr << "cURL error: " << curl_easy_strerror(res) << endl; + // -----вар 17 + if (res == CURLE_HTTP_RETURNED_ERROR) { + cerr << "HTTP Error: Server returned 4xx/5xx status code for URL: " << address << endl; //Точечно обрабатываем случай, когда сервер вернул ошибку + } else { + cerr << "cURL error (" << res << "): " << curl_easy_strerror(res) << endl; + } + //-------- curl_easy_cleanup(curl); exit(1); }