From 161fdb6c932ab12ecfa01fd0e93fae124a41b117 Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Thu, 24 Oct 2024 18:30:03 +0200 Subject: [PATCH] Added throwOnError() for StreamResults. --- source/mijin/io/stream.hpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/source/mijin/io/stream.hpp b/source/mijin/io/stream.hpp index 17e8515..f940716 100644 --- a/source/mijin/io/stream.hpp +++ b/source/mijin/io/stream.hpp @@ -469,6 +469,34 @@ inline void throwOnError(mijin::StreamError error, std::string message) } throw std::runtime_error(message + ": " + errorName(error)); } + +template +inline decltype(auto) throwOnError(StreamResult&& result) +{ + if (result.isError()) + { + throw Exception(errorName(result.getError())); + } + else if (!result.isSuccess()) + { + throw Exception("result is empty"); + } + return *result; +} + +template +inline decltype(auto) throwOnError(StreamResult&& result, const std::string& message) +{ + if (result.isError()) + { + throw Exception(message + ": " + errorName(result.getError())); + } + else if (!result.isSuccess()) + { + throw Exception(message + ": result is empty"); + } + return *result; +} } // namespace mijin #endif // !defined(MIJIN_IO_STREAM_HPP_INCLUDED)