Give a better error when opening a DCP with File -> Open by mistake (#1723).
[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 <stdexcept>
34 #include <cstring>
35
36 /** @class DecodeError
37  *  @brief A low-level problem with the decoder (possibly due to the nature
38  *  of a source file).
39  */
40 class DecodeError : public std::runtime_error
41 {
42 public:
43         explicit DecodeError (std::string s)
44                 : std::runtime_error (s)
45         {}
46 };
47
48 class CryptoError : public std::runtime_error
49 {
50 public:
51         explicit CryptoError (std::string s)
52                 : std::runtime_error (s)
53         {}
54 };
55
56 /** @class EncodeError
57  *  @brief A low-level problem with an encoder.
58  */
59 class EncodeError : public std::runtime_error
60 {
61 public:
62         explicit EncodeError (std::string s)
63                 : std::runtime_error (s)
64         {}
65 };
66
67 /** @class FileError.
68  *  @brief Parent class for file-related errors.
69  */
70 class FileError : public std::runtime_error
71 {
72 public:
73         /** @param m Error message.
74          *  @param f Name of the file that this exception concerns.
75          */
76         FileError (std::string m, boost::filesystem::path f)
77                 : std::runtime_error (String::compose("%1 with %2", m, f.string()))
78                 , _file (f)
79         {}
80
81         virtual ~FileError () throw () {}
82
83         /** @return name of the file that this exception concerns */
84         boost::filesystem::path file () const {
85                 return _file;
86         }
87
88 private:
89         /** name of the file that this exception concerns */
90         boost::filesystem::path _file;
91 };
92
93 class JoinError : public std::runtime_error
94 {
95 public:
96         explicit JoinError (std::string s)
97                 : std::runtime_error (s)
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         enum Mode {
108                 READ,
109                 WRITE,
110                 READ_WRITE
111         };
112
113         /** @param f File that we were trying to open.
114          *  @param error Code of error that occurred.
115          *  @param mode Mode that we tried to open the file in.
116          */
117         OpenFileError (boost::filesystem::path f, int error, Mode mode);
118 };
119
120 class FileNotFoundError : public std::runtime_error
121 {
122 public:
123         FileNotFoundError (boost::filesystem::path f);
124         virtual ~FileNotFoundError () throw () {}
125
126         /** @return name of the file that this exception concerns */
127         boost::filesystem::path file () const {
128                 return _file;
129         }
130
131 private:
132         /** name of the file that this exception concerns */
133         boost::filesystem::path _file;
134 };
135
136 /** @class ReadFileError.
137  *  @brief Indicates that some error occurred when trying to read from a file
138  */
139 class ReadFileError : public FileError
140 {
141 public:
142         /** @param f File that we were trying to read from.
143          *  @param e errno value, or 0.
144          */
145         ReadFileError (boost::filesystem::path f, int e = 0);
146 };
147
148 /** @class WriteFileError.
149  *  @brief Indicates that some error occurred when trying to write to a file
150  */
151 class WriteFileError : public FileError
152 {
153 public:
154         /** @param f File that we were trying to write to.
155          *  @param e errno value, or 0.
156          */
157         WriteFileError (boost::filesystem::path f, int e);
158 };
159
160 /** @class SettingError.
161  *  @brief Indicates that something is wrong with a setting.
162  */
163 class SettingError : public std::runtime_error
164 {
165 public:
166         /** @param s Name of setting that was required.
167          *  @param m Message.
168          */
169         SettingError (std::string s, std::string m)
170                 : std::runtime_error (m)
171                 , _setting (s)
172         {}
173
174         virtual ~SettingError () throw () {}
175
176         /** @return name of setting in question */
177         std::string setting () const {
178                 return _setting;
179         }
180
181 private:
182         std::string _setting;
183 };
184
185 /** @class MissingSettingError.
186  *  @brief Indicates that a Film is missing a setting that is required for some operation.
187  */
188 class MissingSettingError : public SettingError
189 {
190 public:
191         /** @param s Name of setting that was required */
192         explicit MissingSettingError (std::string s);
193 };
194
195 /** @class BadSettingError
196  *  @brief Indicates that a setting is bad in some way.
197  */
198 class BadSettingError : public SettingError
199 {
200 public:
201         /** @param s Name of setting that is bad.
202          *  @param m Error message.
203          */
204         BadSettingError (std::string s, std::string m)
205                 : SettingError (s, m)
206         {}
207 };
208
209 /** @class NetworkError
210  *  @brief Indicates some problem with communication on the network.
211  */
212 class NetworkError : public std::runtime_error
213 {
214 public:
215         explicit NetworkError (std::string s)
216                 : std::runtime_error (s)
217         {}
218 };
219
220 /** @class KDMError
221  *  @brief A problem with a KDM.
222  */
223 class KDMError : public std::runtime_error
224 {
225 public:
226         KDMError (std::string s, std::string d);
227         ~KDMError () throw() {}
228
229         std::string summary () const {
230                 return _summary;
231         }
232
233         std::string detail () const {
234                 return _detail;
235         }
236
237 private:
238         std::string _summary;
239         std::string _detail;
240 };
241
242 /** @class PixelFormatError
243  *  @brief A problem with an unsupported pixel format.
244  */
245 class PixelFormatError : public std::runtime_error
246 {
247 public:
248         PixelFormatError (std::string o, AVPixelFormat f);
249 };
250
251 /** @class TextSubtitleError
252  *  @brief An error that occurs while parsing a TextSubtitleError file.
253  */
254 class TextSubtitleError : public FileError
255 {
256 public:
257         TextSubtitleError (std::string, std::string, boost::filesystem::path);
258 };
259
260 class DCPError : public std::runtime_error
261 {
262 public:
263         explicit DCPError (std::string s)
264                 : std::runtime_error (s)
265         {}
266 };
267
268 class InvalidSignerError : public std::runtime_error
269 {
270 public:
271         InvalidSignerError ();
272         explicit InvalidSignerError (std::string reason);
273 };
274
275 class ProgrammingError : public std::runtime_error
276 {
277 public:
278         ProgrammingError (std::string file, int line, std::string message = "");
279 };
280
281 class TextEncodingError : public std::runtime_error
282 {
283 public:
284         explicit TextEncodingError (std::string s)
285                 : std::runtime_error (s)
286         {}
287 };
288
289 class MetadataError : public std::runtime_error
290 {
291 public:
292         explicit MetadataError (std::string s)
293                 : std::runtime_error (s)
294         {}
295 };
296
297 class OldFormatError : public std::runtime_error
298 {
299 public:
300         explicit OldFormatError (std::string s)
301                 : std::runtime_error (s)
302         {}
303 };
304
305 class KDMAsContentError : public std::runtime_error
306 {
307 public:
308         KDMAsContentError ();
309 };
310
311 #endif