summaryrefslogtreecommitdiff
path: root/src/lib/exceptions.h
blob: 268e8c36d200771d84e291ccd90e8f215da54c29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
    Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/** @file  src/lib/exceptions.h
 *  @brief Our exceptions.
 */

#ifndef DCPOMATIC_EXCEPTIONS_H
#define DCPOMATIC_EXCEPTIONS_H

#include <boost/thread.hpp>
extern "C" {
#include <libavutil/pixfmt.h>
}
#include <boost/exception/all.hpp>
#include <boost/filesystem.hpp>
#include <stdexcept>
#include <cstring>

/** @class StringError
 *  @brief A parent class for exceptions using messages held in a std::string
 */
class StringError : public std::exception
{
public:
	/** @param w Error message */
	StringError (std::string w)
		: _what (w)
	{}

	virtual ~StringError () throw () {}

	/** @return error message */
	char const * what () const throw () {
		return _what.c_str ();
	}

protected:
	/** error message */
	std::string _what;
};

/** @class DecodeError
 *  @brief A low-level problem with the decoder (possibly due to the nature
 *  of a source file).
 */
class DecodeError : public StringError
{
public:
	DecodeError (std::string s)
		: StringError (s)
	{}
};

/** @class EncodeError
 *  @brief A low-level problem with an encoder.
 */
class EncodeError : public StringError
{
public:
	EncodeError (std::string s)
		: StringError (s)
	{}
};

/** @class FileError.
 *  @brief Parent class for file-related errors.
 */
class FileError : public StringError
{
public:
	/** @param m Error message.
	 *  @param f Name of the file that this exception concerns.
	 */
	FileError (std::string m, boost::filesystem::path f)
		: StringError (m)
		, _file (f)
	{}

	virtual ~FileError () throw () {}

	/** @return name of the file that this exception concerns */
	boost::filesystem::path file () const {
		return _file;
	}

private:
	/** name of the file that this exception concerns */
	boost::filesystem::path _file;
};

class JoinError : public StringError
{
public:
	JoinError (std::string s)
		: StringError (s)
	{}
};

/** @class OpenFileError.
 *  @brief Indicates that some error occurred when trying to open a file.
 */
class OpenFileError : public FileError
{
public:
	/** @param f File that we were trying to open */
	OpenFileError (boost::filesystem::path f);
};

/** @class CreateFileError.
 *  @brief Indicates that some error occurred when trying to create a file.
 */
class CreateFileError : public FileError
{
public:
	/** @param f File that we were trying to create */
	CreateFileError (boost::filesystem::path f);
};


/** @class ReadFileError.
 *  @brief Indicates that some error occurred when trying to read from a file
 */
class ReadFileError : public FileError
{
public:
	/** @param f File that we were trying to read from.
	 *  @param e errno value, or 0.
	 */
	ReadFileError (boost::filesystem::path f, int e = 0);
};

/** @class WriteFileError.
 *  @brief Indicates that some error occurred when trying to write to a file
 */
class WriteFileError : public FileError
{
public:
	/** @param f File that we were trying to write to.
	 *  @param e errno value, or 0.
	 */
	WriteFileError (boost::filesystem::path f, int e);
};

/** @class SettingError.
 *  @brief Indicates that something is wrong with a setting.
 */
class SettingError : public StringError
{
public:
	/** @param s Name of setting that was required.
	 *  @param m Message.
	 */
	SettingError (std::string s, std::string m)
		: StringError (m)
		, _setting (s)
	{}

	virtual ~SettingError () throw () {}

	/** @return name of setting in question */
	std::string setting () const {
		return _setting;
	}

private:
	std::string _setting;
};

/** @class MissingSettingError.
 *  @brief Indicates that a Film is missing a setting that is required for some operation.
 */
class MissingSettingError : public SettingError
{
public:
	/** @param s Name of setting that was required */
	MissingSettingError (std::string s);
};

/** @class BadSettingError
 *  @brief Indicates that a setting is bad in some way.
 */
class BadSettingError : public SettingError
{
public:
	/** @param s Name of setting that is bad */
	BadSettingError (std::string s, std::string m)
		: SettingError (s, m)
	{}
};

/** @class NetworkError
 *  @brief Indicates some problem with communication on the network.
 */
class NetworkError : public StringError
{
public:
	NetworkError (std::string s)
		: StringError (s)
	{}
};

/** @class KDMError
 *  @brief A problem with a KDM.
 */
class KDMError : public StringError
{
public:
	KDMError (std::string s)
		: StringError (s)
	{}
};

/** @class PixelFormatError
 *  @brief A problem with an unsupported pixel format.
 */
class PixelFormatError : public StringError
{
public:
	PixelFormatError (std::string o, AVPixelFormat f);
};

/** @class SubRipError
 *  @brief An error that occurs while parsing a SubRip file.
 */
class SubRipError : public FileError
{
public:
	SubRipError (std::string, std::string, boost::filesystem::path);
};

class DCPError : public StringError
{
public:
	DCPError (std::string s)
		: StringError (s)
	{}
};

class InvalidSignerError : public StringError
{
public:
	InvalidSignerError ();
};

class ProgrammingError : public StringError
{
public:
	ProgrammingError (std::string file, int line);
};

/** @class ExceptionStore
 *  @brief A parent class for classes which have a need to catch and
 *  re-throw exceptions.
 *
 *  This is intended for classes which run their own thread; they should do
 *  something like
 *
 *  void my_thread ()
 *  try {
 *    // do things which might throw exceptions
 *  } catch (...) {
 *    store_current ();
 *  }
 *
 *  and then in another thread call rethrow().  If any
 *  exception was thrown by my_thread it will be stored by
 *  store_current() and then rethrow() will re-throw it where
 *  it can be handled.
 */
class ExceptionStore
{
public:
	void rethrow () {
		boost::mutex::scoped_lock lm (_mutex);
		if (_exception) {
			boost::exception_ptr tmp = _exception;
			_exception = boost::exception_ptr ();
			boost::rethrow_exception (tmp);
		}
	}

protected:

	void store_current () {
		boost::mutex::scoped_lock lm (_mutex);
		_exception = boost::current_exception ();
	}

private:
	boost::exception_ptr _exception;
	mutable boost::mutex _mutex;
};

#endif