Changeset View
Changeset View
Standalone View
Standalone View
src/bugzillaintegration/libbugzilla/exceptions.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Harald Sitter <sitter@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #ifndef EXCEPTIONS_H | ||||
22 | #define EXCEPTIONS_H | ||||
23 | | ||||
24 | #include <QException> | ||||
25 | | ||||
26 | class QJsonDocument; | ||||
27 | class QJsonObject; | ||||
28 | | ||||
29 | namespace KIO { | ||||
30 | class TransferJob; | ||||
31 | } | ||||
32 | | ||||
33 | namespace Bugzilla { | ||||
34 | | ||||
35 | class APIJob; | ||||
36 | | ||||
37 | /** | ||||
38 | * Root class for exceptions. Simply a QException which has the what backed | ||||
39 | * by a QString. | ||||
40 | */ | ||||
41 | class Exception : public QException | ||||
42 | { | ||||
43 | public: | ||||
44 | using QException::QException; | ||||
45 | | ||||
46 | virtual QString whatString() const = 0; | ||||
47 | virtual const char *what() const noexcept override; | ||||
48 | }; | ||||
49 | | ||||
50 | /** | ||||
51 | * Generic runtime exception. | ||||
52 | */ | ||||
53 | class RuntimeException : public Exception | ||||
54 | { | ||||
55 | public: | ||||
56 | RuntimeException(const QString &reason); | ||||
57 | virtual RuntimeException *clone() const override { return new RuntimeException(*this); } | ||||
58 | virtual QString whatString() const override; | ||||
59 | | ||||
60 | private: | ||||
61 | QString m_reason; | ||||
62 | }; | ||||
63 | | ||||
64 | /** | ||||
65 | * Translates an API error into an excpetion for easy handling. | ||||
66 | * Specifically when the API sends an error object in the body attempting to | ||||
67 | * access the JSON blob through one of the conveience accessors | ||||
68 | * (e.g. job.object()) will instead raise an exception. | ||||
69 | */ | ||||
70 | class APIException : public Exception | ||||
71 | { | ||||
72 | public: | ||||
73 | APIException(const QJsonDocument &document); | ||||
74 | APIException(const QJsonObject &object); | ||||
75 | APIException(const APIException &other); | ||||
76 | | ||||
77 | virtual void raise() const override { throw *this; } | ||||
78 | virtual APIException *clone() const override { return new APIException(*this); } | ||||
79 | virtual QString whatString() const override; | ||||
80 | | ||||
81 | bool isError() const { return m_isError; } | ||||
82 | | ||||
83 | static void maybeThrow(const QJsonDocument &document); | ||||
84 | | ||||
85 | private: | ||||
86 | bool m_isError = false; | ||||
87 | QString m_message; | ||||
88 | int m_code = -1; | ||||
89 | }; | ||||
90 | | ||||
91 | /** | ||||
92 | * Translates an KJob/APIJob error into an excpetion for easy handling. | ||||
93 | */ | ||||
94 | class ProtocolException : public Exception | ||||
95 | { | ||||
96 | public: | ||||
97 | ProtocolException(const APIJob *job); | ||||
98 | ProtocolException(const ProtocolException &other); | ||||
99 | | ||||
100 | virtual void raise() const override { throw *this; } | ||||
101 | virtual ProtocolException *clone() const override { return new ProtocolException(*this); } | ||||
102 | virtual QString whatString() const override; | ||||
103 | | ||||
104 | static void maybeThrow(const APIJob *job); | ||||
105 | | ||||
106 | private: | ||||
107 | const APIJob *m_job = nullptr; | ||||
108 | }; | ||||
109 | | ||||
110 | } // namespace Bugzilla | ||||
111 | | ||||
112 | #endif // EXCEPTIONS_H |