From b113ae300efe5dc17f148a06df09062546bf54ee Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Tue, 6 Sep 2022 20:05:34 +0200 Subject: Add QXmppError holding a description and std::any --- src/CMakeLists.txt | 2 ++ src/base/QXmppError.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ src/base/QXmppError.h | 51 +++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 src/base/QXmppError.cpp create mode 100644 src/base/QXmppError.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4164bd71..6723e081 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -37,6 +37,7 @@ set(INSTALL_HEADER_FILES base/QXmppE2eeMetadata.h base/QXmppElement.h base/QXmppEntityTimeIq.h + base/QXmppError.h base/QXmppExtension.h base/QXmppFutureUtils_p.h base/QXmppFileMetadata.h @@ -158,6 +159,7 @@ set(SOURCE_FILES base/QXmppDiscoveryIq.cpp base/QXmppElement.cpp base/QXmppEntityTimeIq.cpp + base/QXmppError.cpp base/QXmppFileMetadata.cpp base/QXmppGeolocItem.cpp base/QXmppGlobal.cpp diff --git a/src/base/QXmppError.cpp b/src/base/QXmppError.cpp new file mode 100644 index 00000000..80578c0d --- /dev/null +++ b/src/base/QXmppError.cpp @@ -0,0 +1,75 @@ +// SPDX-FileCopyrightText: 2022 Linus Jahn +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#include "QXmppError.h" + +#include "QXmppStanza.h" + +#include +#include + +/// +/// \class QXmppError +/// +/// Generic error class holding a description and a more specific error object. The specific error +/// usually is something like a QXmppStanza::Error or an enum. +/// +/// \since QXmpp 1.5 +/// + +/// +/// \var QXmppError::description +/// +/// Human readable description of the error. +/// + +/// +/// \var QXmppError::error +/// +/// More specific details on the error. It may be of any type. Functions returning QXmppError +/// should tell you which types are used. +/// + +/// +/// Returns whether the error is a QNetworkReply::NetworkError. +/// +bool QXmppError::isFileError() const +{ + return holdsType(); +} + +/// +/// Returns whether the error is a QNetworkReply::NetworkError. +/// +bool QXmppError::isNetworkError() const +{ + return holdsType(); +} + +/// +/// Returns whether the error is a QXmppStanza::Error. +/// +bool QXmppError::isStanzaError() const +{ + return holdsType(); +} + +/// +/// \fn QXmppError::holdsType() +/// +/// Returns true if the error is of type T. +/// + +/// +/// \fn QXmppError::value() +/// +/// Copies the value if it has type T, returns empty optional otherwise. +/// + +/// +/// \fn QXmppError::takeValue() +/// +/// Moves out the value if it has type T, leaves the stored error intact and returns an empty +/// optional otherwise. +/// diff --git a/src/base/QXmppError.h b/src/base/QXmppError.h new file mode 100644 index 00000000..a646b6e5 --- /dev/null +++ b/src/base/QXmppError.h @@ -0,0 +1,51 @@ +// SPDX-FileCopyrightText: 2022 Linus Jahn +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#ifndef QXMPPERROR_H +#define QXMPPERROR_H + +#include "QXmppGlobal.h" + +#include +#include + +struct QXMPP_EXPORT QXmppError +{ + QString description; + std::any error; + + bool isFileError() const; + bool isNetworkError() const; + bool isStanzaError() const; + + template + bool holdsType() const + { + return error.type().hash_code() == typeid(T).hash_code(); + } + template + std::optional value() const + { + // any_cast always checks this, to avoid an additional check we use exceptions + try { + return std::any_cast(error); + } catch (std::bad_any_cast) { + return {}; + } + } + template + std::optional takeValue() + { + // we can't use unchecked any_cast with moving because we can't access the error after a + // failed any_cast + if (error.type().hash_code() == typeid(T).hash_code()) { + auto value = std::any_cast(std::move(error)); + error = std::any(); + return value; + } + return {}; + } +}; + +#endif // QXMPPERROR_H -- cgit v1.2.3