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