provide also error details in exception and output v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 15 Mar 2022 23:43:02 +0100
branchv_0
changeset 11 6b913e82f52a
parent 10 479557122717
child 12 2f2d64333867
provide also error details in exception and output
src/HTTPClient.cpp
src/HTTPClient.h
src/HTTPHandler.h
--- a/src/HTTPClient.cpp	Tue Mar 15 23:29:36 2022 +0100
+++ b/src/HTTPClient.cpp	Tue Mar 15 23:43:02 2022 +0100
@@ -30,6 +30,7 @@
 class HTTPClient::HTTPClientImpl {
 public:
 	CURL* curl;
+	char curlErrorBuffer[CURL_ERROR_SIZE];
 	std::stringstream responseBody;
 	std::stringstream responseHeaders;
 
@@ -109,6 +110,9 @@
 		return r;
 	});
 
+	// set the error buffer
+	curl_easy_setopt(impl->curl, CURLOPT_ERRORBUFFER, impl->curlErrorBuffer);
+
 	return new HTTPClient(impl);
 }
 
@@ -130,6 +134,9 @@
 	for (size_t i = 0; i < request.headers.size(); i += 2) requestHeders.append(request.headers[i] + ": " + request.headers[i + 1]); // TODO: validate, no CR/LF...
 	curl_easy_setopt(impl->curl, CURLOPT_HTTPHEADER, requestHeders.getList());
 
+	// clear the error buffer
+	impl->curlErrorBuffer[0] = 0;
+
 	// do HTTP call
 	CURLcode result = curl_easy_perform(impl->curl);
 
@@ -143,7 +150,7 @@
 
 		return response;
 	} else {
-		throw Exception(curl_easy_strerror(result));
+		throw Exception(curl_easy_strerror(result), impl->curlErrorBuffer);
 	}
 }
 
--- a/src/HTTPClient.h	Tue Mar 15 23:29:36 2022 +0100
+++ b/src/HTTPClient.h	Tue Mar 15 23:43:02 2022 +0100
@@ -68,14 +68,26 @@
 	};
 
 	class Exception : public std::runtime_error {
+	private:
+		std::string details;
 	public:
 
+		Exception(const std::string& message, const std::string& details) : runtime_error(message), details(details) {
+		}
+
 		Exception(const std::string& message) : runtime_error(message) {
 		}
 
 		Exception(const char* message) : runtime_error(message) {
 		}
 
+		std::string getDetails() const {
+			return details;
+		}
+		
+		std::string getFullMessage() const {
+			return std::string(what()) + ": " + details;
+		}
 	};
 
 	virtual ~HTTPClient();
--- a/src/HTTPHandler.h	Tue Mar 15 23:29:36 2022 +0100
+++ b/src/HTTPHandler.h	Tue Mar 15 23:43:02 2022 +0100
@@ -162,7 +162,7 @@
 				responseHeaders.push_back(convertor.from_bytes(response.headers[i + 1]));
 			}
 		} catch (const HTTPClient::Exception& e) {
-			body = e.what();
+			body = e.getFullMessage();
 			// TODO: move error message into separate attribute?
 		}