libwreport 2.1
|
00001 /* 00002 * wreport/error - wreport exceptions 00003 * 00004 * Copyright (C) 2005--2010 ARPA-SIM <urpsim@smr.arpa.emr.it> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 * 00019 * Author: Enrico Zini <enrico@enricozini.com> 00020 */ 00021 00022 #ifndef WREPORT_ERROR_H 00023 #define WREPORT_ERROR_H 00024 00025 #include <stdexcept> 00026 #include <string> 00027 00040 namespace wreport { 00041 00043 enum ErrorCode { 00045 WR_ERR_NONE = 0, 00047 WR_ERR_NOTFOUND = 1, 00049 WR_ERR_TYPE = 2, 00051 WR_ERR_ALLOC = 3, 00053 WR_ERR_ODBC = 4, 00055 WR_ERR_HANDLES = 5, 00057 WR_ERR_TOOLONG = 6, 00059 WR_ERR_SYSTEM = 7, 00061 WR_ERR_CONSISTENCY = 8, 00063 WR_ERR_PARSE = 9, 00065 WR_ERR_WRITE = 10, 00067 WR_ERR_REGEX = 11, 00069 WR_ERR_UNIMPLEMENTED = 12, 00071 WR_ERR_DOMAIN = 13 00072 }; 00073 00078 #define WREPORT_THROWF_ATTRS(a, b) __attribute__ ((noreturn, format(printf, a, b))) 00079 00081 struct error : public std::exception 00082 { 00088 virtual ErrorCode code() const throw () = 0; 00089 00091 virtual const char* what() const throw () = 0; 00092 00094 static const char* strerror(ErrorCode code); 00095 }; 00096 00098 struct error_notfound : public error 00099 { 00100 std::string msg; 00101 00103 error_notfound(const std::string& msg) : msg(msg) {} 00104 ~error_notfound() throw () {} 00105 00106 ErrorCode code() const throw () { return WR_ERR_NOTFOUND; } 00107 00108 virtual const char* what() const throw () { return msg.c_str(); } 00109 00111 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2); 00112 }; 00113 00118 struct error_type : public error 00119 { 00120 std::string msg; 00121 00123 error_type(const std::string& msg) : msg(msg) {} 00124 ~error_type() throw () {} 00125 00126 ErrorCode code() const throw () { return WR_ERR_TYPE; } 00127 00128 virtual const char* what() const throw () { return msg.c_str(); } 00129 00131 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2); 00132 }; 00133 00135 struct error_alloc : public error 00136 { 00137 const char* msg; 00138 00145 error_alloc(const char* msg) : msg(msg) {} 00146 ~error_alloc() throw () {} 00147 00148 ErrorCode code() const throw () { return WR_ERR_ALLOC; } 00149 00151 virtual const char* what() const throw () { return msg; } 00152 }; 00153 00159 struct error_handles : public error 00160 { 00161 std::string msg; 00162 00164 error_handles(const std::string& msg) : msg(msg) {} 00165 ~error_handles() throw () {} 00166 00167 ErrorCode code() const throw () { return WR_ERR_HANDLES; } 00168 00169 virtual const char* what() const throw () { return msg.c_str(); } 00170 00172 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2); 00173 }; 00174 00176 struct error_toolong : public error 00177 { 00178 std::string msg; 00179 00181 error_toolong(const std::string& msg) : msg(msg) {} 00182 ~error_toolong() throw () {} 00183 00184 ErrorCode code() const throw () { return WR_ERR_TOOLONG; } 00185 00186 virtual const char* what() const throw () { return msg.c_str(); } 00187 00189 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2); 00190 }; 00191 00196 struct error_system : public error 00197 { 00198 std::string msg; 00199 00205 error_system(const std::string& msg); 00213 error_system(const std::string& msg, int errno_val); 00214 ~error_system() throw () {} 00215 00216 ErrorCode code() const throw () { return WR_ERR_SYSTEM; } 00217 00218 virtual const char* what() const throw () { return msg.c_str(); } 00219 00221 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2); 00222 }; 00223 00225 struct error_consistency : public error 00226 { 00227 std::string msg; 00228 00230 error_consistency(const std::string& msg) : msg(msg) {}; 00231 ~error_consistency() throw () {} 00232 00233 ErrorCode code() const throw () { return WR_ERR_CONSISTENCY; } 00234 00235 virtual const char* what() const throw () { return msg.c_str(); } 00236 00238 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2); 00239 }; 00240 00242 struct error_parse : public error 00243 { 00244 std::string msg; 00245 00247 error_parse(const std::string& msg) : msg(msg) {} 00256 error_parse(const char* file, int line, const std::string& msg); 00257 ~error_parse() throw () {} 00258 00259 ErrorCode code() const throw () { return WR_ERR_PARSE; } 00260 00261 virtual const char* what() const throw () { return msg.c_str(); } 00262 00264 static void throwf(const char* file, int line, const char* fmt, ...) WREPORT_THROWF_ATTRS(3, 4); 00265 }; 00266 00268 struct error_regexp : public error 00269 { 00270 std::string msg; 00271 00281 error_regexp(int code, void* re, const std::string& msg); 00282 ~error_regexp() throw () {} 00283 00284 ErrorCode code() const throw () { return WR_ERR_REGEX; } 00285 00286 virtual const char* what() const throw () { return msg.c_str(); } 00287 00289 static void throwf(int code, void* re, const char* fmt, ...) WREPORT_THROWF_ATTRS(3, 4); 00290 }; 00291 00293 struct error_unimplemented : public error 00294 { 00295 std::string msg; 00296 00298 error_unimplemented(const std::string& msg) : msg(msg) {}; 00299 ~error_unimplemented() throw () {} 00300 00301 ErrorCode code() const throw () { return WR_ERR_UNIMPLEMENTED; } 00302 00303 virtual const char* what() const throw () { return msg.c_str(); } 00304 00306 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2); 00307 }; 00308 00310 struct error_domain : public error 00311 { 00312 std::string msg; 00313 00315 error_domain(const std::string& msg) : msg(msg) {} 00316 ~error_domain() throw () {} 00317 00318 ErrorCode code() const throw () { return WR_ERR_DOMAIN; } 00319 00320 virtual const char* what() const throw () { return msg.c_str(); } 00321 00323 static void throwf(const char* fmt, ...) WREPORT_THROWF_ATTRS(1, 2); 00324 }; 00325 00326 } 00327 00328 /* vim:set ts=4 sw=4: */ 00329 #endif