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