exceptions.cpp 3.31 KB
Newer Older
Harald Sitter's avatar
Harald Sitter committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
    Copyright 2019 Harald Sitter <sitter@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) version 3, or any
    later version accepted by the membership of KDE e.V. (or its
    successor approved by the membership of KDE e.V.), which shall
    act as a proxy defined in Section 6 of version 3 of the license.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "exceptions.h"

#include "apijob.h"
Harald Sitter's avatar
Harald Sitter committed
24
#include "bugzilla_debug.h"
Harald Sitter's avatar
Harald Sitter committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

namespace Bugzilla {

APIException::APIException(const QJsonDocument &document)
    : APIException(document.object())
{
}

APIException::APIException(const QJsonObject &object)
{
    if (object.isEmpty()) {
        return;
    }
    m_isError = object.value(QStringLiteral("error")).toBool(m_isError);
    m_message = object.value(QStringLiteral("message")).toString(m_message);
    m_code = object.value(QStringLiteral("code")).toInt(m_code);
    // Our bugzilla is a bit bugged. It doesn't necessarily set error to true
    // but instead keeps it at null. Because of this we need to possibly shimy
    // the bool to align with reality.
    if (object.value(QStringLiteral("error")).type() == QJsonValue::Null &&
            m_code > 0 &&
            !m_message.isNull()) {
        m_isError = true;
    }
Harald Sitter's avatar
Harald Sitter committed
49
50
51
    if (m_isError) {
        qCWarning(BUGZILLA_LOG) << "APIException:" << object.toVariantHash();
    }
Harald Sitter's avatar
Harald Sitter committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
}

APIException::APIException(const APIException &other)
    : m_isError(other.m_isError)
    , m_message(other.m_message)
    , m_code(other.m_code)
{
}

QString APIException::whatString() const
{
    return QStringLiteral("[%1] %2").arg(m_code).arg(m_message);
}

void APIException::maybeThrow(const QJsonDocument &document)
{
    APIException ex(document);

    if (ex.isError()) {
        ex.raise();
    }
}

ProtocolException::ProtocolException(const APIJob *job)
    : Exception()
    , m_job(job)
{
79
    qCWarning(BUGZILLA_LOG) << "ProtocolException:" << whatString() << job->error() << job->errorText() << job->errorString();
Harald Sitter's avatar
Harald Sitter committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
}

ProtocolException::ProtocolException(const ProtocolException &other)
    : m_job(other.m_job)
{
}

QString ProtocolException::whatString() const
{
    // String generally includes the error code, so no extra logic needed.
    return m_job->errorString();
}

void ProtocolException::maybeThrow(const APIJob *job)
{
    if (job->error() == KJob::NoError) {
        return;
    }
    throw ProtocolException(job);
}

Exception::~Exception()
{
    delete m_what;
}

const char *Exception::what() const noexcept
{
    strcpy(m_what, qUtf8Printable(whatString()));
    return m_what;
}

RuntimeException::RuntimeException(const QString &reason)
    : m_reason(reason)
{
Harald Sitter's avatar
Harald Sitter committed
115
    qCWarning(BUGZILLA_LOG) << "RuntimeException:" << whatString();
Harald Sitter's avatar
Harald Sitter committed
116
117
118
119
120
121
122
123
124
}

QString RuntimeException::whatString() const
{
    return m_reason;
}

} // namespace Bugzilla