rogue
Loading...
Searching...
No Matches
GeneralError.cpp
Go to the documentation of this file.
1
17#include "rogue/Directives.h"
18
19#include "rogue/GeneralError.h"
20
21#include <stdarg.h>
22
23#include <cstdio>
24#include <string>
25
26#ifndef NO_PYTHON
27 #include <boost/python.hpp>
28namespace bp = boost::python;
29
31
32#endif
33
34rogue::GeneralError::GeneralError(std::string src, std::string text) {
35 snprintf(text_, BuffSize, "%s: General Error: %s", src.c_str(), text.c_str());
36}
37
38rogue::GeneralError rogue::GeneralError::create(std::string src, const char* fmt, ...) {
39 char temp[BuffSize];
40 va_list args;
41
42 va_start(args, fmt);
43 vsnprintf(temp, BuffSize, fmt, args);
44 va_end(args);
45
46 return (rogue::GeneralError(src, temp));
47}
48
49char const* rogue::GeneralError::what() const throw() {
50 return (text_);
51}
52
54#ifndef NO_PYTHON
55
56 bp::class_<rogue::GeneralError>("GeneralError", bp::init<std::string, std::string>());
57
58 PyObject* typeObj = PyErr_NewException(const_cast<char*>("rogue.GeneralError"), PyExc_Exception, 0);
59 bp::scope().attr("GeneralError") = bp::handle<>(bp::borrowed(typeObj));
60
61 rogue::generalErrorObj = typeObj;
62
63 bp::register_exception_translator<rogue::GeneralError>(&(rogue::GeneralError::translate));
64
65#endif
66}
67
69#ifndef NO_PYTHON
70
71 bp::object exc(e); // wrap the C++ exception
72
73 bp::object exc_t(bp::handle<>(bp::borrowed(rogue::generalErrorObj)));
74 exc_t.attr("cause") = exc; // add the wrapped exception to the Python exception
75
76 PyErr_SetString(rogue::generalErrorObj, e.what());
77#endif
78}
Generic Rogue exception type.
static void translate(GeneralError const &e)
Translates GeneralError into a Python exception.
GeneralError(std::string src, std::string text)
Constructs an error from source and message text.
char const * what() const
Returns exception text for standard exception handling.
static void setup_python()
Registers Python exception translation for GeneralError.
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
PyObject * generalErrorObj