From e29825a034ca3afb179eede90f97fab2e04d5d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Del=20Pino?= <stephane.delpino44@gmail.com> Date: Wed, 6 Mar 2024 19:20:01 +0100 Subject: [PATCH] Improve error message if bad mesh variant is accessed --- src/mesh/MeshVariant.hpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/mesh/MeshVariant.hpp b/src/mesh/MeshVariant.hpp index bf3851a00..7278878a9 100644 --- a/src/mesh/MeshVariant.hpp +++ b/src/mesh/MeshVariant.hpp @@ -1,10 +1,16 @@ #ifndef MESH_VARIANT_HPP #define MESH_VARIANT_HPP +#include <mesh/MeshTraits.hpp> +#include <utils/Demangle.hpp> +#include <utils/Exceptions.hpp> #include <utils/PugsMacros.hpp> +#include <rang.hpp> + #include <iostream> #include <memory> +#include <sstream> #include <variant> class IConnectivity; @@ -38,6 +44,16 @@ class MeshVariant PUGS_INLINE std::shared_ptr<const MeshType> get() const { + if (not std::holds_alternative<std::shared_ptr<const MeshType>>(this->m_p_mesh_variant)) { + std::ostringstream error_msg; + error_msg << "invalid mesh type type\n"; + error_msg << "- required " << rang::fgB::red << demangle<MeshType>() << rang::fg::reset << '\n'; + error_msg << "- contains " << rang::fgB::yellow + << std::visit([](auto&& p_mesh) -> std::string { return demangle<mesh_type_t<decltype(p_mesh)>>(); }, + this->m_p_mesh_variant) + << rang::fg::reset; + throw NormalError(error_msg.str()); + } return std::get<std::shared_ptr<const MeshType>>(m_p_mesh_variant); } -- GitLab