-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathError.hpp
81 lines (68 loc) · 1.85 KB
/
Error.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#pragma once
/*
python::Error - a C++ exception to throw when Python reports an error.
Use with Boost Python like this:
try {
// do things using Boost Python
} catch (const boost::python::error_already_set &) {
throw python::Error();
}
This class fetches error information stored by Python, stores it,
and provides an idiomatic C++ exception to throw instead.
*/
#include <exception>
#include <string>
#include <vector>
#include <boost/exception/all.hpp>
namespace pccl {
namespace python {
class Error : public std::exception, public boost::exception
{
public:
Error();
virtual ~Error() throw() {}
typedef boost::error_info<struct traceback_list_t, std::vector<std::string> > traceback_list;
virtual const char *what() const throw()
{
// Just return the last line of the traceback
std::vector<std::string> const* value =
boost::get_error_info<traceback_list>(*this);
if (value != 0)
return value->back().c_str();
else
return "Unknown";
}
virtual const char *traceback() const throw()
{
#if BOOST_VERSION >= 104100
return boost::diagnostic_information_what(*this);
#else
// Default to what with older versions
return what();
#endif
}
};
} // namespace python
} // namespace pccl
/*
* Create an error_info object for the traceback so that it can be printed
* out
*
*/
namespace boost
{
inline
std::string
to_string(pccl::python::Error::traceback_list const& traceback)
{
std::string result;
if (traceback.value().empty())
return result;
result += traceback.value()[0];
for (std::vector<std::string>::size_type ii = 1; ii < traceback.value().size(); ii++)
{
result += "\n" + traceback.value()[ii];
}
return result;
}
}