API
 
Loading...
Searching...
No Matches
exceptions.hpp
Go to the documentation of this file.
1/** \file exceptions.hpp
2 * \brief Exception helpers
3 */
4
5#ifndef common_exceptions_hpp
6#define common_exceptions_hpp
7
8#include <exception>
9#include <source_location>
10#include <format>
11
12namespace MagAOX
13{
14
15/** \defgroup exceptions
16 * \ingroup common
17 *
18 * @{
19 */
20
21/// Augments an exception with the source file and line
22/**
23 * \tparam baseexcept is the base class exception which takes a string as constructor argument
24 */
25class xwcException : public std::exception
26{
27 protected:
28 std::string m_what; ///< The full what message (message + file information).
29
30 std::string m_message; ///< The explanatory message
31
32 std::source_location m_location;
33
34 int m_code{ 0 }; ///< An error code (optional)
35
36 public:
37 xwcException() = delete;
38
39 /// Constructor
40 /**
41 * The what() message becomes "msg (file line)".
42 */
43 explicit
44 xwcException( const std::string &msg, /**<[in] the error descriptionat message) */
45 const std::source_location loc = std::source_location::current() )
46 : m_what{ std::format( "{} ({} {})", msg, loc.file_name(), loc.line() ) }, m_message{ msg }, m_location{ loc }
47 {
48 }
49
50 /// Constructor with code
51 /**
52 * The what() message becomes "msg (file line)".
53 */
54 xwcException( const std::string &msg, /**<[in] the error description (what message) */
55 int code, /**<[in] a descriptive error code */
56 const std::source_location loc =
57 std::source_location::current() /**< [in] [optional] the location of this call */ )
58 : m_what{ std::format( "{} code: {} ({} {})", msg, code, loc.file_name(), loc.line() ) }, m_message{ msg },
59 m_location{ loc }, m_code{ code }
60 {
61 }
62
63 /// Get the what string
64 /** \returns the value of m_what.c_str()
65 *
66 */
67 virtual const char *what() const noexcept
68 {
69 return m_what.c_str();
70 }
71
72 /// Get the message
73 /** \returns the value of m_message
74 *
75 */
76 const std::string &message() const
77 {
78 return m_message;
79 }
80
81 /// Get the source file
82 /** \returns the value of m_location.file_name()
83 *
84 */
85 const std::string file_name() const
86 {
87 return m_location.file_name();
88 }
89
90 /// Get the source line
91 /** \returns the value of m_location.line()
92 *
93 */
94 int line() const
95 {
96 return m_location.line();
97 }
98
99 /// Get the error code
100 /** \returns the value of m_code
101 *
102 */
103 int code() const
104 {
105 return m_code;
106 }
107};
108
109} // namespace MagAOX
110
111///@}
112
113#endif // common_exceptions_hpp
Augments an exception with the source file and line.
xwcException(const std::string &msg, const std::source_location loc=std::source_location::current())
Constructor.
std::string m_what
The full what message (message + file information).
std::source_location m_location
const std::string & message() const
Get the message.
int line() const
Get the source line.
std::string m_message
The explanatory message.
xwcException(const std::string &msg, int code, const std::source_location loc=std::source_location::current())
Constructor with code.
const std::string file_name() const
Get the source file.
virtual const char * what() const noexcept
Get the what string.
int code() const
Get the error code.
int m_code
An error code (optional)
Definition dm.hpp:28