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 #ifndef SKGERROR_H
0007 #define SKGERROR_H
0008 /** @file
0009  * This file defines classes SKGError and macros.
0010  *
0011  * @author Stephane MANKOWSKI / Guillaume DE BURE
0012  */
0013 #include <qobject.h>
0014 #include <qstring.h>
0015 
0016 #include "skgbasemodeler_export.h"
0017 #include "skgdefine.h"
0018 
0019 /**
0020  * To facilitate the error management
0021  */
0022 #define IFKO(ERROR) \
0023     if (Q_UNLIKELY(ERROR))
0024 
0025 /**
0026  * To facilitate the error management
0027  */
0028 #define IFOK(ERROR) \
0029     if (Q_LIKELY(!(ERROR)))
0030 
0031 /**
0032  * To facilitate the error management
0033  */
0034 #define IFOKDO(ERROR, ACTION) \
0035     IFOK(ERROR) {(ERROR) = ACTION;}
0036 
0037 /**
0038 * This class manages errors
0039 */
0040 class SKGBASEMODELER_EXPORT SKGError final
0041 {
0042     /**
0043      * Return code of the error
0044      */
0045     Q_PROPERTY(int returnCode READ getReturnCode WRITE setReturnCode)
0046     /**
0047      * Message of the error
0048      */
0049     Q_PROPERTY(QString message READ getMessage WRITE setMessage)
0050     /**
0051      * Internal data of the error
0052      */
0053     Q_PROPERTY(QString property READ getProperty WRITE setProperty)
0054     /**
0055      * Action of the error
0056      */
0057     Q_PROPERTY(QString action READ getAction WRITE setAction)
0058     /**
0059      * To know if it is a success of the error
0060      */
0061     Q_PROPERTY(bool succeeded READ isSucceeded)
0062     /**
0063      * To know if it is a failure of the error
0064      */
0065     Q_PROPERTY(bool failed READ isFailed)
0066 public:
0067     /**
0068     * Constructor
0069     */
0070     explicit SKGError();
0071 
0072     /**
0073     * Copy constructor
0074     * @param iError the error to copy
0075     */
0076     SKGError(const SKGError& iError);
0077 
0078     /**
0079     * Move constructor
0080     * @param iError the error to copy
0081     */
0082     SKGError(SKGError&& iError) noexcept;
0083 
0084     /**
0085     * Constructor
0086     * @param iRc the error code
0087     * @param iMessage the error message
0088     * @param iAction the error action. This is normally a recovery action (example: skg://file_save, https://google.com, …)
0089     */
0090     SKGError(int iRc, QString  iMessage, QString  iAction = QString());
0091 
0092     /**
0093     * Destructor
0094     */
0095     ~SKGError();
0096 
0097     /**
0098     * Operator affectation
0099     * @param iError the error to copy
0100     */
0101     SKGError& operator= (const SKGError& iError);
0102 
0103     /**
0104     * To know if this is an error or not. Equivalent to @see isSucceeded
0105     * @return true or false
0106     */
0107     bool operator!() const;
0108 
0109     /**
0110     * To know if this is an error or not. Equivalent to @see isFailed
0111     * @return true or false
0112     */
0113     operator bool() const;
0114 
0115     /**
0116     * To know if it is an error or not
0117     * @return true: It is an error
0118     *         false: It is not an error (it could be a warning)
0119     */
0120     bool isFailed() const;
0121 
0122     /**
0123     * To know if it is an error or not
0124     * @return true: It is not an error (it could be a warning)
0125     *         false: It is an error
0126     */
0127     bool isSucceeded() const;
0128 
0129     /**
0130     * To know if it is a warning or not
0131     * @return true: It is a warning
0132     *         false: It is not a warning
0133     */
0134     bool isWarning() const;
0135 
0136     /**
0137     * Return the return code associated to this error
0138     * @return 0 : It is not an error (SUCCEEDED)
0139     *         <0: It is just a warning (SUCCEEDED)
0140     *         >0: It is not error (FAILED)
0141     */
0142     int getReturnCode() const;
0143 
0144     /**
0145     * Return the message associated to this error
0146     * @return the message
0147     */
0148     QString getMessage() const;
0149 
0150     /**
0151     * Return the internal data associated to this error
0152     * @return the internal data
0153     */
0154     QString getProperty() const;
0155 
0156     /**
0157     * Return the full message associated to this error
0158     * @return the full message
0159     */
0160     QString getFullMessage() const;
0161 
0162     /**
0163     * Return the full message with historical associated to this error
0164     * @return the full message
0165     */
0166     QString getFullMessageWithHistorical() const;
0167 
0168     /**
0169      * Return the size of the historical associated to this error
0170      * @return the size
0171      */
0172     int getHistoricalSize() const;
0173 
0174     /**
0175     * Return the action associated to this error
0176     * @return the action
0177     */
0178     QString getAction() const;
0179 
0180     /**
0181     * Return previous error associated to this SKGError in the historical
0182     * @return previous error (null if not exist)
0183     * WARNING: this pointer mustn't be deleted
0184     */
0185     SKGError* getPreviousError() const;
0186 
0187 public Q_SLOTS:
0188     /**
0189     * Set the return code associated to this error
0190     * @param iReturnCode the return code
0191     *         0 : It is not an error (SUCCEEDED)
0192     *         <0: It is just a warning (SUCCEEDED)
0193     *         >0: It is not error (FAILED)
0194     * @return itself to facilitate usage
0195     */
0196     SKGError& setReturnCode(int iReturnCode);
0197 
0198     /**
0199     * Set the message associated to this error
0200     * @param iMessage the message
0201     * @return itself to facilitate usage
0202     */
0203     SKGError& setMessage(const QString& iMessage);
0204 
0205     /**
0206     * Set internal data associated to this error
0207     * @param iProperty the internal data
0208     * @return itself to facilitate usage
0209     */
0210     SKGError& setProperty(const QString& iProperty);
0211 
0212     /**
0213     * Set the action associated to this error
0214     * @param iAction the action
0215     * @return itself to facilitate usage
0216     */
0217     SKGError& setAction(const QString& iAction);
0218 
0219     /**
0220     * Add a new historical message to the current error.
0221     * @param iRc the error code
0222     * @param iMessage the error message
0223     * @param iAction the error action. This is normally a recovery action (example: skg://file_save, https://google.com, …)
0224     * @return itself to facilitate usage
0225     */
0226     SKGError& addError(int iRc, const QString& iMessage, const QString& iAction = QString());
0227 
0228     /**
0229     * Add a new historical message to the current error.
0230     * @param iError the error
0231     * @return itself to facilitate usage
0232     */
0233     SKGError& addError(const SKGError& iError);
0234 
0235 private:
0236     Q_GADGET
0237     /**
0238     * the return code of the error
0239     *         0 : It is not an error (SUCCEEDED)
0240     *         <0: It is just a warning (SUCCEEDED)
0241     *         >0: It is not error (FAILED)
0242     */
0243     int m_rc{0};
0244 
0245     /**
0246      * the message of the error
0247      */
0248     QString m_message;
0249 
0250     /**
0251      * the property of the error
0252      */
0253     QString m_property;
0254 
0255     /**
0256      * the action of the error
0257      */
0258     QString m_action;
0259 
0260     /**
0261      * the previous error on this branch
0262      */
0263     SKGError* m_previousError{nullptr};
0264 };
0265 #endif  // SKGERROR_H