LCOV - code coverage report
Current view: top level - src/AH/Error - Error.hpp (source / functions) Hit Total Coverage
Test: 90a1b9beff85a60dc6ebcea034a947a845e56960 Lines: 5 5 100.0 %
Date: 2019-11-30 15:53:32 Functions: 4 5 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : /// @file
       4             : 
       5             : #include <AH/Settings/Warnings.hpp>
       6             : AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
       7             : 
       8             : #include <AH/Debug/Debug.hpp>
       9             : 
      10             : #ifdef ARDUINO // ------------------------------------------------------ ARDUINO
      11             : 
      12             : BEGIN_AH_NAMESPACE
      13             : 
      14             : /// Function that executes and loops forever, blinking the built-in LED when a
      15             : /// fatal error is encountered.
      16             : extern void fatalErrorExit() __attribute__((noreturn));
      17             : 
      18             : END_AH_NAMESPACE
      19             : 
      20             : #ifdef FATAL_ERRORS
      21             : 
      22             : #define ERROR(msg, errc)                                                       \
      23             :     do {                                                                       \
      24             :         USING_AH_NAMESPACE;                                                    \
      25             :         DEBUGFN(msg << " (0x" << hex << uppercase << errc << dec               \
      26             :                     << nouppercase << ')');                                    \
      27             :         fatalErrorExit();                                                      \
      28             :     } while (0)
      29             : 
      30             : #else
      31             : 
      32             : /// Print the error message and error code, and stop the execution if
      33             : /// `FATAL_ERRORS` are enabled. Otherwise just prints the error.
      34             : ///
      35             : /// @param  msg
      36             : ///         The information to print, can contain streaming operators (`<<`) to
      37             : ///         print multiple things.
      38             : /// @param  errc
      39             : ///         A unique error code.
      40             : ///
      41             : /// @ingroup    AH_Error
      42             : #define ERROR(msg, errc)                                                       \
      43             :     do {                                                                       \
      44             :         DEBUGFN(msg << " (0x" << hex << uppercase << errc << dec               \
      45             :                     << nouppercase << ')');                                    \
      46             :     } while (0)
      47             : 
      48             : #endif
      49             : 
      50             : /// Print the error message and error code, and stop the execution.
      51             : /// Doesn't depend on `FATAL_ERRORS`, it always stops the execution.
      52             : ///
      53             : /// @param  msg
      54             : ///         The information to print, can contain streaming operators (`<<`) to
      55             : ///         print multiple things.
      56             : /// @param  errc
      57             : ///         A unique error code.
      58             : ///
      59             : /// @ingroup    AH_Error
      60             : #define FATAL_ERROR(msg, errc)                                                 \
      61             :     do {                                                                       \
      62             :         USING_AH_NAMESPACE;                                                    \
      63             :         DEBUGFN(F("Fatal Error: ") << msg << " (0x" << hex << uppercase        \
      64             :                                    << errc << dec << nouppercase << ')');      \
      65             :         fatalErrorExit();                                                      \
      66             :     } while (0)
      67             : 
      68             : #else // ----------------------------------------------------------------- TESTS
      69             : 
      70             : #include <exception>
      71             : #include <sstream>
      72             : 
      73             : BEGIN_AH_NAMESPACE
      74             : 
      75          17 : class ErrorException : public std::exception {
      76             :   public:
      77          17 :     ErrorException(const std::string message, int errorCode)
      78          34 :         : message(std::move(message)), errorCode(errorCode) {}
      79           1 :     const char *what() const throw() override { return message.c_str(); }
      80          11 :     int getErrorCode() const { return errorCode; }
      81             : 
      82             :   private:
      83             :     const std::string message;
      84             :     const int errorCode;
      85             : };
      86             : 
      87             : END_AH_NAMESPACE
      88             : 
      89             : #define ERROR(msg, errc)                                                       \
      90             :     do {                                                                       \
      91             :         USING_AH_NAMESPACE;                                                    \
      92             :         std::ostringstream s;                                                  \
      93             :         s << DEBUG_FUNC_LOCATION << msg;                                       \
      94             :         throw ErrorException(s.str(), errc);                                   \
      95             :     } while (0)
      96             : 
      97             : #define FATAL_ERROR(msg, errc)                                                 \
      98             :     do {                                                                       \
      99             :         USING_AH_NAMESPACE;                                                    \
     100             :         std::ostringstream s;                                                  \
     101             :         s << DEBUG_FUNC_LOCATION << msg;                                       \
     102             :         throw ErrorException(s.str(), errc);                                   \
     103             :     } while (0)
     104             : 
     105             : #endif
     106             : 
     107             : AH_DIAGNOSTIC_POP()

Generated by: LCOV version 1.14-5-g4ff2ed6