From 7197433905f30788477adfdfd469da16cea54136 Mon Sep 17 00:00:00 2001 From: sale <sandi.fatic@memgraph.io> Date: Tue, 29 Nov 2016 13:29:11 +0000 Subject: [PATCH] Refactored BasicException Summary: Refactored BasicException Test Plan: manual Reviewers: buda Subscribers: buda Maniphest Tasks: T129 Differential Revision: https://memgraph.phacility.com/D10 --- include/utils/exceptions/basic_exception.hpp | 58 +++++++++----------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/include/utils/exceptions/basic_exception.hpp b/include/utils/exceptions/basic_exception.hpp index f5cae09f3..90fed57ca 100644 --- a/include/utils/exceptions/basic_exception.hpp +++ b/include/utils/exceptions/basic_exception.hpp @@ -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; };