TODO.
[dcpomatic.git] / src / lib / exceptions.h
1 /*
2     Copyright (C) 2012 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/exceptions.h
21  *  @brief Our exceptions.
22  */
23
24 #include <stdexcept>
25 #include <sstream>
26 #include <cstring>
27
28 /** @class StringError
29  *  @brief A parent class for exceptions using messages held in a std::string
30  */
31 class StringError : public std::exception
32 {
33 public:
34         /** @param w Error message */
35         StringError (std::string w) {
36                 _what = w;
37         }
38
39         virtual ~StringError () throw () {}
40
41         /** @return error message */
42         char const * what () const throw () {
43                 return _what.c_str ();
44         }
45
46 protected:
47         /** error message */
48         std::string _what;
49 };
50
51 /** @class DecodeError
52  *  @brief A low-level problem with the decoder (possibly due to the nature
53  *  of a source file).
54  */
55 class DecodeError : public StringError
56 {
57 public:
58         DecodeError (std::string s)
59                 : StringError (s)
60         {}
61 };
62
63 /** @class EncodeError
64  *  @brief A low-level problem with an encoder.
65  */
66 class EncodeError : public StringError
67 {
68 public:
69         EncodeError (std::string s)
70                 : StringError (s)
71         {}
72 };
73
74 /** @class FileError.
75  *  @brief Parent class for file-related errors.
76  */
77 class FileError : public StringError
78 {
79 public:
80         FileError (std::string m, std::string f)
81                 : StringError (m)
82                 , _file (f)
83         {}
84
85         virtual ~FileError () throw () {}
86
87         std::string file () const {
88                 return _file;
89         }
90
91 private:
92         std::string _file;
93 };
94         
95
96 /** @class OpenFileError.
97  *  @brief Indicates that some error occurred when trying to open a file.
98  */
99 class OpenFileError : public FileError
100 {
101 public:
102         /** @param f File that we were trying to open */
103         OpenFileError (std::string f)
104                 : FileError ("could not open file " + f, f)
105         {}
106 };
107
108 /** @class CreateFileError.
109  *  @brief Indicates that some error occurred when trying to create a file.
110  */
111 class CreateFileError : public FileError
112 {
113 public:
114         /** @param f File that we were trying to create */
115         CreateFileError (std::string f)
116                 : FileError ("could not create file " + f, f)
117         {}
118 };
119
120
121 /** @class ReadFileError.
122  *  @brief Indicates that some error occurred when trying to read from a file
123  */
124 class ReadFileError : public FileError
125 {
126 public:
127         /** @param f File that we were trying to read from.
128          *  @param e errno value, or 0.
129          */
130         ReadFileError (std::string f, int e = 0)
131                 : FileError ("", f)
132         {
133                 std::stringstream s;
134                 s << "could not read from file " << f;
135                 if (e) {
136                         s << " (" << strerror (e) << ")";
137                 }
138                 _what = s.str ();
139         }
140 };
141
142 /** @class WriteFileError.
143  *  @brief Indicates that some error occurred when trying to write to a file
144  */
145 class WriteFileError : public FileError
146 {
147 public:
148         /** @param f File that we were trying to write to.
149          *  @param e errno value, or 0.
150          */
151         WriteFileError (std::string f, int e)
152                 : FileError ("", f)
153         {
154                 std::stringstream s;
155                 s << "could not write to file " << f;
156                 if (e) {
157                         s << " (" << strerror (e) << ")";
158                 }
159                 _what = s.str ();
160         }
161 };
162
163 /** @class SettingError.
164  *  @brief Indicates that something is wrong with a setting.
165  */
166 class SettingError : public StringError
167 {
168 public:
169         /** @param s Name of setting that was required.
170          *  @param m Message.
171          */
172         SettingError (std::string s, std::string m)
173                 : StringError (m)
174                 , _setting (s)
175         {}
176
177         virtual ~SettingError () throw () {}
178
179         /** @return name of setting in question */
180         std::string setting () const {
181                 return _setting;
182         }
183
184 private:
185         std::string _setting;
186 };
187
188 /** @class MissingSettingError.
189  *  @brief Indicates that a Film is missing a setting that is required for some operation.
190  */
191 class MissingSettingError : public SettingError
192 {
193 public:
194         /** @param s Name of setting that was required */
195         MissingSettingError (std::string s)
196                 : SettingError (s, "missing required setting " + s)
197         {}
198 };
199
200 /** @class BadSettingError
201  *  @brief Indicates that a setting is bad in some way.
202  */
203 class BadSettingError : public SettingError
204 {
205 public:
206         /** @param s Name of setting that is bad */
207         BadSettingError (std::string s, std::string m)
208                 : SettingError (s, m)
209         {}
210 };
211
212 /** @class NetworkError.
213  *  @brief Indicates some problem with communication on the network.
214  */
215 class NetworkError : public StringError
216 {
217 public:
218         NetworkError (std::string s)
219                 : StringError (s)
220         {}
221 };
222
223 class PlayError : public StringError
224 {
225 public:
226         PlayError (std::string s)
227                 : StringError (s)
228         {}
229 };
230
231 class DVDError : public StringError
232 {
233 public:
234         DVDError (std::string s)
235                 : StringError (s)
236         {}
237 };