Skip to content

Commit

Permalink
Include nested path in 'OutOfBounds' error message
Browse files Browse the repository at this point in the history
  • Loading branch information
jedelbo committed Mar 18, 2024
1 parent 218df45 commit 7f67e63
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Enhancements
* <New feature description> (PR [#????](https://github.com/realm/realm-core/pull/????))
* None.
* Nested path included in 'OutOfBoundsø error message ([#7438](https://github.com/realm/realm-core/issues/7438))

### Fixed
* <How do the end-user experience this issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
Expand Down
9 changes: 9 additions & 0 deletions src/realm/collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,13 @@ UpdateStatus CollectionBase::do_init_from_parent(BPlusTreeBase* tree, ref_type r
return UpdateStatus::Updated;
}

void CollectionBase::out_of_bounds(const char* msg, size_t index, size_t size) const
{
auto path = get_short_path();
path.erase(path.begin());
throw OutOfBounds(util::format("%1 on %2 '%3.%4%5'", msg, collection_type_name(get_collection_type()),
get_table()->get_class_name(), get_property_name(), path),
index, size);
}

} // namespace realm
18 changes: 7 additions & 11 deletions src/realm/collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,13 @@ class CollectionBase : public Collection {
CollectionBase& operator=(const CollectionBase&) noexcept = default;
CollectionBase& operator=(CollectionBase&&) noexcept = default;

void validate_index(const char* msg, size_t index, size_t size) const;
void validate_index(const char* msg, size_t index, size_t size) const
{
if (index >= size) {
out_of_bounds(msg, index, size);
}
}
void out_of_bounds(const char* msg, size_t index, size_t size) const;
static UpdateStatus do_init_from_parent(BPlusTreeBase* tree, ref_type ref, bool allow_create);
};

Expand All @@ -277,16 +283,6 @@ inline std::string_view collection_type_name(CollectionType col_type, bool upper
return "";
}

inline void CollectionBase::validate_index(const char* msg, size_t index, size_t size) const
{
if (index >= size) {
throw OutOfBounds(util::format("%1 on %2 '%3.%4'", msg, collection_type_name(get_collection_type()),
get_table()->get_class_name(), get_property_name()),
index, size);
}
}


template <class T>
inline void check_column_type(ColKey col)
{
Expand Down
3 changes: 3 additions & 0 deletions test/test_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,9 @@ TEST(List_NestedList_Path)
CHECK_EQUAL(path.path_from_top[0], col_child);
CHECK_EQUAL(path.path_from_top[1], "Any");
CHECK_EQUAL(path.path_from_top[2], "Foo");
std::string message;
CHECK_THROW_ANY_GET_MESSAGE(list_int->set(7, 0), message);
CHECK(message.find("Any['Foo']") != std::string::npos);
}

// Collections contained in Mixed
Expand Down

0 comments on commit 7f67e63

Please sign in to comment.