From f35ee5f03838ce3dd59a95f3e11a7008c65ef670 Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Tue, 23 Jul 2024 20:16:24 +0200 Subject: [PATCH] Added throwOnError utility function. --- source/mijin/io/stream.hpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/source/mijin/io/stream.hpp b/source/mijin/io/stream.hpp index 4ee5e36..1b7b0b8 100644 --- a/source/mijin/io/stream.hpp +++ b/source/mijin/io/stream.hpp @@ -258,6 +258,30 @@ StreamError Stream::readAsString(std::basic_string& outString) return StreamError::SUCCESS; } + +inline const char* errorName(StreamError error) noexcept +{ + switch (error) + { + case StreamError::SUCCESS: + return "success"; + case StreamError::IO_ERROR: + return "IO error"; + case StreamError::NOT_SUPPORTED: + return "not supported"; + case StreamError::UNKNOWN_ERROR: + return "unknown error"; + } + return ""; +} + +inline void throwOnError(mijin::StreamError error) +{ + if (error == mijin::StreamError::SUCCESS) { + return; + } + throw std::runtime_error(errorName(error)); +} } // namespace mijin #endif // !defined(MIJIN_IO_STREAM_HPP_INCLUDED)