Added throwOnError() for StreamResults.

This commit is contained in:
Patrick 2024-10-24 18:30:03 +02:00
parent c4e3576bc7
commit 161fdb6c93

View File

@ -469,6 +469,34 @@ inline void throwOnError(mijin::StreamError error, std::string message)
} }
throw std::runtime_error(message + ": " + errorName(error)); throw std::runtime_error(message + ": " + errorName(error));
} }
template<typename TSuccess>
inline decltype(auto) throwOnError(StreamResult<TSuccess>&& result)
{
if (result.isError())
{
throw Exception(errorName(result.getError()));
}
else if (!result.isSuccess())
{
throw Exception("result is empty");
}
return *result;
}
template<typename TSuccess>
inline decltype(auto) throwOnError(StreamResult<TSuccess>&& 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 } // namespace mijin
#endif // !defined(MIJIN_IO_STREAM_HPP_INCLUDED) #endif // !defined(MIJIN_IO_STREAM_HPP_INCLUDED)