From bcb9cecec2c1fc21a06cd9d2f112fe4681baaa57 Mon Sep 17 00:00:00 2001 From: KozlovDanD Date: Wed, 11 Jun 2025 11:56:56 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B2:17?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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); }