Skip to content

Commit 8b341dc

Browse files
committed
Helper method CurlHandle::setopt
Introduced helper method CurlHandle::setopt to automate error checking of curl_easy_setopt call.
1 parent b781c2c commit 8b341dc

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

zipkin/src/zipkin_http_transporter.cc

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,37 @@ static std::string getUrl(const char *collector_host, uint32_t collector_port) {
1313
ZipkinCoreConstants::get().DEFAULT_COLLECTOR_ENDPOINT;
1414
}
1515

16+
template <class... Types> void CurlHandle::setopt(CURLoption option, Types... args) {
17+
auto rcode = curl_easy_setopt(handle_, option, args...);
18+
if (rcode != CURLE_OK) {
19+
throw CurlError{rcode};
20+
}
21+
}
22+
1623
ZipkinHttpTransporter::ZipkinHttpTransporter(const char *collector_host,
1724
uint32_t collector_port,
1825
std::chrono::milliseconds collector_timeout) {
19-
auto rcode = curl_easy_setopt(handle_, CURLOPT_URL,
20-
getUrl(collector_host, collector_port).c_str());
21-
if (rcode != CURLE_OK) {
22-
throw CurlError{rcode};
23-
}
26+
handle_.setopt(CURLOPT_URL, getUrl(collector_host, collector_port).c_str());
2427

2528
headers_.append("Content-Type: application/json");
26-
rcode = curl_easy_setopt(handle_, CURLOPT_HTTPHEADER,
27-
static_cast<curl_slist *>(headers_));
28-
if (rcode != CURLE_OK) {
29-
throw CurlError{rcode};
30-
}
29+
handle_.setopt(CURLOPT_HTTPHEADER, static_cast<curl_slist *>(headers_));
3130

32-
rcode = curl_easy_setopt(handle_, CURLOPT_ERRORBUFFER, error_buffer_);
33-
if (rcode != CURLE_OK) {
34-
throw CurlError{rcode};
35-
}
31+
handle_.setopt(CURLOPT_ERRORBUFFER, error_buffer_);
3632

37-
rcode = curl_easy_setopt(handle_, CURLOPT_TIMEOUT_MS, collector_timeout.count());
38-
if (rcode != CURLE_OK) {
39-
throw CurlError{rcode};
40-
}
33+
handle_.setopt(CURLOPT_TIMEOUT_MS, collector_timeout.count());
4134
}
4235

4336
ZipkinHttpTransporter::~ZipkinHttpTransporter() {}
4437

4538
void ZipkinHttpTransporter::transportSpans(SpanBuffer &spans) try {
4639
auto data = spans.toStringifiedJsonArray();
47-
auto rcode = curl_easy_setopt(handle_, CURLOPT_POSTFIELDS, data.c_str());
48-
if (rcode != CURLE_OK) {
49-
std::cerr << curl_easy_strerror(rcode) << '\n';
40+
try {
41+
handle_.setopt(CURLOPT_POSTFIELDS, data.c_str());
42+
} catch(const CurlError &e) {
43+
std::cerr << e.what() << '\n';
5044
return;
5145
}
52-
rcode = curl_easy_perform(handle_);
46+
auto rcode = curl_easy_perform(handle_);
5347
if (rcode != CURLE_OK) {
5448
std::cerr << error_buffer_ << '\n';
5549
}

zipkin/src/zipkin_http_transporter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class CurlHandle {
4646

4747
CURL *operator->() { return handle_; }
4848

49+
template <class... Types> void setopt(CURLoption option, Types... args);
50+
4951
private:
5052
CURL *handle_;
5153
};

0 commit comments

Comments
 (0)