error.h

Go to the documentation of this file.
00001 /*
00002  *
00003  * C++ sphinx search client library
00004  * Copyright (C) 2007  Seznam.cz, a.s.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  * Seznam.cz, a.s.
00021  * Radlicka 2, Praha 5, 15000, Czech Republic
00022  * http://www.seznam.cz, mailto:sphinxclient@firma.seznam.cz
00023  *
00024  *
00025  * $Id: error.h 13 2009-06-01 12:36:51Z honkir $
00026  *
00027  * DESCRIPTION
00028  * SphinxClient header file - error handling
00029  *
00030  * AUTHOR
00031  * Jan Kirschner <jan.kirschner@firma.seznam.cz>
00032  *
00033  * HISTORY
00034  * 2006-01-03 (jan.kirschner)
00035  *            First draft.
00036  */
00037 
00039 
00040 #ifndef __SPHINXERROR_H__
00041 #define __SPHINXERROR_H__
00042 
00043 #include <string>
00044 #include <exception>
00045 
00046 #include <sstream>
00047 #include <errno.h>
00048 #include <string.h>
00049 namespace Sphinx
00050 {
00051 
00052 // --------------------- exception handling ---------------------------
00053 
00063 enum ErrorType_t {
00064     STATUS_OK = 0,        
00065     CONNECTION_ERROR = 1, 
00066     SERVER_ERROR = 2,   
00067     MESSAGE_ERROR = 3,    
00068     VALUE_TYPE_ERROR = 4,  
00069     CLIENT_USAGE_ERROR = 5 
00070 };
00071 
00080 struct Error_t : public std::exception
00081 {
00082     std::string errMsg;
00083     ErrorType_t errCode;
00084 
00085     Error_t(ErrorType_t err, const std::string &msg)
00086                        :std::exception(),errMsg(msg),errCode(err)
00087     {}
00088 
00089     virtual const char* what() const throw() {
00090         return errMsg.c_str();
00091     }
00092 
00093     virtual ~Error_t() throw(){}
00094 };//struct
00095 
00096 
00105 struct Warning_t : public std::exception
00106 {
00107     std::string errMsg;
00108 
00109     Warning_t(const std::string &msg) : std::exception() ,errMsg(msg)
00110     {}
00111 
00112     virtual const char* what() const throw() {
00113         return errMsg.c_str();
00114     }
00115 
00116     virtual ~Warning_t() throw(){}
00117 };//struct
00118 
00119 
00125 struct ServerError_t : public Error_t
00126 {
00127     ServerError_t(const std::string &msg)
00128                        :Error_t(SERVER_ERROR, msg) {}
00129 };//struct
00130 
00136 struct MessageError_t : public Error_t
00137 {
00138     MessageError_t(const std::string &msg)
00139                        :Error_t(MESSAGE_ERROR, msg) {}
00140 };//struct
00141 
00147 struct ConnectionError_t : public Error_t
00148 {
00149     ConnectionError_t(const std::string &msg)
00150                        :Error_t(CONNECTION_ERROR, msg) {}
00151 };//struct
00152 
00153 
00159 struct ValueTypeError_t : public Error_t
00160 {
00161     ValueTypeError_t(const std::string &msg)
00162                        :Error_t(VALUE_TYPE_ERROR, msg) {}
00163 };//struct
00164 
00165 
00171 struct ClientUsageError_t : public Error_t
00172 {
00173     ClientUsageError_t(const std::string &msg)
00174                        :Error_t(CLIENT_USAGE_ERROR, msg) {}
00175 };//struct
00176 
00177 
00178 inline std::string strError(const char *msg, int errNo=0) {
00179         std::ostringstream errMsg;
00180         errMsg << msg
00181                << ": "
00182                << (errNo == 0 ? strerror(errno) : strerror(errNo));
00183         return errMsg.str();
00184 }
00185 
00186 }
00187 
00188 #endif
00189