00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022
00023 #include <xapian/error.h>
00024
00025 #include "safeerrno.h"
00026 #ifdef __WIN32__
00027 # include "safewindows.h"
00028 #else
00029 # include <netdb.h>
00030 #endif
00031
00032 #include <cstdio>
00033 #include <cstring>
00034
00035 #include "str.h"
00036
00037 using namespace std;
00038
00039 Xapian::Error::Error(const std::string &msg_, const std::string &context_,
00040 const char * type_, const char * error_string_)
00041 : msg(msg_), context(context_), type(type_), my_errno(0),
00042 error_string(), already_handled(false)
00043 {
00044 if (error_string_) error_string.assign(error_string_);
00045 }
00046
00047 const char *
00048 Xapian::Error::get_error_string() const
00049 {
00050 if (!error_string.empty()) return error_string.c_str();
00051 if (my_errno == 0) return NULL;
00052 if (my_errno > 0) {
00053 error_string.assign(strerror(my_errno));
00054 return error_string.c_str();
00055 }
00056 #ifdef __WIN32__
00057 DWORD len;
00058 char * error;
00059 len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ALLOCATE_BUFFER,
00060 0, -my_errno, 0, (CHAR*)&error, 0, 0);
00061 if (error) {
00062
00063 if (len >= 2 && error[len - 2] == '\r' && error[len - 1] == '\n')
00064 len -= 2;
00065 error_string.assign(error, len);
00066 LocalFree(error);
00067 return error_string.c_str();
00068 }
00069 #else
00070 # ifdef HAVE_HSTRERROR
00071 error_string.assign(hstrerror(-my_errno));
00072 return error_string.c_str();
00073 # else
00074 const char * s = NULL;
00075 switch (-my_errno) {
00076 case HOST_NOT_FOUND:
00077 s = "Unknown host";
00078 break;
00079 case NO_ADDRESS:
00080 # if NO_ADDRESS != NO_DATA
00081 case NO_DATA:
00082 # endif
00083 s = "No address associated with name";
00084 break;
00085 case NO_RECOVERY:
00086 s = "Unknown server error";
00087 break;
00088 case TRY_AGAIN:
00089 s = "Host name lookup failure";
00090 break;
00091 }
00092 if (s) {
00093 error_string.assign(s);
00094 return error_string.c_str();
00095 }
00096 # endif
00097 #endif
00098
00099 #ifndef HAVE_HSTRERROR
00100 error_string = "Unknown Error ";
00101 error_string += str(-my_errno);
00102 return error_string.c_str();
00103 #endif
00104 }
00105
00106 string
00107 Xapian::Error::get_description() const
00108 {
00109 string desc(type);
00110 desc += ": ";
00111 desc += msg;
00112 if (!context.empty()) {
00113 desc += " (context: ";
00114 desc += context;
00115 desc += ')';
00116 }
00117 const char *e = get_error_string();
00118 if (e) {
00119 desc += " (";
00120 desc += e;
00121 desc += ')';
00122 }
00123 return desc;
00124 }