xapian-core  2.0.0
serialise-error.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2006,2007,2008,2009,2010,2011,2014,2015,2016 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 
23 #include <xapian/error.h>
24 
25 #include "pack.h"
26 #include "serialise-error.h"
27 
28 #include <string>
29 
30 using namespace std;
31 
32 string
34 {
35  // The byte before the type name is the type code.
36  string result(1, (e.get_type())[-1]);
37  pack_string(result, e.get_context());
38  pack_string(result, e.get_msg());
39  // The "error string" goes last so we don't need to store its length.
40  const char * err = e.get_error_string();
41  if (err) result += err;
42  return result;
43 }
44 
45 void
46 unserialise_error(const string &serialised_error, const string &prefix,
47  const string &new_context)
48 {
49  // Use c_str() so last string is nul-terminated.
50  const char * p = serialised_error.c_str();
51  const char * end = p + serialised_error.size();
52  if (p != end) {
53  char type = *p++;
54 
55  string context;
56  string msg(prefix);
57  if (!unpack_string(&p, end, context) ||
58  !unpack_string_append(&p, end, msg)) {
60  }
61 
62  const char * error_string = (p == end) ? NULL : p;
63 
64  if (!new_context.empty()) {
65  if (!context.empty()) {
66  msg += "; context was: ";
67  msg += context;
68  }
69  context = new_context;
70  }
71 
72  switch (type) {
73 #include "xapian/errordispatch.h"
74  }
75  }
76 
77  throw Xapian::InternalError("Unknown remote exception type", new_context);
78 }
All exceptions thrown by Xapian are subclasses of Xapian::Error.
Definition: error.h:41
const char * get_error_string() const
Returns any system error string associated with this exception.
Definition: error.cc:50
const std::string & get_msg() const noexcept
Message giving details of the error, intended for human consumption.
Definition: error.h:111
const char * get_type() const noexcept
The type of this error (e.g. "DocNotFoundError".)
Definition: error.h:106
const std::string & get_context() const noexcept
Optional context information.
Definition: error.h:121
InternalError indicates a runtime problem of some sort.
Definition: error.h:749
PositionList * p
Hierarchy of classes which Xapian can throw as exceptions.
void unpack_throw_serialisation_error(const char *p)
Throw appropriate SerialisationError.
Definition: pack.cc:29
Pack types into strings and unpack them again.
bool unpack_string(const char **p, const char *end, std::string &result)
Decode a std::string from a string.
Definition: pack.h:468
bool unpack_string_append(const char **p, const char *end, std::string &result)
Decode a std::string from a string and append.
Definition: pack.h:493
void pack_string(std::string &s, std::string_view value)
Append an encoded std::string to a string.
Definition: pack.h:442
void unserialise_error(const string &serialised_error, const string &prefix, const string &new_context)
Unserialise a Xapian::Error object and throw it.
string serialise_error(const Xapian::Error &e)
Serialise a Xapian::Error object to a string.
functions to convert classes to strings and back