feat(DebugException): Simple exception for debug

Sometimes it is useful to pause execution with an exception while
debugging (though bad practice in production code). This is an explicit
exception type dedicated to that purpose. Further we have included some
compile time checks to ensure that these do not get used in release
builds.
This commit is contained in:
2025-11-19 07:41:42 -05:00
parent 80a4e12324
commit 4d2f5888ec
3 changed files with 43 additions and 2 deletions

View File

@@ -10,9 +10,12 @@ namespace gridfire::exceptions {
std::string m_message; std::string m_message;
std::string m_reactionID; std::string m_reactionID;
public: public:
ReactionError(const std::string& msg, const std::string& reactionId): m_message(msg), m_reactionID(reactionId) {} ReactionError(const std::string& msg, const std::string& reactionId) {
m_reactionID = reactionId;
m_message = std::format("Reaction {}: {}", reactionId, msg);
}
const char* what() const noexcept override { const char* what() const noexcept override {
return std::format("Reaction {}: {}", m_reactionID, m_message).c_str(); return m_message.c_str();
} }
}; };

View File

@@ -1,4 +1,9 @@
#pragma once #pragma once
#include "gridfire/exceptions/error_engine.h" #include "gridfire/exceptions/error_engine.h"
#include "gridfire/exceptions/error_utils.h"
#include "gridfire/exceptions/general.h"
#include "gridfire/exceptions/error_policy.h"
#include "gridfire/exceptions/error_reaction.h"
#include "gridfire/exceptions/error_solver.h"
#include "gridfire/exceptions/error_utils.h" #include "gridfire/exceptions/error_utils.h"

View File

@@ -0,0 +1,33 @@
#pragma once
#include <exception>
#include <source_location>
#include <string>
namespace gridfire::exceptions {
class DebugException final : public std::runtime_error {
public:
#ifdef NDEBUG
#if defined(__clang__)
__attribute__((unavailable("DebugExceptions may not be used in release builds")))
#elif defined(__GNUC__)
__attribute__((error("DebugExceptions may not be used in release builds")))
#endif
#endif
explicit DebugException(const std::string_view message,
const std::source_location loc = std::source_location::current())
: std::runtime_error(format_error(message, loc))
{
}
private:
static std::string format_error(std::string_view message, const std::source_location loc) {
return std::format("[DEBUG HALT] {}:{}: {}",
loc.file_name(),
loc.line(),
message);
}
};
}