From f5bea15501ec6d3e3e02839ff0672dd68f23c1dc Mon Sep 17 00:00:00 2001 From: Ivan-lis Date: Mon, 25 Nov 2024 13:20:18 +0000 Subject: [PATCH] Added unknown stream error handling and fixed clang errors --- library/include/larra/serialization/auto.hpp | 2 +- library/include/larra/stream_error.hpp | 29 ++++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/library/include/larra/serialization/auto.hpp b/library/include/larra/serialization/auto.hpp index b469dae..79486eb 100644 --- a/library/include/larra/serialization/auto.hpp +++ b/library/include/larra/serialization/auto.hpp @@ -115,7 +115,7 @@ struct Config : V { requires(!HasParse) : V(AttributeConfig{}) { } - constexpr auto Base() const -> const V& { + [[nodiscard]] constexpr auto Base() const -> const V& { return static_cast(*this); } using type = T; diff --git a/library/include/larra/stream_error.hpp b/library/include/larra/stream_error.hpp index 4a6615c..ca908aa 100644 --- a/library/include/larra/stream_error.hpp +++ b/library/include/larra/stream_error.hpp @@ -73,6 +73,25 @@ struct ErrorImpl : BaseError { } }; +// Helper class to prevent parsing response stream into an expected return type if its name is a 'stream:error' +struct UnknownXmppError : BaseError { + static constexpr auto kDefaultName = "stream:error"; + static constexpr std::string_view kErrorMessage = "Unknown XMPP stream error"; + + static constexpr auto TryParse(xmlpp::Element* element) { + return std::optional{UnknownXmppError{}}; + } + static constexpr auto Parse(xmlpp::Element* element) { + return TryParse(element).value(); + } + friend constexpr auto operator<<(xmlpp::Element* element, const UnknownXmppError& obj) -> void { + throw std::format("'{}' must never be written into the real stream!", kErrorMessage); + } + [[nodiscard]] constexpr auto what() const noexcept -> const char* override { + return kErrorMessage.data(); + } +}; + struct BadFormat : ErrorImpl {}; struct BadNamespacePrefix : ErrorImpl {}; struct Conflict : ErrorImpl {}; @@ -81,7 +100,7 @@ struct HostGone : ErrorImpl {}; struct HostUnknown : ErrorImpl {}; struct ImproperAdressing : ErrorImpl {}; struct InternalServerError : ErrorImpl {}; -struct InvalidForm : ErrorImpl {}; +struct InvalidFrom : ErrorImpl {}; struct InvalidNamespace : ErrorImpl {}; struct InvalidXml : ErrorImpl {}; struct NotAuthorized : ErrorImpl {}; @@ -109,7 +128,7 @@ using StreamError = std::variant; + error::stream::UnsupportedVersion, + error::stream::UnknownXmppError>; + +static_assert(!std::is_same_v - 1, StreamError>, StreamError>, + "'UnknownXmppError' must be at the end of 'StreamError' variant"); } // namespace larra::xmpp