Changeset View
Changeset View
Standalone View
Standalone View
src/bugzillaintegration/libbugzilla/exceptions.cpp
- 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 | #include "exceptions.h" | ||||
22 | | ||||
23 | #include <QDebug> | ||||
24 | | ||||
25 | #include "apijob.h" | ||||
26 | | ||||
27 | namespace Bugzilla { | ||||
28 | | ||||
29 | APIException::APIException(const QJsonDocument &document) | ||||
30 | : APIException(document.object()) | ||||
31 | { | ||||
32 | } | ||||
33 | | ||||
34 | APIException::APIException(const QJsonObject &object) | ||||
35 | { | ||||
36 | if (object.isEmpty()) { | ||||
37 | return; | ||||
38 | } | ||||
39 | m_isError = object.value("error").toBool(m_isError); | ||||
40 | m_message = object.value("message").toString(m_message); | ||||
41 | m_code = object.value("code").toInt(m_code); | ||||
42 | } | ||||
43 | | ||||
44 | APIException::APIException(const APIException &other) | ||||
45 | : m_isError(other.m_isError) | ||||
46 | , m_message(other.m_message) | ||||
47 | , m_code(other.m_code) | ||||
48 | { | ||||
49 | } | ||||
50 | | ||||
51 | QString APIException::whatString() const | ||||
52 | { | ||||
53 | return QStringLiteral("[%1] %2").arg(m_code).arg(m_message); | ||||
54 | } | ||||
55 | | ||||
56 | void APIException::maybeThrow(const QJsonDocument &document) | ||||
57 | { | ||||
58 | APIException ex(document); | ||||
59 | | ||||
60 | if (ex.isError()) { | ||||
61 | ex.raise(); | ||||
62 | } | ||||
63 | } | ||||
64 | | ||||
65 | ProtocolException::ProtocolException(const APIJob *job) | ||||
66 | : Exception() | ||||
67 | , m_job(job) | ||||
68 | { | ||||
69 | } | ||||
70 | | ||||
71 | ProtocolException::ProtocolException(const ProtocolException &other) | ||||
72 | : m_job(other.m_job) | ||||
73 | { | ||||
74 | } | ||||
75 | | ||||
76 | QString ProtocolException::whatString() const | ||||
77 | { | ||||
78 | // String generally includes the error code, so no extra logic needed. | ||||
79 | return m_job->errorString(); | ||||
80 | } | ||||
81 | | ||||
82 | void ProtocolException::maybeThrow(const APIJob *job) | ||||
83 | { | ||||
84 | if (job->error() == KJob::NoError) { | ||||
85 | return; | ||||
86 | } | ||||
87 | throw ProtocolException(job); | ||||
88 | } | ||||
89 | | ||||
90 | const char *Exception::what() const noexcept | ||||
91 | { | ||||
92 | return qUtf8Printable(whatString()); | ||||
93 | } | ||||
94 | | ||||
95 | RuntimeException::RuntimeException(const QString &reason) | ||||
96 | : m_reason(reason) | ||||
97 | { | ||||
98 | } | ||||
99 | | ||||
100 | QString RuntimeException::whatString() const | ||||
101 | { | ||||
102 | return m_reason; | ||||
103 | } | ||||
104 | | ||||
105 | } // namespace Bugzilla | ||||
106 | |