Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #ifndef ELEKTRA_KDB_EXCEPT_HPP
10 : #define ELEKTRA_KDB_EXCEPT_HPP
11 :
12 : /*
13 : * @brief See examples/cpp_example_userexception.cpp for how to use
14 : * USER_DEFINED_EXCEPTIONS.
15 : */
16 : #ifndef USER_DEFINED_EXCEPTIONS
17 :
18 : #include <keyexcept.hpp>
19 :
20 : #include <sstream>
21 : #include <string>
22 :
23 : #include <kdbio.hpp>
24 :
25 : namespace kdb
26 : {
27 :
28 16 : class KDBException : public Exception
29 : {
30 : public:
31 448 : explicit KDBException (Key key) : m_key (key), m_str ()
32 : {
33 112 : }
34 :
35 116 : virtual ~KDBException () throw ()
36 448 : {
37 116 : }
38 :
39 0 : virtual const char * what () const throw ()
40 : {
41 0 : return whatWithArguments (true, true);
42 : }
43 :
44 40 : virtual const char * whatWithArguments (bool printVerbose, bool printDebug) const throw ()
45 : {
46 80 : if (!m_key)
47 : {
48 : return "Generic KDBException";
49 : }
50 80 : else if (m_str.empty ())
51 : {
52 : // note that the code will be re-evaluated
53 : // if it prints nothing, but an expensive
54 : // function not printing anything seems
55 : // to be unlikely.
56 : //
57 : // note that printError/printWarning will be
58 : // used either from namespace kdb or global
59 : // namespace.
60 80 : std::stringstream ss;
61 40 : printWarnings (ss, m_key, printVerbose, printDebug);
62 40 : printError (ss, m_key, printVerbose, printDebug);
63 80 : m_str = ss.str ();
64 : }
65 80 : return m_str.c_str ();
66 : }
67 :
68 : protected:
69 : Key m_key;
70 :
71 : private:
72 : mutable std::string m_str;
73 : };
74 :
75 : class ContractException : public KDBException
76 : {
77 : public:
78 0 : explicit ContractException (Key key) : KDBException (key)
79 : {
80 0 : }
81 :
82 0 : ~ContractException () noexcept override = default;
83 :
84 0 : const char * what () const noexcept override
85 : {
86 0 : if (!m_key)
87 : {
88 : return "Malformed contract";
89 : }
90 0 : return KDBException::what ();
91 : }
92 : };
93 :
94 : } // namespace kdb
95 :
96 : #endif
97 :
98 : #endif
|