File indexing completed on 2024-04-28 16:44:07

0001 /*
0002     SPDX-FileCopyrightText: 2019 Harald Sitter <sitter@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef EXCEPTIONS_H
0008 #define EXCEPTIONS_H
0009 
0010 #include <QException>
0011 
0012 class QJsonDocument;
0013 class QJsonObject;
0014 
0015 namespace Bugzilla
0016 {
0017 class APIJob;
0018 
0019 /**
0020  * Root class for exceptions. Simply a QException which has the what backed
0021  * by a QString.
0022  */
0023 class Exception : public QException
0024 {
0025 public:
0026     using QException::QException;
0027     ~Exception() override;
0028 
0029     virtual QString whatString() const = 0;
0030     const char *what() const noexcept override;
0031 
0032 private:
0033     char *m_what = nullptr;
0034 };
0035 
0036 /**
0037  * Generic runtime exception.
0038  */
0039 class RuntimeException : public Exception
0040 {
0041 public:
0042     explicit RuntimeException(const QString &reason);
0043     RuntimeException *clone() const override
0044     {
0045         return new RuntimeException(*this);
0046     }
0047     QString whatString() const override;
0048 
0049 private:
0050     QString m_reason;
0051 };
0052 
0053 /**
0054  * Translates an API error into an exception for easy handling.
0055  * Specifically when the API sends an error object in the body attempting to
0056  * access the JSON blob through one of the convenience accessors
0057  * (e.g. job.object()) will instead raise an exception.
0058  */
0059 class APIException : public Exception
0060 {
0061 public:
0062     explicit APIException(const QJsonDocument &document);
0063     explicit APIException(const QJsonObject &object);
0064     APIException(const APIException &other);
0065 
0066     void raise() const override
0067     {
0068         throw *this;
0069     }
0070     APIException *clone() const override
0071     {
0072         return new APIException(*this);
0073     }
0074     QString whatString() const override;
0075 
0076     bool isError() const
0077     {
0078         return m_isError;
0079     }
0080 
0081     static void maybeThrow(const QJsonDocument &document);
0082 
0083 private:
0084     bool m_isError = false;
0085     QString m_message;
0086     int m_code = -1;
0087 };
0088 
0089 /**
0090  * Translates an KJob/APIJob error into an excpetion for easy handling.
0091  */
0092 class ProtocolException : public Exception
0093 {
0094 public:
0095     explicit ProtocolException(const APIJob *job);
0096     ProtocolException(const ProtocolException &other);
0097 
0098     void raise() const override
0099     {
0100         throw *this;
0101     }
0102     ProtocolException *clone() const override
0103     {
0104         return new ProtocolException(*this);
0105     }
0106     QString whatString() const override;
0107 
0108     static void maybeThrow(const APIJob *job);
0109 
0110 private:
0111     const APIJob *m_job = nullptr;
0112 };
0113 
0114 } // namespace Bugzilla
0115 
0116 #endif // EXCEPTIONS_H