Various work on certificate handling for screens; need XML config here, now.
[dcpomatic.git] / src / lib / exceptions.h
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU 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, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/exceptions.h
21  *  @brief Our exceptions.
22  */
23
24 #include <stdexcept>
25 #include <sstream>
26 #include <cstring>
27
28 /** @class StringError
29  *  @brief A parent class for exceptions using messages held in a std::string
30  */
31 class StringError : public std::exception
32 {
33 public:
34         /** @param w Error message */
35         StringError (std::string w) {
36                 _what = w;
37         }
38
39         virtual ~StringError () throw () {}
40
41         /** @return error message */
42         char const * what () const throw () {
43                 return _what.c_str ();
44         }
45
46 protected:
47         /** error message */
48         std::string _what;
49 };
50
51 /** @class DecodeError
52  *  @brief A low-level problem with the decoder (possibly due to the nature
53  *  of a source file).
54  */
55 class DecodeError : public StringError
56 {
57 public:
58         DecodeError (std::string s)
59                 : StringError (s)
60         {}
61 };
62
63 /** @class EncodeError
64  *  @brief A low-level problem with an encoder.
65  */
66 class EncodeError : public StringError
67 {
68 public:
69         EncodeError (std::string s)
70                 : StringError (s)
71         {}
72 };
73
74 /** @class FileError.
75  *  @brief Parent class for file-related errors.
76  */
77 class FileError : public StringError
78 {
79 public:
80         /** @param m Error message.
81          *  @param f Name of the file that this exception concerns.
82          */
83         FileError (std::string m, std::string f)
84                 : StringError (m)
85                 , _file (f)
86         {}
87
88         virtual ~FileError () throw () {}
89
90         /** @return name of the file that this exception concerns */
91         std::string file () const {
92                 return _file;
93         }
94
95 private:
96         /** name of the file that this exception concerns */
97         std::string _file;
98 };
99         
100
101 /** @class OpenFileError.
102  *  @brief Indicates that some error occurred when trying to open a file.
103  */
104 class OpenFileError : public FileError
105 {
106 public:
107         /** @param f File that we were trying to open */
108         OpenFileError (std::string f)
109                 : FileError ("could not open file " + f, f)
110         {}
111 };
112
113 /** @class CreateFileError.
114  *  @brief Indicates that some error occurred when trying to create a file.
115  */
116 class CreateFileError : public FileError
117 {
118 public:
119         /** @param f File that we were trying to create */
120         CreateFileError (std::string f)
121                 : FileError ("could not create file " + f, f)
122         {}
123 };
124
125
126 /** @class ReadFileError.
127  *  @brief Indicates that some error occurred when trying to read from a file
128  */
129 class ReadFileError : public FileError
130 {
131 public:
132         /** @param f File that we were trying to read from.
133          *  @param e errno value, or 0.
134          */
135         ReadFileError (std::string f, int e = 0)
136                 : FileError ("", f)
137         {
138                 std::stringstream s;
139                 s << "could not read from file " << f;
140                 if (e) {
141                         s << " (" << strerror (e) << ")";
142                 }
143                 _what = s.str ();
144         }
145 };
146
147 /** @class WriteFileError.
148  *  @brief Indicates that some error occurred when trying to write to a file
149  */
150 class WriteFileError : public FileError
151 {
152 public:
153         /** @param f File that we were trying to write to.
154          *  @param e errno value, or 0.
155          */
156         WriteFileError (std::string f, int e)
157                 : FileError ("", f)
158         {
159                 std::stringstream s;
160                 s << "could not write to file " << f;
161                 if (e) {
162                         s << " (" << strerror (e) << ")";
163                 }
164                 _what = s.str ();
165         }
166 };
167
168 /** @class SettingError.
169  *  @brief Indicates that something is wrong with a setting.
170  */
171 class SettingError : public StringError
172 {
173 public:
174         /** @param s Name of setting that was required.
175          *  @param m Message.
176          */
177         SettingError (std::string s, std::string m)
178                 : StringError (m)
179                 , _setting (s)
180         {}
181
182         virtual ~SettingError () throw () {}
183
184         /** @return name of setting in question */
185         std::string setting () const {
186                 return _setting;
187         }
188
189 private:
190         std::string _setting;
191 };
192
193 /** @class MissingSettingError.
194  *  @brief Indicates that a Film is missing a setting that is required for some operation.
195  */
196 class MissingSettingError : public SettingError
197 {
198 public:
199         /** @param s Name of setting that was required */
200         MissingSettingError (std::string s)
201                 : SettingError (s, "missing required setting " + s)
202         {}
203 };
204
205 /** @class BadSettingError
206  *  @brief Indicates that a setting is bad in some way.
207  */
208 class BadSettingError : public SettingError
209 {
210 public:
211         /** @param s Name of setting that is bad */
212         BadSettingError (std::string s, std::string m)
213                 : SettingError (s, m)
214         {}
215 };
216
217 /** @class NetworkError.
218  *  @brief Indicates some problem with communication on the network.
219  */
220 class NetworkError : public StringError
221 {
222 public:
223         NetworkError (std::string s)
224                 : StringError (s)
225         {}
226 };
227
228 class KDMError : public StringError
229 {
230 public:
231         KDMError (std::string s)
232                 : StringError (s)
233         {}
234 };