2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #ifndef DVDOMATIC_EXCEPTIONS_H
21 #define DVDOMATIC_EXCEPTIONS_H
23 /** @file src/exceptions.h
24 * @brief Our exceptions.
29 #include <boost/exception/all.hpp>
30 #include <boost/thread.hpp>
31 #include "compose.hpp"
33 /** @class StringError
34 * @brief A parent class for exceptions using messages held in a std::string
36 class StringError : public std::exception
39 /** @param w Error message */
40 StringError (std::string w) {
44 virtual ~StringError () throw () {}
46 /** @return error message */
47 char const * what () const throw () {
48 return _what.c_str ();
56 /** @class DecodeError
57 * @brief A low-level problem with the decoder (possibly due to the nature
60 class DecodeError : public StringError
63 DecodeError (std::string s)
68 /** @class EncodeError
69 * @brief A low-level problem with an encoder.
71 class EncodeError : public StringError
74 EncodeError (std::string s)
80 * @brief Parent class for file-related errors.
82 class FileError : public StringError
85 /** @param m Error message.
86 * @param f Name of the file that this exception concerns.
88 FileError (std::string m, std::string f)
93 virtual ~FileError () throw () {}
95 /** @return name of the file that this exception concerns */
96 std::string file () const {
101 /** name of the file that this exception concerns */
106 /** @class OpenFileError.
107 * @brief Indicates that some error occurred when trying to open a file.
109 class OpenFileError : public FileError
112 /** @param f File that we were trying to open */
113 OpenFileError (std::string f)
114 : FileError ("could not open file " + f, f)
118 /** @class CreateFileError.
119 * @brief Indicates that some error occurred when trying to create a file.
121 class CreateFileError : public FileError
124 /** @param f File that we were trying to create */
125 CreateFileError (std::string f)
126 : FileError ("could not create file " + f, f)
131 /** @class ReadFileError.
132 * @brief Indicates that some error occurred when trying to read from a file
134 class ReadFileError : public FileError
137 /** @param f File that we were trying to read from.
138 * @param e errno value, or 0.
140 ReadFileError (std::string f, int e = 0)
143 _what = String::compose ("could not read from file %1 (%2)", f, strerror (e));
147 /** @class WriteFileError.
148 * @brief Indicates that some error occurred when trying to write to a file
150 class WriteFileError : public FileError
153 /** @param f File that we were trying to write to.
154 * @param e errno value, or 0.
156 WriteFileError (std::string f, int e)
159 _what = String::compose ("could not write to file %1 (%2)", f, strerror (e));
163 /** @class SettingError.
164 * @brief Indicates that something is wrong with a setting.
166 class SettingError : public StringError
169 /** @param s Name of setting that was required.
172 SettingError (std::string s, std::string m)
177 virtual ~SettingError () throw () {}
179 /** @return name of setting in question */
180 std::string setting () const {
185 std::string _setting;
188 /** @class MissingSettingError.
189 * @brief Indicates that a Film is missing a setting that is required for some operation.
191 class MissingSettingError : public SettingError
194 /** @param s Name of setting that was required */
195 MissingSettingError (std::string s)
196 : SettingError (s, "missing required setting " + s)
200 /** @class BadSettingError
201 * @brief Indicates that a setting is bad in some way.
203 class BadSettingError : public SettingError
206 /** @param s Name of setting that is bad */
207 BadSettingError (std::string s, std::string m)
208 : SettingError (s, m)
212 /** @class NetworkError.
213 * @brief Indicates some problem with communication on the network.
215 class NetworkError : public StringError
218 NetworkError (std::string s)
226 bool thrown () const {
227 boost::mutex::scoped_lock lm (_mutex);
232 boost::mutex::scoped_lock lm (_mutex);
233 boost::rethrow_exception (_exception);
238 void store_current () {
239 boost::mutex::scoped_lock lm (_mutex);
240 _exception = boost::current_exception ();
244 boost::exception_ptr _exception;
245 mutable boost::mutex _mutex;