From 1f9da29669fa5adb5a138227c2625c9e576ca4d1 Mon Sep 17 00:00:00 2001 From: Matteo Bertrone Date: Wed, 30 Jan 2019 14:41:55 +0100 Subject: [PATCH] improve get_port() error message if no port present this commit improves error message when trying to access invalid port, by throwing an exception with a clear message. Signed-off-by: Matteo Bertrone --- src/libs/polycube/include/polycube/services/cube.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libs/polycube/include/polycube/services/cube.h b/src/libs/polycube/include/polycube/services/cube.h index 70fde8191..d79133c20 100644 --- a/src/libs/polycube/include/polycube/services/cube.h +++ b/src/libs/polycube/include/polycube/services/cube.h @@ -328,11 +328,19 @@ void Cube::impl::remove_port(const std::string &port_name) { template std::shared_ptr Cube::impl::get_port(const std::string &port_name) { + if (ports_by_name_.count(port_name) == 0) { + throw std::runtime_error("Port " + port_name + " does not exist"); + } + return ports_by_name_.at(port_name); } template std::shared_ptr Cube::impl::get_port(int index) { + if (ports_by_id_.count(index) == 0) { + throw std::runtime_error("Port id " + std::to_string(index) + " does not exist"); + } + return ports_by_id_.at(index); }