dodo  0.0.1
A C++ library to create containerized Linux services
exception.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file exception.hpp
20  * Defines dodo::common::Exception.
21  */
22 
23 #ifndef dodo_common_exception_hpp
24 #define dodo_common_exception_hpp
25 
26 #include <stdexcept>
27 #include <string>
28 #include <sstream>
29 #include <iomanip>
30 #include <set>
31 
32 #include <common/systemerror.hpp>
33 
34 namespace dodo::common {
35 
36  /**
37  * Interface to objects that support dumping their state to a string.
38  */
39  class DebugObject {
40  public:
41 
42  /**
43  * Default constructor does nothing.
44  */
46 
47  /**
48  * Destructor does nothing.
49  */
50  virtual ~DebugObject() {};
51 
52  /**
53  * Return the object dump to string. debugHeader() and debugDetail() is integrated in the dump.
54  * @return that string.
55  * @see debugHeader()
56  * @see debugDetail()
57  */
58  std::string debugString() const;
59 
60  protected:
61 
62  /**
63  * Descendant classes can override to dump details specific to the class. By default, returns nothing.
64  * @return The string.
65  */
66  virtual std::string debugDetail() const { return ""; };
67 
68  /**
69  * Generates a debug header (address of this object and a demangled class name.
70  * @return The string.
71  */
72  std::string debugHeader() const;
73 
74  };
75 
76  /**
77  * An Exception is thrown in exceptional circumstances, and its occurrence should generally imply that the program
78  * should stop, as it has entered a state it was never designed to handle.
79  *
80  * When used in conjunction with the throw_Exception() and throw_SystemException() macros, the source file
81  * and line number where the Exception is thrown are picked up automatically.
82  */
83  class Exception : public std::runtime_error {
84  public:
85  /**
86  * Construct an Exception. Use the throw_Exception() macro to construct and throw Exceptions.
87  * @param file The source file where the exception was raised.
88  * @param line The line number where the exception was raised.
89  * @param what The exception message.
90  */
91  Exception( const std::string &file,
92  unsigned int line,
93  const std::string &what );
94  /**
95  * Construct an Exception. Use the throw_ExceptionObject() macro to construct and throw Exceptions.
96  * @param file The source file where the exception was raised.
97  * @param line The line number where the exception was raised.
98  * @param what The exception message.
99  * @param thing The exception context.
100  */
101  Exception( const std::string &file,
102  unsigned int line,
103  const std::string &what,
104  const DebugObject* thing );
105 
106  virtual ~Exception();
107 
108  /**
109  * Return the exception message.
110  * @return the exception message.
111  */
112  virtual const char* what() const noexcept;
113 
114  /**
115  * Return the source file where the exception was thrown.
116  * @return The source file where the exception was thrown.
117  */
118  const std::string& getFile() const { return file_; };
119 
120  /**
121  * Return the line number in the source file where the exception was thrown.
122  * @return The line number in the source file where the exception was thrown.
123  */
124  unsigned int getLine() const { return line_; };
125  protected:
126  /** The source file */
127  std::string file_;
128  /** The source line number */
129  unsigned int line_;
130  /** The exception message */
131  std::string msg_;
132  };
133 
134  /**
135  * Descending from Exception, exceptions based on a dodo::common::SystemError code.
136  * @see SystemError
137  */
138  class SystemException : public Exception {
139  public:
140 
141  /**
142  * Constructor
143  * @param file The source file where the exception was raised.
144  * @param line The line number where the exception was raised.
145  * @param what The exception message.
146  * @param error The underlying SystemError.
147  */
148  SystemException( const std::string &file,
149  unsigned int line,
150  const std::string &what,
151  const dodo::common::SystemError &error );
152  /**
153  * Constructor
154  * @param file The source file where the exception was raised.
155  * @param line The line number where the exception was raised.
156  * @param what The exception message.
157  * @param error The underlying SystemError.
158  * @param thing The DebugObject context to the error.
159  */
160  SystemException( const std::string &file,
161  unsigned int line,
162  const std::string &what,
163  const dodo::common::SystemError &error,
164  const DebugObject* thing );
165  protected:
166  /** The exception system error */
168  };
169 
170  /**
171  * Throws an Exception, passes __FILE__ and __LINE__ to constructor.
172  * @param what The exception message passed as << stream to dodo::common::Puts()
173  */
174  #define throw_Exception( what ) throw dodo::common::Exception( __FILE__, __LINE__, dodo::common::Puts() << what )
175 
176  /**
177  * Throws an Exception with DebugContext, passes __FILE__ and __LINE__ to constructor.
178  * @param what The exception message as std::string.
179  * @param thing The DebugObject as context.
180  */
181  #define throw_ExceptionObject( what, thing ) throw dodo::common::Exception( __FILE__, __LINE__, dodo::common::Puts() << what, thing )
182 
183  /**
184  * Throws an Exception with errno, passes __FILE__ and __LINE__ to constructor.
185  * @param what The exception message as std::string.
186  * @param errno The error number.
187  */
188  #define throw_SystemException( what, errno ) throw dodo::common::SystemException( __FILE__, __LINE__, dodo::common::Puts() << what, errno )
189 
190  /**
191  * Throws an Exception with errno, passes __FILE__ and __LINE__ to constructor.
192  * @param what The exception message as std::string.
193  * @param errno The error number.
194  * @param thing The DebugObject as context.
195  */
196  #define throw_SystemExceptionObject( what, errno, thing ) throw dodo::common::SystemException( __FILE__, __LINE__, dodo::common::Puts() << what, errno, thing )
197 
198 };
199 
200 #endif
dodo::common::SystemException
Descending from Exception, exceptions based on a dodo::common::SystemError code.
Definition: exception.hpp:138
dodo::common::Exception::line_
unsigned int line_
The source line number.
Definition: exception.hpp:129
dodo::common::DebugObject::~DebugObject
virtual ~DebugObject()
Destructor does nothing.
Definition: exception.hpp:50
dodo::common::SystemException::error_
dodo::common::SystemError error_
The exception system error.
Definition: exception.hpp:167
dodo::common::DebugObject
Interface to objects that support dumping their state to a string.
Definition: exception.hpp:39
dodo::common::Exception::getLine
unsigned int getLine() const
Return the line number in the source file where the exception was thrown.
Definition: exception.hpp:124
dodo::common::Exception::Exception
Exception(const std::string &file, unsigned int line, const std::string &what)
Construct an Exception.
Definition: exception.cpp:55
dodo::common::Exception::getFile
const std::string & getFile() const
Return the source file where the exception was thrown.
Definition: exception.hpp:118
dodo::common::Exception::what
virtual const char * what() const noexcept
Return the exception message.
Definition: exception.cpp:73
dodo::common::DebugObject::debugHeader
std::string debugHeader() const
Generates a debug header (address of this object and a demangled class name.
Definition: exception.cpp:39
dodo::common::Exception::file_
std::string file_
The source file.
Definition: exception.hpp:124
dodo::common::SystemException::SystemException
SystemException(const std::string &file, unsigned int line, const std::string &what, const dodo::common::SystemError &error)
Constructor.
Definition: exception.cpp:77
dodo::common
Common and utility interfaces.
Definition: application.hpp:29
dodo::common::DebugObject::debugString
std::string debugString() const
Return the object dump to string.
Definition: exception.cpp:32
dodo::common::DebugObject::debugDetail
virtual std::string debugDetail() const
Descendant classes can override to dump details specific to the class.
Definition: exception.hpp:66
dodo::common::SystemError
Linux system error primitive to provide a consistent interface to Linux error codes.
Definition: systemerror.hpp:53
systemerror.hpp
dodo::common::Exception::msg_
std::string msg_
The exception message.
Definition: exception.hpp:131
dodo::common::DebugObject::DebugObject
DebugObject()
Default constructor does nothing.
Definition: exception.hpp:45
dodo::common::Exception
An Exception is thrown in exceptional circumstances, and its occurrence should generally imply that t...
Definition: exception.hpp:83