File indexing completed on 2024-04-28 16:30:31

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 /** @file
0007 * This file implements classes SKGError.
0008 *
0009 * @author Stephane MANKOWSKI / Guillaume DE BURE
0010 */
0011 #include "skgerror.h"
0012 
0013 SKGError::SKGError()
0014     : m_message(QLatin1String("")), m_property(QLatin1String(""))
0015 {}
0016 
0017 SKGError::SKGError(int iRc, QString  iMessage, QString  iAction)
0018     : m_rc(iRc), m_message(std::move(iMessage)),  m_property(QLatin1String("")), m_action(std::move(iAction)), m_previousError(nullptr)
0019 {}
0020 
0021 SKGError::SKGError(const SKGError& iError)
0022 {
0023     m_rc = iError.m_rc;
0024     m_message = iError.m_message;
0025     m_property = iError.m_property;
0026     m_action = iError.m_action;
0027     if (Q_UNLIKELY(iError.m_previousError != nullptr)) {
0028         m_previousError = new SKGError(*iError.m_previousError);
0029     } else {
0030         m_previousError = nullptr;
0031     }
0032 }
0033 
0034 SKGError::SKGError(SKGError&& iError) noexcept
0035 {
0036     m_rc = iError.m_rc;
0037     m_message = iError.m_message;
0038     m_property = iError.m_property;
0039     m_action = iError.m_action;
0040     m_previousError = iError.m_previousError;
0041     iError.m_previousError = nullptr;
0042 }
0043 
0044 SKGError::~SKGError()
0045 {
0046     delete m_previousError;
0047     m_previousError = nullptr;
0048 }
0049 
0050 SKGError& SKGError::operator= (const SKGError& iError)
0051 {
0052     if (Q_LIKELY(&iError != this)) {
0053         delete m_previousError;
0054         m_previousError = nullptr;
0055 
0056         m_rc = iError.m_rc;
0057         m_message = iError.m_message;
0058         m_property = iError.m_property;
0059         m_action = iError.m_action;
0060         if (Q_UNLIKELY(iError.m_previousError != nullptr)) {
0061             m_previousError = new SKGError(*iError.m_previousError);
0062         }
0063     }
0064     return *this;
0065 }
0066 
0067 bool SKGError::operator!() const
0068 {
0069     return isSucceeded();
0070 }
0071 
0072 SKGError::operator bool() const
0073 {
0074     return isFailed();
0075 }
0076 
0077 bool SKGError::isFailed() const
0078 {
0079     return (m_rc > 0);
0080 }
0081 
0082 bool SKGError::isSucceeded() const
0083 {
0084     return (m_rc <= 0);
0085 }
0086 
0087 bool SKGError::isWarning() const
0088 {
0089     return (m_rc < 0);
0090 }
0091 
0092 int SKGError::getReturnCode() const
0093 {
0094     return m_rc;
0095 }
0096 
0097 SKGError& SKGError::setReturnCode(int iReturnCode)
0098 {
0099     m_rc = iReturnCode;
0100     return *this;
0101 }
0102 
0103 SKGError& SKGError::setMessage(const QString& iMessage)
0104 {
0105     m_message = iMessage;
0106     return *this;
0107 }
0108 
0109 QString SKGError::getMessage() const
0110 {
0111     return m_message;
0112 }
0113 
0114 SKGError& SKGError::setProperty(const QString& iProperty)
0115 {
0116     m_property = iProperty;
0117     return *this;
0118 }
0119 
0120 QString SKGError::getProperty() const
0121 {
0122     return m_property;
0123 }
0124 
0125 QString SKGError::getFullMessage() const
0126 {
0127     QString output('[');
0128     output += (m_rc == 0 ? QStringLiteral("SUC") : (m_rc > 0 ? QStringLiteral("ERR") : QStringLiteral("WAR")));
0129     output += '-';
0130 
0131     QString tmp;
0132     tmp.setNum(m_rc);
0133     output += tmp;
0134     output += ']';
0135     if (Q_LIKELY(!m_message.isEmpty())) {
0136         output += ": " % m_message;
0137     }
0138     return output;
0139 }
0140 
0141 QString SKGError::getFullMessageWithHistorical() const
0142 {
0143     QString output = getFullMessage();
0144     if (Q_UNLIKELY(m_previousError)) {
0145         output += '\n' % m_previousError->getFullMessageWithHistorical();
0146     }
0147     return output;
0148 }
0149 
0150 int SKGError::getHistoricalSize() const
0151 {
0152     int output = 0;
0153     if (Q_UNLIKELY(m_previousError)) {
0154         output += 1 + m_previousError->getHistoricalSize();
0155     }
0156     return output;
0157 }
0158 
0159 
0160 SKGError& SKGError::setAction(const QString& iAction)
0161 {
0162     if (m_action != iAction) {
0163         m_action = iAction;
0164     }
0165     return *this;
0166 }
0167 
0168 QString SKGError::getAction() const
0169 {
0170     return m_action;
0171 }
0172 
0173 SKGError& SKGError::addError(int iRc, const QString& iMessage, const QString& iAction)
0174 {
0175     auto pe = new SKGError(*this);
0176     setReturnCode(iRc);
0177     setMessage(iMessage);
0178     setAction(iAction);
0179     delete m_previousError;
0180     m_previousError = pe;
0181     return *this;
0182 }
0183 
0184 SKGError& SKGError::addError(const SKGError& iError)
0185 {
0186     return addError(iError.getReturnCode(), iError.getMessage());
0187 }
0188 
0189 SKGError* SKGError::getPreviousError() const
0190 {
0191     return m_previousError;
0192 }
0193 
0194