Keep signing certificates / keys in config.xml rather than on disk; allow configuration.
[dcpomatic.git] / src / lib / exceptions.h
1 /*
2     Copyright (C) 2012-2014 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/lib/exceptions.h
21  *  @brief Our exceptions.
22  */
23
24 #ifndef DCPOMATIC_EXCEPTIONS_H
25 #define DCPOMATIC_EXCEPTIONS_H
26
27 #include <stdexcept>
28 #include <cstring>
29 #include <boost/exception/all.hpp>
30 #include <boost/filesystem.hpp>
31 #include <boost/thread.hpp>
32 extern "C" {
33 #include <libavutil/pixfmt.h>
34 }
35
36 /** @class StringError
37  *  @brief A parent class for exceptions using messages held in a std::string
38  */
39 class StringError : public std::exception
40 {
41 public:
42         /** @param w Error message */
43         StringError (std::string w) {
44                 _what = w;
45         }
46
47         virtual ~StringError () throw () {}
48
49         /** @return error message */
50         char const * what () const throw () {
51                 return _what.c_str ();
52         }
53
54 protected:
55         /** error message */
56         std::string _what;
57 };
58
59 /** @class DecodeError
60  *  @brief A low-level problem with the decoder (possibly due to the nature
61  *  of a source file).
62  */
63 class DecodeError : public StringError
64 {
65 public:
66         DecodeError (std::string s)
67                 : StringError (s)
68         {}
69 };
70
71 /** @class EncodeError
72  *  @brief A low-level problem with an encoder.
73  */
74 class EncodeError : public StringError
75 {
76 public:
77         EncodeError (std::string s)
78                 : StringError (s)
79         {}
80 };
81
82 /** @class FileError.
83  *  @brief Parent class for file-related errors.
84  */
85 class FileError : public StringError
86 {
87 public:
88         /** @param m Error message.
89          *  @param f Name of the file that this exception concerns.
90          */
91         FileError (std::string m, boost::filesystem::path f)
92                 : StringError (m)
93                 , _file (f)
94         {}
95
96         virtual ~FileError () throw () {}
97
98         /** @return name of the file that this exception concerns */
99         boost::filesystem::path file () const {
100                 return _file;
101         }
102
103 private:
104         /** name of the file that this exception concerns */
105         boost::filesystem::path _file;
106 };
107
108 class JoinError : public StringError
109 {
110 public:
111         JoinError (std::string s)
112                 : StringError (s)
113         {}
114 };
115
116 /** @class OpenFileError.
117  *  @brief Indicates that some error occurred when trying to open a file.
118  */
119 class OpenFileError : public FileError
120 {
121 public:
122         /** @param f File that we were trying to open */
123         OpenFileError (boost::filesystem::path f);
124 };
125
126 /** @class CreateFileError.
127  *  @brief Indicates that some error occurred when trying to create a file.
128  */
129 class CreateFileError : public FileError
130 {
131 public:
132         /** @param f File that we were trying to create */
133         CreateFileError (boost::filesystem::path f);
134 };
135
136
137 /** @class ReadFileError.
138  *  @brief Indicates that some error occurred when trying to read from a file
139  */
140 class ReadFileError : public FileError
141 {
142 public:
143         /** @param f File that we were trying to read from.
144          *  @param e errno value, or 0.
145          */
146         ReadFileError (boost::filesystem::path f, int e = 0);
147 };
148
149 /** @class WriteFileError.
150  *  @brief Indicates that some error occurred when trying to write to a file
151  */
152 class WriteFileError : public FileError
153 {
154 public:
155         /** @param f File that we were trying to write to.
156          *  @param e errno value, or 0.
157          */
158         WriteFileError (boost::filesystem::path f, int e);
159 };
160
161 /** @class SettingError.
162  *  @brief Indicates that something is wrong with a setting.
163  */
164 class SettingError : public StringError
165 {
166 public:
167         /** @param s Name of setting that was required.
168          *  @param m Message.
169          */
170         SettingError (std::string s, std::string m)
171                 : StringError (m)
172                 , _setting (s)
173         {}
174
175         virtual ~SettingError () throw () {}
176
177         /** @return name of setting in question */
178         std::string setting () const {
179                 return _setting;
180         }
181
182 private:
183         std::string _setting;
184 };
185
186 /** @class MissingSettingError.
187  *  @brief Indicates that a Film is missing a setting that is required for some operation.
188  */
189 class MissingSettingError : public SettingError
190 {
191 public:
192         /** @param s Name of setting that was required */
193         MissingSettingError (std::string s);
194 };
195
196 /** @class BadSettingError
197  *  @brief Indicates that a setting is bad in some way.
198  */
199 class BadSettingError : public SettingError
200 {
201 public:
202         /** @param s Name of setting that is bad */
203         BadSettingError (std::string s, std::string m)
204                 : SettingError (s, m)
205         {}
206 };
207
208 /** @class NetworkError
209  *  @brief Indicates some problem with communication on the network.
210  */
211 class NetworkError : public StringError
212 {
213 public:
214         NetworkError (std::string s)
215                 : StringError (s)
216         {}
217 };
218
219 /** @class KDMError
220  *  @brief A problem with a KDM.
221  */
222 class KDMError : public StringError
223 {
224 public:
225         KDMError (std::string s)
226                 : StringError (s)
227         {}
228 };
229
230 /** @class PixelFormatError
231  *  @brief A problem with an unsupported pixel format.
232  */
233 class PixelFormatError : public StringError
234 {
235 public:
236         PixelFormatError (std::string o, AVPixelFormat f);
237 };
238
239 /** @class SubRipError
240  *  @brief An error that occurs while parsing a SubRip file.
241  */
242 class SubRipError : public FileError
243 {
244 public:
245         SubRipError (std::string, std::string, boost::filesystem::path);
246 };
247
248 class DCPError : public StringError
249 {
250 public:
251         DCPError (std::string s)
252                 : StringError (s)
253         {}
254 };
255
256 class InvalidSignerError : public StringError
257 {
258 public:
259         InvalidSignerError ();
260 };
261
262 /** @class ExceptionStore
263  *  @brief A parent class for classes which have a need to catch and
264  *  re-throw exceptions.
265  *
266  *  This is intended for classes which run their own thread; they should do
267  *  something like
268  *
269  *  void my_thread ()
270  *  try {
271  *    // do things which might throw exceptions
272  *  } catch (...) {
273  *    store_current ();
274  *  }
275  *
276  *  and then in another thread call rethrow().  If any
277  *  exception was thrown by my_thread it will be stored by
278  *  store_current() and then rethrow() will re-throw it where
279  *  it can be handled.
280  */
281 class ExceptionStore
282 {
283 public:
284         void rethrow () {
285                 boost::mutex::scoped_lock lm (_mutex);
286                 if (_exception) {
287                         boost::rethrow_exception (_exception);
288                         _exception = boost::exception_ptr ();
289                 }
290         }
291
292 protected:      
293         
294         void store_current () {
295                 boost::mutex::scoped_lock lm (_mutex);
296                 _exception = boost::current_exception ();
297         }
298
299 private:
300         boost::exception_ptr _exception;
301         mutable boost::mutex _mutex;
302 };
303
304 #endif