2 * @brief Xapian::Error base class.
4 /* Copyright (C) 2007,2008,2011,2013,2014,2015 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <xapian/error.h>
25 #include "safeerrno.h"
27 # include "safewindows.h"
29 # include "safenetdb.h"
32 #include <cstdlib> // For abs().
33 #include <cstring> // For memcmp().
35 #include "errno_to_string.h"
37 #include "unicode/description_append.h"
41 Xapian::Error::Error(const std::string
&msg_
, const std::string
&context_
,
42 const char * type_
, const char * error_string_
)
43 : msg(msg_
), context(context_
), error_string(), type(type_
),
44 my_errno(0), already_handled(false)
46 if (error_string_
) error_string
.assign(error_string_
);
50 Xapian::Error::get_error_string() const
52 if (error_string
.empty()) {
53 if (my_errno
== 0) return NULL
;
55 if (my_errno
< 0 || my_errno
>= WSABASEERR
) {
56 int e
= abs(my_errno
);
59 len
= FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
|
60 FORMAT_MESSAGE_ALLOCATE_BUFFER
,
61 0, e
, 0, (CHAR
*)&error
, 0, 0);
63 // Remove any trailing \r\n from output of FormatMessage.
64 if (len
>= 2 && memcmp(error
+ len
- 2, "\r\n", 2) == 0)
66 error_string
.assign(error
, len
);
69 error_string
= "Unknown Error ";
70 error_string
+= str(e
);
73 errno_to_string(my_errno
, error_string
);
77 errno_to_string(my_errno
, error_string
);
79 // POSIX says only that EAI_* constants are "non-zero" - they're
80 // negative on Linux, but we allow for them being positive. We
81 // check they all that the same sign in net/remoteconnection.h.
83 error_string
.assign(gai_strerror(-my_errno
));
85 error_string
.assign(gai_strerror(my_errno
));
89 return error_string
.c_str();
93 Xapian::Error::get_description() const
95 string
desc(get_type());
98 if (!context
.empty()) {
99 desc
+= " (context: ";
100 description_append(desc
, context
);
103 const char *e
= get_error_string();
106 description_append(desc
, e
);