From 361a0bdf6beb3519e81e0bba7fbdc82c03be9513 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andreas=20D=C3=BCring?= Date: Tue, 22 Nov 2016 15:36:51 +0100 Subject: [PATCH] Curl return code handling also add assert for things that should always go right --- doc/shortTermTodo.md | 1 - src/CurlWikiGrabber.cpp | 30 +++++++++++++++++++++++------- src/Makefile | 2 +- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/doc/shortTermTodo.md b/doc/shortTermTodo.md index abee830..e69de29 100644 --- a/doc/shortTermTodo.md +++ b/doc/shortTermTodo.md @@ -1 +0,0 @@ - - curl return code handling diff --git a/src/CurlWikiGrabber.cpp b/src/CurlWikiGrabber.cpp index c35e97c..b915579 100644 --- a/src/CurlWikiGrabber.cpp +++ b/src/CurlWikiGrabber.cpp @@ -1,5 +1,10 @@ #include "CurlWikiGrabber.h" +#ifndef DEBUG +#define NDEBUG +#endif +#include + static size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) { static_cast(userdata)->append(ptr, size * nmemb); @@ -23,18 +28,29 @@ std::string CurlWikiGrabber::grabUrl(std::string url) const throw WalkerException("error initiating curl"); } - curl_easy_setopt(handle, CURLOPT_URL, url.c_str()); - curl_easy_setopt(handle, CURLOPT_USERAGENT, "WikiWalker/ test program"); - curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback); - std::string gotContent; - curl_easy_setopt(handle, CURLOPT_WRITEDATA, &gotContent); + CURLcode crv = curl_easy_setopt(handle, CURLOPT_URL, url.c_str()); + assert(crv == 0); + crv = curl_easy_setopt(handle, CURLOPT_USERAGENT, "WikiWalker/ test program"); + assert(crv == 0); + crv = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback); + assert(crv == 0); + std::string gotContent; + crv = curl_easy_setopt(handle, CURLOPT_WRITEDATA, &gotContent); + assert(crv == 0); gotContent = ""; - curl_easy_perform(handle); + crv = curl_easy_perform(handle); + + if(crv != 0) { + const char* err = curl_easy_strerror(crv); + std::string text(err); + throw WalkerException(text); + } long httpcode = 0; - curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &httpcode); + crv = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &httpcode); + assert(crv == 0); curl_easy_cleanup(handle); diff --git a/src/Makefile b/src/Makefile index 170fe90..71688f2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -26,7 +26,7 @@ clean: distclean: clean rm -f $(LIBOBJ) -debug: CXXFLAGS += -g +debug: CXXFLAGS += -g -DDEBUG debug: $(BIN_OUT_NAME) optimized: CXXFLAGS += -O3 -- 2.11.4.GIT