7c9509800ed3a40bf35f2cd88ce45755c44a03f8
[dcpomatic.git] / src / lib / exceptions.h
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 /** @file  src/lib/exceptions.h
23  *  @brief Our exceptions.
24  */
25
26
27 #ifndef DCPOMATIC_EXCEPTIONS_H
28 #define DCPOMATIC_EXCEPTIONS_H
29
30
31 #include "compose.hpp"
32 extern "C" {
33 #include <libavutil/pixfmt.h>
34 }
35 #include <boost/filesystem.hpp>
36 #include <boost/optional.hpp>
37 #include <cstring>
38 #include <stdexcept>
39
40
41 /** @class DecodeError
42  *  @brief A low-level problem with the decoder (possibly due to the nature
43  *  of a source file).
44  */
45 class DecodeError : public std::runtime_error
46 {
47 public:
48         explicit DecodeError (std::string s)
49                 : std::runtime_error (s)
50         {}
51
52         DecodeError (std::string function, std::string caller)
53                 : std::runtime_error (String::compose("%1 failed [%2]", function, caller))
54         {}
55
56         DecodeError (std::string function, std::string caller, int error)
57                 : std::runtime_error (String::compose("%1 failed [%2] (%3)", function, caller, error))
58         {}
59
60         DecodeError (std::string function, std::string caller, boost::filesystem::path file)
61                 : std::runtime_error (String::compose("%1 failed [%2] (%3)", function, caller, file.string()))
62         {}
63
64         DecodeError (std::string function, std::string caller, int error, boost::filesystem::path file)
65                 : std::runtime_error (String::compose("%1 failed [%2] (%3) (%4)", function, caller, error, file.string()))
66         {}
67 };
68
69
70 class CryptoError : public std::runtime_error
71 {
72 public:
73         explicit CryptoError (std::string s)
74                 : std::runtime_error (s)
75         {}
76 };
77
78
79 /** @class EncodeError
80  *  @brief A low-level problem with an encoder.
81  */
82 class EncodeError : public std::runtime_error
83 {
84 public:
85         explicit EncodeError (std::string s)
86                 : std::runtime_error (s)
87         {}
88
89         explicit EncodeError (std::string function, std::string caller)
90                 : std::runtime_error (String::compose("%1 failed [%2]", function, caller))
91         {}
92
93         explicit EncodeError (std::string function, std::string caller, int error)
94                 : std::runtime_error (String::compose("%1 failed [%2] (%3)", function, caller, error))
95         {}
96 };
97
98
99 /** @class FileError.
100  *  @brief Parent class for file-related errors.
101  */
102 class FileError : public std::runtime_error
103 {
104 public:
105         /** @param m Error message.
106          *  @param f Name of the file that this exception concerns.
107          */
108         FileError (std::string m, boost::filesystem::path f)
109                 : std::runtime_error (String::compose("%1 with %2", m, f.string()))
110                 , _file (f)
111         {}
112
113         virtual ~FileError () throw () {}
114
115         /** @return name of the file that this exception concerns */
116         boost::filesystem::path file () const {
117                 return _file;
118         }
119
120 private:
121         /** name of the file that this exception concerns */
122         boost::filesystem::path _file;
123 };
124
125
126 class JoinError : public std::runtime_error
127 {
128 public:
129         explicit JoinError (std::string s)
130                 : std::runtime_error (s)
131         {}
132 };
133
134
135 /** @class OpenFileError.
136  *  @brief Indicates that some error occurred when trying to open a file.
137  */
138 class OpenFileError : public FileError
139 {
140 public:
141         enum Mode {
142                 READ,
143                 WRITE,
144                 READ_WRITE
145         };
146
147         /** @param f File that we were trying to open.
148          *  @param error Code of error that occurred.
149          *  @param mode Mode that we tried to open the file in.
150          */
151         OpenFileError (boost::filesystem::path f, int error, Mode mode);
152 };
153
154
155 class FileNotFoundError : public std::runtime_error
156 {
157 public:
158         FileNotFoundError (boost::filesystem::path f);
159         virtual ~FileNotFoundError () throw () {}
160
161         /** @return name of the file that this exception concerns */
162         boost::filesystem::path file () const {
163                 return _file;
164         }
165
166 private:
167         /** name of the file that this exception concerns */
168         boost::filesystem::path _file;
169 };
170
171
172 /** @class ReadFileError.
173  *  @brief Indicates that some error occurred when trying to read from a file
174  */
175 class ReadFileError : public FileError
176 {
177 public:
178         /** @param f File that we were trying to read from.
179          *  @param e errno value, or 0.
180          */
181         ReadFileError (boost::filesystem::path f, int e = 0);
182 };
183
184
185 /** @class WriteFileError.
186  *  @brief Indicates that some error occurred when trying to write to a file
187  */
188 class WriteFileError : public FileError
189 {
190 public:
191         /** @param f File that we were trying to write to.
192          *  @param e errno value, or 0.
193          */
194         WriteFileError (boost::filesystem::path f, int e);
195 };
196
197
198 /** @class SettingError.
199  *  @brief Indicates that something is wrong with a setting.
200  */
201 class SettingError : public std::runtime_error
202 {
203 public:
204         /** @param s Name of setting that was required.
205          *  @param m Message.
206          */
207         SettingError (std::string s, std::string m)
208                 : std::runtime_error (m)
209                 , _setting (s)
210         {}
211
212         virtual ~SettingError () throw () {}
213
214         /** @return name of setting in question */
215         std::string setting () const {
216                 return _setting;
217         }
218
219 private:
220         std::string _setting;
221 };
222
223
224 /** @class MissingSettingError.
225  *  @brief Indicates that a Film is missing a setting that is required for some operation.
226  */
227 class MissingSettingError : public SettingError
228 {
229 public:
230         /** @param s Name of setting that was required */
231         explicit MissingSettingError (std::string s);
232 };
233
234
235 /** @class BadSettingError
236  *  @brief Indicates that a setting is bad in some way.
237  */
238 class BadSettingError : public SettingError
239 {
240 public:
241         /** @param s Name of setting that is bad.
242          *  @param m Error message.
243          */
244         BadSettingError (std::string s, std::string m)
245                 : SettingError (s, m)
246         {}
247 };
248
249
250 /** @class NetworkError
251  *  @brief Indicates some problem with communication on the network.
252  */
253 class NetworkError : public std::runtime_error
254 {
255 public:
256         explicit NetworkError (std::string s, boost::optional<std::string> d = boost::optional<std::string>());
257
258         std::string summary () const {
259                 return _summary;
260         }
261
262         boost::optional<std::string> detail () const {
263                 return _detail;
264         }
265
266 private:
267         std::string _summary;
268         boost::optional<std::string> _detail;
269 };
270
271
272 /** @class KDMError
273  *  @brief A problem with a KDM.
274  */
275 class KDMError : public std::runtime_error
276 {
277 public:
278         KDMError (std::string s, std::string d);
279         ~KDMError () throw() {}
280
281         std::string summary () const {
282                 return _summary;
283         }
284
285         std::string detail () const {
286                 return _detail;
287         }
288
289 private:
290         std::string _summary;
291         std::string _detail;
292 };
293
294
295 /** @class PixelFormatError
296  *  @brief A problem with an unsupported pixel format.
297  */
298 class PixelFormatError : public std::runtime_error
299 {
300 public:
301         PixelFormatError (std::string o, AVPixelFormat f);
302 };
303
304
305 /** @class TextSubtitleError
306  *  @brief An error that occurs while parsing a TextSubtitleError file.
307  */
308 class TextSubtitleError : public FileError
309 {
310 public:
311         TextSubtitleError (std::string, std::string, boost::filesystem::path);
312 };
313
314
315 class DCPError : public std::runtime_error
316 {
317 public:
318         explicit DCPError (std::string s)
319                 : std::runtime_error (s)
320         {}
321 };
322
323
324 /** @class ProjectFolderError
325  *  @brief An attempt has been made to read a DCP from a directory, but it looks
326  *  like the directory actually contains a DCP-o-matic project.
327  */
328 class ProjectFolderError : public DCPError
329 {
330 public:
331         /* Code which catches this exception will provide their own message */
332         ProjectFolderError ()
333                 : DCPError ("dummy")
334         {}
335 };
336
337
338 class InvalidSignerError : public std::runtime_error
339 {
340 public:
341         InvalidSignerError ();
342         explicit InvalidSignerError (std::string reason);
343 };
344
345 class ProgrammingError : public std::runtime_error
346
347 {
348 public:
349         ProgrammingError (std::string file, int line, std::string message = "");
350 };
351
352
353 class TextEncodingError : public std::runtime_error
354 {
355 public:
356         explicit TextEncodingError (std::string s)
357                 : std::runtime_error (s)
358         {}
359 };
360
361
362 class MetadataError : public std::runtime_error
363 {
364 public:
365         explicit MetadataError (std::string s)
366                 : std::runtime_error (s)
367         {}
368 };
369
370
371 class OldFormatError : public std::runtime_error
372 {
373 public:
374         explicit OldFormatError (std::string s)
375                 : std::runtime_error (s)
376         {}
377 };
378
379
380 class KDMAsContentError : public std::runtime_error
381 {
382 public:
383         KDMAsContentError ();
384 };
385
386
387 class GLError : public std::runtime_error
388 {
389 public:
390         GLError (char const* last, int e);
391         GLError (char const* message);
392 };
393
394
395 /** @class CopyError
396  *  @brief An error which occurs when copying a DCP to a distribution drive.
397  */
398 class CopyError : public std::runtime_error
399 {
400 public:
401         CopyError (std::string s, boost::optional<int> n = boost::optional<int>());
402         virtual ~CopyError () throw () {}
403
404         std::string message () const {
405                 return _message;
406         }
407
408         boost::optional<int> number () const {
409                 return _number;
410         }
411
412 private:
413         std::string _message;
414         boost::optional<int> _number;
415 };
416
417
418 /** @class CommunicationFailedError
419  *  @brief Communication between dcpomatic2_disk and _disk_writer failed somehow.
420  */
421 class CommunicationFailedError : public CopyError
422 {
423 public:
424         CommunicationFailedError ();
425 };
426
427
428 /** @class VerifyError
429  *  @brief An error which occurs when verifying a DCP that we copied to a distribution drive.
430  */
431 class VerifyError : public std::runtime_error
432 {
433 public:
434         VerifyError (std::string s, int n);
435         virtual ~VerifyError () throw () {}
436
437         std::string message () const {
438                 return _message;
439         }
440
441         int number () const {
442                 return _number;
443         }
444
445 private:
446         std::string _message;
447         int _number;
448 };
449
450
451 class PrivilegeError : public std::runtime_error
452 {
453 public:
454         explicit PrivilegeError (std::string s)
455                         : std::runtime_error (s)
456                 {}
457 };
458
459
460 #endif