Line data Source code
1 : #pragma once 2 : 3 : #include "Debug.hpp" 4 : 5 : #ifdef ARDUINO // ------------------------------------------------------ ARDUINO 6 : 7 : BEGIN_CS_NAMESPACE 8 : 9 : /// Function that executes and loops forever, blinking the LED when a fatal 10 : /// error is encountered. 11 : extern void fatalErrorExit() __attribute__((noreturn)); 12 : 13 : END_CS_NAMESPACE 14 : 15 : #ifdef FATAL_ERRORS 16 : 17 : #define ERROR(x, e) \ 18 : do { \ 19 : USING_CS_NAMESPACE; \ 20 : DEBUGFN(x << " (0x" << hex << uppercase << e << dec << nouppercase \ 21 : << ')'); \ 22 : fatalErrorExit(); \ 23 : } while (0) 24 : 25 : #else 26 : 27 : #define ERROR(x, e) \ 28 : do { \ 29 : DEBUGFN(x << " (0x" << hex << uppercase << e << dec << nouppercase \ 30 : << ')'); \ 31 : } while (0) 32 : 33 : #endif 34 : 35 : #define FATAL_ERROR(x, e) \ 36 : do { \ 37 : DEBUGFN(F("Fatal Error: ") << x << " (0x" << hex << uppercase << e \ 38 : << dec << nouppercase << ')'); \ 39 : fatalErrorExit(); \ 40 : } while (0) 41 : 42 : #else // ----------------------------------------------------------------- TESTS 43 : 44 : #include <exception> 45 : #include <sstream> 46 : 47 : BEGIN_CS_NAMESPACE 48 : 49 15 : class ErrorException : public std::exception { 50 : public: 51 15 : ErrorException(const std::string message, int errorCode) 52 30 : : message(std::move(message)), errorCode(errorCode) {} 53 1 : const char *what() const throw() override { return message.c_str(); } 54 11 : int getErrorCode() const { return errorCode; } 55 : 56 : private: 57 : const std::string message; 58 : const int errorCode; 59 : }; 60 : 61 : END_CS_NAMESPACE 62 : 63 : #define ERROR(x, e) \ 64 : do { \ 65 : USING_CS_NAMESPACE; \ 66 : std::ostringstream s; \ 67 : s << FUNC_LOCATION << x; \ 68 : throw ErrorException(s.str(), e); \ 69 : } while (0) 70 : 71 : #define FATAL_ERROR(x, e) \ 72 : do { \ 73 : USING_CS_NAMESPACE; \ 74 : std::ostringstream s; \ 75 : s << FUNC_LOCATION << x; \ 76 : throw ErrorException(s.str(), e); \ 77 : } while (0) 78 : 79 : #endif