Skip to content

Commit 3797c96

Browse files
committed
Container/MeshNodePtr/at: exception -> std::unexpected
1 parent 533dcde commit 3797c96

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

modules/Container/MeshNetwork.mpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,16 +309,18 @@ export namespace CppUtils::Container
309309
return Branch{*this, key, branches[key]};
310310
}
311311

312-
[[nodiscard]] auto at(const Key& key) const -> Branch<const MeshNodePtr<Key, Value>>
312+
[[nodiscard]] auto at(const Key& key) const -> std::expected<Branch<const MeshNodePtr<Key, Value>>, std::string_view>
313313
{
314+
using namespace std::literals;
315+
314316
if (not m_node)
315-
throw std::runtime_error{"Invalid MeshNodePtr: cannot access branch on a null node"};
317+
return std::unexpected{"Invalid MeshNodePtr: cannot access branch on a null node"sv};
316318

317319
auto sharedLocker = m_node->sharedAccess();
318320
const auto& branches = sharedLocker->value.branches;
319321
if (auto iterator = branches.find(key); iterator != std::cend(branches))
320322
return Branch{*this, key, iterator->second};
321-
throw std::out_of_range{"Key not found in MeshNodePtr branches"};
323+
return std::unexpected{"Key not found in MeshNodePtr branches"sv};
322324
}
323325

324326
[[nodiscard]] auto contains(const Key& key) const -> bool

tests/Container/MeshNetwork.mpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -556,23 +556,24 @@ export namespace CppUtils::UnitTest::Container::MeshNetwork
556556

557557
const auto children = constRoot.at("Branch");
558558

559-
suite.expectEqual(std::size(children), 1uz);
560-
suite.expectEqual(children[0]->getValue().value(), "child");
559+
suite.expect(children.has_value());
560+
suite.expectEqual(std::size(children.value()), 1uz);
561+
suite.expectEqual(children.value()[0].value().getValue().value(), "child");
561562
});
562563

563564
suite.addTest("at(): Non-existent branch", [&] {
564565
const auto root = StringMeshNodePtr::makeRoot("root");
565566

566-
suite.template expectThrow<std::out_of_range>([&] {
567-
auto _ = root.at("NonExistentBranch");
568-
});
567+
auto result = root.at("NonExistentBranch");
568+
suite.expect(not result.has_value());
569+
suite.expectEqual(result.error(), "Key not found in MeshNodePtr branches"sv);
569570
});
570571

571572
suite.addTest("at(): Invalid node", [&] {
572573
const auto invalidNode = StringMeshNodePtr{};
573-
suite.template expectThrow<std::runtime_error>([&] {
574-
auto _ = invalidNode.at("Branch");
575-
});
574+
auto result = invalidNode.at("Branch");
575+
suite.expect(not result.has_value());
576+
suite.expectEqual(result.error(), "Invalid MeshNodePtr: cannot access branch on a null node"sv);
576577
});
577578

578579
suite.addTest("Get first node", [&] {

0 commit comments

Comments
 (0)