2010-09-28 20:26:55 +04:00
|
|
|
#include "util/exception.hh"
|
2010-09-15 01:33:11 +04:00
|
|
|
|
2011-07-14 00:53:18 +04:00
|
|
|
#ifdef __GXX_RTTI
|
|
|
|
#include <typeinfo>
|
|
|
|
#endif
|
|
|
|
|
2015-03-28 15:37:48 +03:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
2010-09-15 01:33:11 +04:00
|
|
|
|
2015-09-29 18:58:02 +03:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#include <windows.h>
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
namespace util {
|
|
|
|
|
|
|
|
Exception::Exception() throw() {}
|
|
|
|
Exception::~Exception() throw() {}
|
|
|
|
|
2011-07-14 00:53:18 +04:00
|
|
|
void Exception::SetLocation(const char *file, unsigned int line, const char *func, const char *child_name, const char *condition) {
|
|
|
|
/* The child class might have set some text, but we want this to come first.
|
|
|
|
* Another option would be passing this information to the constructor, but
|
|
|
|
* then child classes would have to accept constructor arguments and pass
|
2015-04-30 08:05:11 +03:00
|
|
|
* them down.
|
2011-07-14 00:53:18 +04:00
|
|
|
*/
|
2015-09-29 18:58:02 +03:00
|
|
|
std::string old_text;
|
2016-01-13 02:39:27 +03:00
|
|
|
what_.swap(old_text);
|
|
|
|
what_ << file << ':' << line;
|
|
|
|
if (func) what_ << " in " << func << " threw ";
|
2011-07-14 00:53:18 +04:00
|
|
|
if (child_name) {
|
2016-01-13 02:39:27 +03:00
|
|
|
what_ << child_name;
|
2011-07-14 00:53:18 +04:00
|
|
|
} else {
|
|
|
|
#ifdef __GXX_RTTI
|
2016-01-13 02:39:27 +03:00
|
|
|
what_ << typeid(this).name();
|
2011-07-14 00:53:18 +04:00
|
|
|
#else
|
2016-01-13 02:39:27 +03:00
|
|
|
what_ << "an exception";
|
2011-07-14 00:53:18 +04:00
|
|
|
#endif
|
|
|
|
}
|
2015-09-29 18:58:02 +03:00
|
|
|
if (condition) {
|
2016-01-13 02:39:27 +03:00
|
|
|
what_ << " because `" << condition << '\'';
|
2015-09-29 18:58:02 +03:00
|
|
|
}
|
2016-01-13 02:39:27 +03:00
|
|
|
what_ << ".\n";
|
|
|
|
what_ << old_text;
|
2011-07-14 00:53:18 +04:00
|
|
|
}
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
namespace {
|
2015-01-22 19:42:46 +03:00
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
const char *HandleStrerror(int ret, const char *buf) __attribute__ ((unused));
|
|
|
|
const char *HandleStrerror(const char *ret, const char * /*buf*/) __attribute__ ((unused));
|
|
|
|
#endif
|
2014-01-28 04:51:35 +04:00
|
|
|
// At least one of these functions will not be called.
|
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wunused-function"
|
|
|
|
#endif
|
2010-09-15 01:33:11 +04:00
|
|
|
// The XOPEN version.
|
|
|
|
const char *HandleStrerror(int ret, const char *buf) {
|
|
|
|
if (!ret) return buf;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The GNU version.
|
2011-01-26 04:19:11 +03:00
|
|
|
const char *HandleStrerror(const char *ret, const char * /*buf*/) {
|
2010-09-15 01:33:11 +04:00
|
|
|
return ret;
|
|
|
|
}
|
2014-01-28 04:51:35 +04:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
2010-09-15 01:33:11 +04:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ErrnoException::ErrnoException() throw() : errno_(errno) {
|
|
|
|
char buf[200];
|
|
|
|
buf[0] = 0;
|
2011-11-11 00:46:59 +04:00
|
|
|
#if defined(sun) || defined(_WIN32) || defined(_WIN64)
|
2010-11-28 05:54:56 +03:00
|
|
|
const char *add = strerror(errno);
|
|
|
|
#else
|
2010-09-15 01:33:11 +04:00
|
|
|
const char *add = HandleStrerror(strerror_r(errno, buf, 200), buf);
|
2010-11-28 05:54:56 +03:00
|
|
|
#endif
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
if (add) {
|
|
|
|
*this << add << ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrnoException::~ErrnoException() throw() {}
|
|
|
|
|
2012-09-28 18:04:48 +04:00
|
|
|
OverflowException::OverflowException() throw() {}
|
|
|
|
OverflowException::~OverflowException() throw() {}
|
|
|
|
|
2015-09-29 18:58:02 +03:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
WindowsException::WindowsException() throw() {
|
|
|
|
unsigned int last_error = GetLastError();
|
|
|
|
char error_msg[256] = "";
|
|
|
|
if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, last_error, LANG_NEUTRAL, error_msg, sizeof(error_msg), NULL)) {
|
|
|
|
*this << "Windows error " << GetLastError() << " while formatting Windows error " << last_error << ". ";
|
|
|
|
} else {
|
|
|
|
*this << "Windows error " << last_error << ": " << error_msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WindowsException::~WindowsException() throw() {}
|
|
|
|
#endif
|
|
|
|
|
2010-09-15 01:33:11 +04:00
|
|
|
} // namespace util
|