Skip to content

Commit

Permalink
The Ultimate exception, thanks to Dax! (#87)
Browse files Browse the repository at this point in the history
* Fixed GAIAPLAT-79 by adding exception

* implemented PR suggestion from Dax

* one more coding improvement from suggestion

* suggested message improvement added

* augmented exception message, just to show it can be done

* yet-another-code-improvement

* trying out Tobin's omission of nullptr in comparisons

* use 'const char*' rather than string in exception constructor
  • Loading branch information
waynelwarren authored May 22, 2020
1 parent c3ad8f1 commit b3fa27d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
32 changes: 17 additions & 15 deletions production/inc/public/common/gaia_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,12 @@ struct gaia_base_t
class edc_invalid_object_type: public gaia_exception
{
public:
edc_invalid_object_type(gaia_type_t expected, gaia_base_t* x_obj, gaia_id_t id,
const char* type_name, gaia_type_t actual) {
edc_invalid_object_type(gaia_id_t id, gaia_type_t expected, const char* expected_type,
gaia_type_t actual, const char* type_name) {
stringstream msg;
msg << "requesting Gaia type " << x_obj->gaia_typename() << "(" << expected << ") but object identified by "
msg << "requesting Gaia type " << expected_type << "(" << expected << ") but object identified by "
<< id << " is type " << type_name << "(" << actual << ")";
m_message = msg.str();
delete x_obj;
}
};

Expand Down Expand Up @@ -303,7 +302,7 @@ struct gaia_object_t : gaia_base_t
{
if (m_copy) {
auto node_ptr = gaia_se_node::open(m_id);
if (nullptr == node_ptr) {
if (!node_ptr) {
throw invalid_node_id(m_id);
}
auto u = T_fb::Pack(*m_fbb, m_copy.get());
Expand All @@ -320,7 +319,7 @@ struct gaia_object_t : gaia_base_t
void delete_row()
{
auto node_ptr = gaia_se_node::open(m_id);
if (nullptr == node_ptr) {
if (!node_ptr) {
throw invalid_node_id(m_id);
}

Expand Down Expand Up @@ -365,7 +364,7 @@ struct gaia_object_t : gaia_base_t
*/
T_obj* copy_write()
{
if (m_copy == nullptr) {
if (!m_copy) {
m_copy.reset(new T_obj());
if (m_fb) {
m_fb->UnPackTo(m_copy.get());
Expand All @@ -379,23 +378,26 @@ struct gaia_object_t : gaia_base_t
static T_gaia* get_object(gaia_ptr<gaia_se_node>& node_ptr)
{
T_gaia* obj = nullptr;
if (node_ptr != nullptr) {
if (node_ptr) {
auto it = s_gaia_cache.find(node_ptr->id);
if (it != s_gaia_cache.end()) {
obj = dynamic_cast<T_gaia *>(it->second);
if (obj == nullptr) {
auto x_obj = new T_gaia(0);

throw edc_invalid_object_type(T_gaia_type, x_obj, node_ptr->id,
((gaia_base_t *)(it->second))->gaia_typename(),
((gaia_base_t *)(it->second))->gaia_type());
if (!obj) {
// The T_gaia object will contain the type name we want for the exception.
T_gaia expected;
gaia_base_t * actual = (gaia_base_t *)(it->second);
throw edc_invalid_object_type(node_ptr->id,
expected.gaia_type(),
expected.gaia_typename(),
actual->gaia_type(),
actual->gaia_typename());
}
}
else {
obj = new T_gaia(node_ptr->id);
s_gaia_cache.insert(pair<gaia_id_t, gaia_base_t *>(node_ptr->id, obj));
}
if (obj->m_fb == nullptr) {
if (!obj->m_fb) {
auto fb = flatbuffers::GetRoot<T_fb>(node_ptr->payload);
obj->m_fb = fb;
obj->m_id = node_ptr->id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ TEST_F(gaia_object_test, read_wrong_type) {
Address::get_row_by_id(eid);
}
catch (const exception& e) {
// The eid is unpredictable, but the exception will use it in its message.
string compare_string = "requesting Gaia type Address(2) but object identified by " + to_string(eid) + " is type Employee(1)";
EXPECT_STREQ(e.what(), compare_string.c_str());
}
Expand Down

0 comments on commit b3fa27d

Please sign in to comment.