Skip to content

Commit

Permalink
More informative cast error message (#4162)
Browse files Browse the repository at this point in the history
* More informative cast error message

* Use c_str when formatting
  • Loading branch information
lisitsyn committed Feb 9, 2018
1 parent 5993912 commit d1763b8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/shogun/base/SGObject.h
Expand Up @@ -394,10 +394,11 @@ class CSGObject
{
return any_cast<T>(value);
}
catch (const std::logic_error& exc)
catch (const TypeMismatchException& exc)
{
SG_ERROR(
"Get \"%s\" failed: %s.\n", _tag.name().c_str(), exc.what());
"Get \"%s\" failed. Expected %s, got %s.\n",
exc.expected().c_str(), exc.actual().c_str());
}
// we won't be there
return any_cast<T>(value);
Expand Down
41 changes: 32 additions & 9 deletions src/shogun/lib/any.h
Expand Up @@ -81,6 +81,32 @@ namespace shogun
template <class T>
class SGMatrix;

class TypeMismatchException : public std::exception
{
public:
TypeMismatchException(
const std::string& expected, const std::string& actual)
: m_expected(expected), m_actual(actual)
{
}
TypeMismatchException(const TypeMismatchException& other)
: m_expected(other.m_expected), m_actual(other.m_actual)
{
}
std::string expected() const
{
return m_expected;
}
std::string actual() const
{
return m_actual;
}

private:
std::string m_expected;
std::string m_actual;
};

template <class T, class S>
class ArrayReference
{
Expand Down Expand Up @@ -671,9 +697,8 @@ namespace shogun
}
if (!policy->matches(other.policy))
{
throw std::logic_error(
"Bad assign into " + policy->type() + " from " +
other.policy->type());
throw TypeMismatchException(
other.policy->type(), policy->type());
}
policy->clear(&storage);
if (other.policy->policy_type() == PolicyType::NON_OWNING)
Expand All @@ -692,9 +717,8 @@ namespace shogun
{
if (!policy->matches(other.policy))
{
throw std::logic_error(
"Can't clone into " + policy->type() + " from " +
other.policy->type());
throw TypeMismatchException(
other.policy->type(), policy->type());
}
policy->clone(&storage, other.storage);
return *(this);
Expand Down Expand Up @@ -732,9 +756,8 @@ namespace shogun
}
else
{
throw std::logic_error(
"Bad cast to " + demangled_type<T>() + " but the type is " +
policy->type());
throw TypeMismatchException(
demangled_type<T>(), policy->type());
}
}

Expand Down
12 changes: 6 additions & 6 deletions tests/unit/lib/Any_unittest.cc
Expand Up @@ -81,7 +81,7 @@ TEST(Any, as)
int32_t integer = 10;
auto any = make_any(integer);
EXPECT_EQ(any.as<int32_t>(), integer);
EXPECT_THROW(any.as<float64_t>(), std::logic_error);
EXPECT_THROW(any.as<float64_t>(), TypeMismatchException);
}

TEST(Any, same_type)
Expand Down Expand Up @@ -131,8 +131,8 @@ TEST(Any, any_cast)
auto any = make_any(integer);
auto empty_any = Any();
EXPECT_EQ(any_cast<int32_t>(any), integer);
EXPECT_THROW(any_cast<float64_t>(any), std::logic_error);
EXPECT_THROW(any_cast<int32_t>(empty_any), std::logic_error);
EXPECT_THROW(any_cast<float64_t>(any), TypeMismatchException);
EXPECT_THROW(any_cast<int32_t>(empty_any), TypeMismatchException);
}

TEST(Any, make_any_ref)
Expand Down Expand Up @@ -201,14 +201,14 @@ TEST(Any, assign_wrong_type_into_owning)
{
int32_t integer = 10;
auto any = make_any(integer);
EXPECT_THROW(any = make_any(3.14), std::logic_error);
EXPECT_THROW(any = make_any(3.14), TypeMismatchException);
}

TEST(Any, assign_wrong_type_into_non_owning)
{
int32_t integer = 10;
auto any = make_any_ref(&integer);
EXPECT_THROW(any = make_any(3.14), std::logic_error);
EXPECT_THROW(any = make_any(3.14), TypeMismatchException);
}

TEST(Any, compare_owning_and_non_owning)
Expand Down Expand Up @@ -360,7 +360,7 @@ TEST(Any, clone_wrong_type)
Simple* a = nullptr;
int other = 5;
auto any = make_any_ref(&a);
EXPECT_THROW(any.clone_from(make_any(other)), std::logic_error);
EXPECT_THROW(any.clone_from(make_any(other)), TypeMismatchException);
}

TEST(Any, clone_into_owning_via_copy)
Expand Down

0 comments on commit d1763b8

Please sign in to comment.