Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from datasift/feature/exception_improvements
Browse files Browse the repository at this point in the history
Feature/exception improvements
  • Loading branch information
Christopher Gilbert committed Sep 17, 2014
2 parents 40f224b + 2d7d9e7 commit 8af97d3
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/riemannpp/attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ attribute::set(const std::string& key, const std::string& value) {
int result = riemann_attribute_set(d_attribute.get(),
key.c_str(), value.c_str());
if (-1 == result) {
throw internal_exception();
throw internal_exception(errno, "failed to set attribute for key: " + key + ", with value: " + value);
}
}

void
attribute::set_key(const std::string& key) {
int result = riemann_attribute_set_key(d_attribute.get(), key.c_str());
if (-1 == result) {
throw internal_exception();
throw internal_exception(errno, "failed to set attribute key " + key);
}
}

void
attribute::set_value(const std::string& value) {
int result = riemann_attribute_set_value(d_attribute.get(), value.c_str());
if (-1 == result) {
throw internal_exception();
throw internal_exception(errno, "failed to set attribute value " + value);
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/riemannpp/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ client::client(client_type type, const std::string& host, int port) {
d_client = riemann_client_create(riemann_client_type_t(type),
host.c_str(), port);
if (!d_client) {
throw internal_exception();
throw internal_exception(errno, "failed to create riemann client");
}
}

Expand All @@ -64,23 +64,28 @@ client::connect(client_type type, const std::string& host, int port) {
int result = riemann_client_connect(d_client, riemann_client_type_t(type),
host.c_str(), port);
if (0 != result) {
throw internal_exception();
std::string message;
message += "failed to connect client to ";
message += host;
message += ":";
message += std::to_string(port);
throw internal_exception(errno, message);
}
}

void
client::disconnect() {
int result = riemann_client_disconnect(d_client);
if (0 != result) {
throw internal_exception();
throw internal_exception(errno, "failed to disconnect client");
}
}

void
client::send(message&& m) {
int result = riemann_client_send_message(d_client, m.release());
if (0 != result) {
throw internal_exception();
throw internal_exception(errno, "failed to send message");
}
}

Expand All @@ -99,7 +104,7 @@ void
client::send_oneshot(message&& m) {
int result = riemann_client_send_message_oneshot(d_client, m.release());
if (0 != result) {
throw internal_exception();
throw internal_exception(errno, "failed to send oneshot message");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/riemannpp/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void
event::tag_add(const std::string& tag) {
int result = riemann_event_tag_add(d_event.get(), tag.c_str());
if (0 != result) {
throw internal_exception();
throw internal_exception(errno, "failed to add event tag: " + tag);
}
}

Expand All @@ -75,7 +75,7 @@ void
event::attribute_add(attribute&& a) {
int result = riemann_event_attribute_add(d_event.get(), a.release());
if (0 != result) {
throw internal_exception();
throw internal_exception(errno, "failed to add event attribute");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/riemannpp/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ namespace riemannpp {
void event::set(const event_field field, const T& value) {
int result = riemann_event_set(d_event.get(), field, value, RIEMANN_EVENT_FIELD_NONE);
if (0 != result) {
throw internal_exception();
throw internal_exception(errno, "failed to set event field");
}
}

Expand Down
15 changes: 5 additions & 10 deletions src/riemannpp/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,14 @@ namespace riemannpp {
int d_error;
std::string d_reason;
public:
// Default constructor. Will construct the base exception class with
// the string error deduced by strerror, and store errno.
internal_exception()
: exception(strerror(errno))
, d_error(errno)
, d_reason(strerror(errno)) {}

// Construct an internal exception with the given reason. Behaves the
// same as the default constructor, with the exception being that the
// reason string is overridden by the given parameter.
internal_exception(const std::string& reason)
: exception(strerror(errno))
, d_error(errno)
internal_exception(int error, std::string const & reason)
: exception(
reason + ", error: " + strerror(error)
)
, d_error(error)
, d_reason(reason) {}

// Destructor.
Expand Down
4 changes: 2 additions & 2 deletions src/riemannpp/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void
message::set_event(event& e) {
int result = riemann_message_set_events(d_message.get(), e.release(), nullptr);
if (0 != result) {
throw internal_exception();
throw internal_exception(errno, "failed to set event");
}
}

Expand All @@ -76,7 +76,7 @@ void
message::set_query(query& q) {
int result = riemann_message_set_query(d_message.get(), q.release());
if (0 != result) {
throw internal_exception();
throw internal_exception(errno, "failed to set query");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/riemannpp/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void
query::set_string(const std::string& query) {
int result = riemann_query_set_string(d_query.get(), query.c_str());
if (0 != result) {
throw internal_exception();
throw internal_exception(errno, "failed to set query string: " + query);
}
}

Expand Down

0 comments on commit 8af97d3

Please sign in to comment.