Refactored BasicException

Summary: Refactored BasicException

Test Plan: manual

Reviewers: buda

Subscribers: buda

Maniphest Tasks: T129

Differential Revision: https://memgraph.phacility.com/D10
This commit is contained in:
sale 2016-11-29 13:29:11 +00:00
parent b4fc263be1
commit 7197433905

View File

@ -6,39 +6,33 @@
#include "utils/auto_scope.hpp"
#include "utils/stacktrace.hpp"
class BasicException : public std::exception
{
public:
BasicException(const std::string &message) noexcept : message(message),
stacktrace_size(10)
{
class BasicException : public std::exception {
public:
BasicException(const std::string &message, uint64_t stacktrace_size) noexcept
: message_(message),
stacktrace_size_(stacktrace_size) {}
BasicException(const std::string &message) noexcept : message_(message),
stacktrace_size_(10) {}
template <class... Args>
BasicException(const std::string &format, Args &&... args) noexcept
: BasicException(fmt::format(format, std::forward<Args>(args)...)) {}
const char *what() const noexcept override { return message_.c_str(); }
private:
std::string message_;
uint64_t stacktrace_size_;
#ifndef NDEBUG
this->message += '\n';
void generate_stacktrace() {
Stacktrace stacktrace;
Stacktrace stacktrace;
// TODO: write this better
// (limit the size of stacktrace)
uint64_t count = 0;
for (auto &line : stacktrace) {
this->message +=
fmt::format(" at {} ({})\n", line.function, line.location);
if (++count >= stacktrace_size) break;
}
int size = std::min(stacktrace_size_, stacktrace.size());
for (int i = 0; i < size; i++) {
message_.append(fmt::format("\n at {} ({})", stacktrace[i].function,
stacktrace[i].location));
}
}
#endif
}
template <class... Args>
BasicException(const std::string &format, Args &&... args) noexcept
: BasicException(fmt::format(format, std::forward<Args>(args)...))
{
}
const char *what() const noexcept override { return message.c_str(); }
private:
std::string message;
uint64_t stacktrace_size;
};