From b3fa27d60ab6c89b978be521828623aab149c170 Mon Sep 17 00:00:00 2001 From: Wayne Warren Date: Fri, 22 May 2020 10:06:55 -0700 Subject: [PATCH] The Ultimate exception, thanks to Dax! (#87) * 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 --- production/inc/public/common/gaia_object.hpp | 32 ++++++++++--------- .../direct_access/test_direct_access.cpp | 1 + 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/production/inc/public/common/gaia_object.hpp b/production/inc/public/common/gaia_object.hpp index 17ac174c9569..9e7f55138286 100644 --- a/production/inc/public/common/gaia_object.hpp +++ b/production/inc/public/common/gaia_object.hpp @@ -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; } }; @@ -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()); @@ -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); } @@ -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()); @@ -379,23 +378,26 @@ struct gaia_object_t : gaia_base_t static T_gaia* get_object(gaia_ptr& 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(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(node_ptr->id, obj)); } - if (obj->m_fb == nullptr) { + if (!obj->m_fb) { auto fb = flatbuffers::GetRoot(node_ptr->payload); obj->m_fb = fb; obj->m_id = node_ptr->id; diff --git a/production/tests/common/direct_access/test_direct_access.cpp b/production/tests/common/direct_access/test_direct_access.cpp index 0ed599e2ac92..d77a6396c92d 100644 --- a/production/tests/common/direct_access/test_direct_access.cpp +++ b/production/tests/common/direct_access/test_direct_access.cpp @@ -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()); }