xapian-core  1.4.27
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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 
23 #include <xapian/error.h>
24 
25 #include "length.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  result += encode_length(e.get_context().length());
38  result += e.get_context();
39  result += encode_length(e.get_msg().length());
40  result += e.get_msg();
41  // The "error string" goes last so we don't need to store its length.
42  const char * err = e.get_error_string();
43  if (err) result += err;
44  return result;
45 }
46 
47 void
48 unserialise_error(const string &serialised_error, const string &prefix,
49  const string &new_context)
50 {
51  // Use c_str() so last string is nul-terminated.
52  const char * p = serialised_error.c_str();
53  const char * end = p + serialised_error.size();
54  if (p != end) {
55  char type = *p++;
56 
57  size_t len;
58  decode_length_and_check(&p, end, len);
59  string context(p, len);
60  p += len;
61 
62  decode_length_and_check(&p, end, len);
63  string msg(prefix);
64  msg.append(p, len);
65  p += len;
66 
67  const char * error_string = (p == end) ? NULL : p;
68 
69  if (!new_context.empty()) {
70  if (!context.empty()) {
71  msg += "; context was: ";
72  msg += context;
73  }
74  context = new_context;
75  }
76 
77  switch (type) {
78 #include "xapian/errordispatch.h"
79  }
80  }
81 
82  throw Xapian::InternalError("Unknown remote exception type", new_context);
83 }
length encoded as a string
const std::string & get_msg() const
Message giving details of the error, intended for human consumption.
Definition: error.h:122
STL namespace.
string serialise_error(const Xapian::Error &e)
Serialise a Xapian::Error object to a string.
const char * get_type() const
The type of this error (e.g. "DocNotFoundError".)
Definition: error.h:117
std::string encode_length(T len)
Encode a length as a variable-length string.
Definition: length.h:36
Hierarchy of classes which Xapian can throw as exceptions.
void unserialise_error(const string &serialised_error, const string &prefix, const string &new_context)
Unserialise a Xapian::Error object and throw it.
InternalError indicates a runtime problem of some sort.
Definition: error.h:761
void decode_length_and_check(const char **p, const char *end, unsigned &out)
Decode a length encoded by encode_length.
Definition: length.cc:112
const char * get_error_string() const
Returns any system error string associated with this exception.
Definition: error.cc:50
All exceptions thrown by Xapian are subclasses of Xapian::Error.
Definition: error.h:43
const std::string & get_context() const
Optional context information.
Definition: error.h:133
functions to convert classes to strings and back