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 #include "exceptions.h"
0008 
0009 #include "apijob.h"
0010 #include "bugzilla_debug.h"
0011 
0012 namespace Bugzilla
0013 {
0014 APIException::APIException(const QJsonDocument &document)
0015     : APIException(document.object())
0016 {
0017 }
0018 
0019 APIException::APIException(const QJsonObject &object)
0020 {
0021     if (object.isEmpty()) {
0022         return;
0023     }
0024     m_isError = object.value(QStringLiteral("error")).toBool(m_isError);
0025     m_message = object.value(QStringLiteral("message")).toString(m_message);
0026     m_code = object.value(QStringLiteral("code")).toInt(m_code);
0027     // Our bugzilla is a bit bugged. It doesn't necessarily set error to true
0028     // but instead keeps it at null. Because of this we need to possibly shimy
0029     // the bool to align with reality.
0030     if (object.value(QStringLiteral("error")).type() == QJsonValue::Null && m_code > 0 && !m_message.isNull()) {
0031         m_isError = true;
0032     }
0033     if (m_isError) {
0034         qCWarning(BUGZILLA_LOG) << "APIException:" << object.toVariantHash();
0035     }
0036 }
0037 
0038 APIException::APIException(const APIException &other)
0039     : m_isError(other.m_isError)
0040     , m_message(other.m_message)
0041     , m_code(other.m_code)
0042 {
0043 }
0044 
0045 QString APIException::whatString() const
0046 {
0047     return QStringLiteral("[%1] %2").arg(m_code).arg(m_message);
0048 }
0049 
0050 void APIException::maybeThrow(const QJsonDocument &document)
0051 {
0052     APIException ex(document);
0053 
0054     if (ex.isError()) {
0055         ex.raise();
0056     }
0057 }
0058 
0059 ProtocolException::ProtocolException(const APIJob *job)
0060     : Exception()
0061     , m_job(job)
0062 {
0063     qCWarning(BUGZILLA_LOG) << "ProtocolException:" << whatString() << job->error() << job->errorText() << job->errorString();
0064 }
0065 
0066 ProtocolException::ProtocolException(const ProtocolException &other)
0067     : m_job(other.m_job)
0068 {
0069 }
0070 
0071 QString ProtocolException::whatString() const
0072 {
0073     // String generally includes the error code, so no extra logic needed.
0074     return m_job->errorString();
0075 }
0076 
0077 void ProtocolException::maybeThrow(const APIJob *job)
0078 {
0079     if (job->error() == KJob::NoError) {
0080         return;
0081     }
0082     throw ProtocolException(job);
0083 }
0084 
0085 Exception::~Exception()
0086 {
0087     delete m_what;
0088 }
0089 
0090 const char *Exception::what() const noexcept
0091 {
0092     strcpy(m_what, qUtf8Printable(whatString()));
0093     return m_what;
0094 }
0095 
0096 RuntimeException::RuntimeException(const QString &reason)
0097     : m_reason(reason)
0098 {
0099     qCWarning(BUGZILLA_LOG) << "RuntimeException:" << whatString();
0100 }
0101 
0102 QString RuntimeException::whatString() const
0103 {
0104     return m_reason;
0105 }
0106 
0107 } // namespace Bugzilla