Line data Source code
1 : // Snap Websites Server -- base exception of the Snap! library
2 : // Copyright (c) 2011-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // https://snapwebsites.org/
5 : // contact@m2osw.com
6 : //
7 : // This program is free software; you can redistribute it and/or modify
8 : // it under the terms of the GNU General Public License as published by
9 : // the Free Software Foundation; either version 2 of the License, or
10 : // (at your option) any later version.
11 : //
12 : // This program is distributed in the hope that it will be useful,
13 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : // GNU General Public License for more details.
16 : //
17 : // You should have received a copy of the GNU General Public License
18 : // along with this program; if not, write to the Free Software
19 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 : #pragma once
21 :
22 : // Qt lib
23 : //
24 : #include <QString>
25 :
26 : // C++ lib
27 : //
28 : #include <stdexcept>
29 :
30 : namespace snap
31 : {
32 :
33 : // Base exception that automatically logs a stack trace at the location
34 : // of the exception (thus our exceptions are extremely slow!)
35 : //
36 : class snap_exception_base
37 : {
38 : public:
39 : static int const STACK_TRACE_DEPTH = 20;
40 :
41 : snap_exception_base(char const * what_msg);
42 : snap_exception_base(std::string const & what_msg);
43 : snap_exception_base(QString const & what_msg);
44 0 : virtual ~snap_exception_base() {}
45 :
46 : static void output_stack_trace( int const stack_trace_depth = STACK_TRACE_DEPTH );
47 : };
48 :
49 :
50 :
51 : // This is a runtime exception; these are expected to be caught
52 : //
53 0 : class snap_exception
54 : : public snap_exception_base
55 : , public std::runtime_error
56 : {
57 : public:
58 : // no sub-name
59 0 : explicit snap_exception(char const * what_msg) : snap_exception_base(what_msg), runtime_error("Snap! Exception: " + std::string(what_msg)) {}
60 0 : explicit snap_exception(std::string const & what_msg) : snap_exception_base(what_msg), runtime_error("Snap! Exception: " + what_msg) {}
61 0 : explicit snap_exception(QString const & what_msg) : snap_exception_base(what_msg), runtime_error(("Snap! Exception: " + what_msg).toUtf8().data()) {}
62 :
63 : // with a sub-name
64 0 : explicit snap_exception(char const * subname, char const * what_msg) : snap_exception_base(what_msg), runtime_error(std::string("Snap! Exception:") + subname + ": " + what_msg) {}
65 0 : explicit snap_exception(char const * subname, std::string const & what_msg) : snap_exception_base(what_msg), runtime_error(std::string("Snap! Exception:") + subname + ": " + what_msg) {}
66 0 : explicit snap_exception(char const * subname, QString const & what_msg) : snap_exception_base(what_msg), runtime_error(std::string("Snap! Exception:") + subname + ": " + what_msg.toUtf8().data()) {}
67 : };
68 :
69 :
70 : // This is a logic exception; you should not catch those, instead you
71 : // should fix the code if they happen
72 : //
73 0 : class snap_logic_exception
74 : : public snap_exception_base
75 : , public std::logic_error
76 : {
77 : public:
78 : // no sub-name
79 0 : explicit snap_logic_exception(char const * what_msg) : snap_exception_base(what_msg), logic_error("Snap! Exception: " + std::string(what_msg)) {}
80 : explicit snap_logic_exception(std::string const & what_msg) : snap_exception_base(what_msg), logic_error("Snap! Exception: " + what_msg) {}
81 0 : explicit snap_logic_exception(QString const & what_msg) : snap_exception_base(what_msg), logic_error(("Snap! Exception: " + what_msg).toUtf8().data()) {}
82 :
83 : // with a sub-name
84 0 : explicit snap_logic_exception(char const * subname, char const * what_msg) : snap_exception_base(what_msg), logic_error(std::string("Snap! Exception:") + subname + ": " + what_msg) {}
85 0 : explicit snap_logic_exception(char const * subname, std::string const & what_msg) : snap_exception_base(what_msg), logic_error(std::string("Snap! Exception:") + subname + ": " + what_msg) {}
86 0 : explicit snap_logic_exception(char const * subname, QString const & what_msg) : snap_exception_base(what_msg), logic_error(std::string("Snap! Exception:") + subname + ": " + what_msg.toUtf8().data()) {}
87 : };
88 :
89 :
90 : // A basic I/O exception one can use anywhere
91 : //
92 : class snap_exception_io : public snap_exception
93 : {
94 : public:
95 : // no sub-name
96 : explicit snap_exception_io(char const * what_msg) : snap_exception(what_msg) {}
97 : explicit snap_exception_io(std::string const & what_msg) : snap_exception(what_msg) {}
98 : explicit snap_exception_io(QString const & what_msg) : snap_exception(what_msg) {}
99 : };
100 :
101 :
102 : // A basic invalid parameter exception
103 : //
104 0 : class snap_exception_invalid_parameter : public snap_logic_exception
105 : {
106 : public:
107 : // no sub-name
108 0 : explicit snap_exception_invalid_parameter(char const * what_msg) : snap_logic_exception(what_msg) {}
109 : explicit snap_exception_invalid_parameter(std::string const & what_msg) : snap_logic_exception(what_msg) {}
110 0 : explicit snap_exception_invalid_parameter(QString const & what_msg) : snap_logic_exception(what_msg) {}
111 : };
112 :
113 :
114 : // A basic missing parameter exception
115 : //
116 0 : class snap_exception_missing_parameter : public snap_logic_exception
117 : {
118 : public:
119 : // no sub-name
120 0 : explicit snap_exception_missing_parameter(char const * what_msg) : snap_logic_exception(what_msg) {}
121 : explicit snap_exception_missing_parameter(std::string const & what_msg) : snap_logic_exception(what_msg) {}
122 : explicit snap_exception_missing_parameter(QString const & what_msg) : snap_logic_exception(what_msg) {}
123 : };
124 :
125 :
126 : } // namespace snap
127 :
128 : // vim: ts=4 sw=4 et
|