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(QStringLiteral("error")).toBool(m_isError); | ||||
40 | m_message = object.value(QStringLiteral("message")).toString(m_message); | ||||
41 | m_code = object.value(QStringLiteral("code")).toInt(m_code); | ||||
42 | // Our bugzilla is a bit bugged. It doesn't necessarily set error to true | ||||
43 | // but instead keeps it at null. Because of this we need to possibly shimy | ||||
44 | // the bool to align with reality. | ||||
45 | if (object.value(QStringLiteral("error")).type() == QJsonValue::Null && | ||||
46 | m_code > 0 && | ||||
47 | !m_message.isNull()) { | ||||
48 | m_isError = true; | ||||
49 | } | ||||
50 | } | ||||
51 | | ||||
52 | APIException::APIException(const APIException &other) | ||||
53 | : m_isError(other.m_isError) | ||||
54 | , m_message(other.m_message) | ||||
55 | , m_code(other.m_code) | ||||
56 | { | ||||
57 | } | ||||
58 | | ||||
59 | QString APIException::whatString() const | ||||
60 | { | ||||
61 | return QStringLiteral("[%1] %2").arg(m_code).arg(m_message); | ||||
62 | } | ||||
63 | | ||||
64 | void APIException::maybeThrow(const QJsonDocument &document) | ||||
65 | { | ||||
66 | APIException ex(document); | ||||
67 | | ||||
68 | if (ex.isError()) { | ||||
69 | ex.raise(); | ||||
70 | } | ||||
71 | } | ||||
72 | | ||||
73 | ProtocolException::ProtocolException(const APIJob *job) | ||||
74 | : Exception() | ||||
75 | , m_job(job) | ||||
76 | { | ||||
77 | } | ||||
78 | | ||||
79 | ProtocolException::ProtocolException(const ProtocolException &other) | ||||
80 | : m_job(other.m_job) | ||||
81 | { | ||||
82 | } | ||||
83 | | ||||
84 | QString ProtocolException::whatString() const | ||||
85 | { | ||||
86 | // String generally includes the error code, so no extra logic needed. | ||||
87 | return m_job->errorString(); | ||||
88 | } | ||||
89 | | ||||
90 | void ProtocolException::maybeThrow(const APIJob *job) | ||||
91 | { | ||||
92 | if (job->error() == KJob::NoError) { | ||||
93 | return; | ||||
94 | } | ||||
95 | throw ProtocolException(job); | ||||
96 | } | ||||
97 | | ||||
98 | Exception::~Exception() | ||||
99 | { | ||||
100 | delete m_what; | ||||
101 | } | ||||
102 | | ||||
103 | const char *Exception::what() const noexcept | ||||
104 | { | ||||
105 | strcpy(m_what, qUtf8Printable(whatString())); | ||||
106 | return m_what; | ||||
107 | } | ||||
108 | | ||||
109 | RuntimeException::RuntimeException(const QString &reason) | ||||
110 | : m_reason(reason) | ||||
111 | { | ||||
112 | } | ||||
113 | | ||||
114 | QString RuntimeException::whatString() const | ||||
115 | { | ||||
116 | return m_reason; | ||||
117 | } | ||||
118 | | ||||
119 | } // namespace Bugzilla | ||||
120 | |