refactor(exceptions): All exceptions are derived from GridFireError

Now all GridFire exceptions are derived from the base GridFireError,
this allows for more clean handling of various exception cases
This commit is contained in:
2025-11-21 14:26:24 -05:00
parent 442d4ed86c
commit acc71ba12e
20 changed files with 89 additions and 201 deletions

View File

View File

View File

@@ -4,8 +4,10 @@
#include <source_location> #include <source_location>
#include <string> #include <string>
#include "gridfire/exceptions/gridfire_exception.h"
namespace gridfire::exceptions { namespace gridfire::exceptions {
class DebugException final : public std::runtime_error { class DebugException final : public GridFireError{
public: public:
#ifdef NDEBUG #ifdef NDEBUG
#if defined(__clang__) #if defined(__clang__)
@@ -18,10 +20,8 @@ namespace gridfire::exceptions {
explicit DebugException(const std::string_view message, explicit DebugException(const std::string_view message,
const std::source_location loc = std::source_location::current()) const std::source_location loc = std::source_location::current())
: std::runtime_error(format_error(message, loc)) : GridFireError(format_error(message, loc)) {
{
} }
private: private:
static std::string format_error(std::string_view message, const std::source_location loc) { static std::string format_error(std::string_view message, const std::source_location loc) {
return std::format("[DEBUG HALT] {}:{}: {}", return std::format("[DEBUG HALT] {}:{}: {}",

View File

@@ -5,150 +5,52 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "gridfire/exceptions/gridfire_exception.h"
namespace gridfire::exceptions { namespace gridfire::exceptions {
class EngineError : public std::exception {}; class EngineError : public GridFireError {
using GridFireError::GridFireError;
class StaleEngineTrigger final : public EngineError {
public:
struct state {
double m_T9;
double m_rho;
std::vector<double> m_Y;
double m_t;
int m_total_steps;
double m_eps_nuc;
};
explicit StaleEngineTrigger(state s)
: m_state(std::move(s)) {}
[[nodiscard]] const char* what() const noexcept override{
return "Engine reports stale state. This means that the caller should trigger a update of the engine state before continuing with the integration. If you as an end user are seeing this error, it is likely a bug in the code that should be reported. Please provide the input parameters and the context in which this error occurred. Thank you for your help!";
}
[[nodiscard]] state getState() const {
return m_state;
}
[[nodiscard]] size_t numSpecies() const {
return m_state.m_Y.size();
}
[[nodiscard]] size_t totalSteps() const {
return m_state.m_total_steps;
}
[[nodiscard]] double energy() const {
return m_state.m_eps_nuc;
}
[[nodiscard]] double getMolarAbundance(const size_t index) const {
if (index > m_state.m_Y.size() - 1) {
throw std::out_of_range("Index out of bounds for molar abundance vector.");
}
return m_state.m_Y[index];
}
[[nodiscard]] double temperature() const {
return m_state.m_T9 * 1e9; // Convert T9 back to Kelvin
}
[[nodiscard]] double density() const {
return m_state.m_rho;
}
private:
state m_state;
};
class StaleEngineError final : public EngineError {
public:
explicit StaleEngineError(std::string message)
: m_message(std::move(message)) {}
[[nodiscard]] const char* what() const noexcept override {
return m_message.c_str();
}
private:
std::string m_message;
}; };
class FailedToPartitionEngineError final : public EngineError { class FailedToPartitionEngineError final : public EngineError {
public: using EngineError::EngineError;
explicit FailedToPartitionEngineError(std::string message)
: m_message(std::move(message)) {}
[[nodiscard]] const char* what() const noexcept override {
return m_message.c_str();
}
private:
std::string m_message;
}; };
class NetworkResizedError final : public EngineError { class NetworkResizedError final : public EngineError {
public: using EngineError::EngineError;
explicit NetworkResizedError(std::string message)
: m_message(std::move(message)) {}
[[nodiscard]] const char* what() const noexcept override {
return m_message.c_str();
}
private:
std::string m_message;
}; };
class UnableToSetNetworkReactionsError final : public EngineError { class UnableToSetNetworkReactionsError final : public EngineError {
public: using EngineError::EngineError;
explicit UnableToSetNetworkReactionsError(std::string message)
: m_message(std::move(message)) {}
[[nodiscard]] const char* what() const noexcept override {
return m_message.c_str();
}
private:
std::string m_message;
};
class JacobianError : public EngineError {};
class StaleJacobianError final : public JacobianError {
public:
explicit StaleJacobianError(std::string message) : m_message(std::move(message)) {}
[[nodiscard]] const char* what() const noexcept override {
return m_message.c_str();
}
private:
std::string m_message;
};
class UninitializedJacobianError final: public JacobianError {
public:
explicit UninitializedJacobianError(std::string message): m_message(std::move(message)) {}
[[nodiscard]] const char* what() const noexcept override {
return m_message.c_str();
}
private:
std::string m_message;
};
class UnknownJacobianError final : public JacobianError {
public:
explicit UnknownJacobianError(std::string message): m_message(std::move(message)) {}
[[nodiscard]] const char* what() const noexcept override {
return m_message.c_str();
}
private:
std::string m_message;
}; };
class BadCollectionError final : public EngineError { class BadCollectionError final : public EngineError {
public: using EngineError::EngineError;
explicit BadCollectionError(std::string message): m_message(std::move(message)) {}
[[nodiscard]] const char* what() const noexcept override { return m_message.c_str(); }
private:
std::string m_message;
}; };
class InvalidQSESolutionError final : public EngineError {
using EngineError::EngineError;
};
class BadRHSEngineError final : public EngineError {
using EngineError::EngineError;
};
class JacobianError : public EngineError {
using EngineError::EngineError;
};
class StaleJacobianError final : public JacobianError {
using JacobianError::JacobianError;
};
class UninitializedJacobianError final: public JacobianError {
using JacobianError::JacobianError;
};
class UnknownJacobianError final : public JacobianError {
using JacobianError::JacobianError;
};
} }

View File

@@ -15,6 +15,8 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include "gridfire/exceptions/gridfire_exception.h"
namespace gridfire::exceptions { namespace gridfire::exceptions {
/** /**
* @class PolicyError * @class PolicyError
@@ -23,23 +25,8 @@ namespace gridfire::exceptions {
* This exception is the parent for more specific policy-related errors. Catching this * This exception is the parent for more specific policy-related errors. Catching this
* type will catch any exception originating from the policy system. * type will catch any exception originating from the policy system.
*/ */
class PolicyError : public std::exception { class PolicyError : public GridFireError {
public: using GridFireError::GridFireError;
/**
* @brief Constructs a PolicyError with a descriptive message.
* @param msg The error message.
*/
explicit PolicyError(std::string msg) : m_message(std::move(msg)) {};
/**
* @brief Returns the explanatory string.
* @return A C-style string with the error message.
*/
[[nodiscard]] const char* what() const noexcept override {
return m_message.c_str();
}
private:
std::string m_message;
}; };
/** /**
@@ -50,12 +37,7 @@ namespace gridfire::exceptions {
* reaction library used by GridFire does not contain a reaction specified by the policy. * reaction library used by GridFire does not contain a reaction specified by the policy.
*/ */
class MissingBaseReactionError final : public PolicyError { class MissingBaseReactionError final : public PolicyError {
public: using PolicyError::PolicyError;
/**
* @brief Constructs a MissingBaseReactionError with a descriptive message.
* @param msg The error message.
*/
explicit MissingBaseReactionError(const std::string& msg) : PolicyError(msg) {};
}; };
/** /**
@@ -66,12 +48,7 @@ namespace gridfire::exceptions {
* one or more of the essential species needed to construct the network. * one or more of the essential species needed to construct the network.
*/ */
class MissingSeedSpeciesError final : public PolicyError { class MissingSeedSpeciesError final : public PolicyError {
public: using PolicyError::PolicyError;
/**
* @brief Constructs a MissingSeedSpeciesError with a descriptive message.
* @param msg The error message.
*/
explicit MissingSeedSpeciesError(const std::string& msg) : PolicyError(msg) {};
}; };
/** /**
@@ -82,11 +59,6 @@ namespace gridfire::exceptions {
* the network has been built but fails the final verification step. * the network has been built but fails the final verification step.
*/ */
class MissingKeyReactionError final : public PolicyError { class MissingKeyReactionError final : public PolicyError {
public: using PolicyError::PolicyError;
/**
* @brief Constructs a MissingKeyReactionError with a descriptive message.
* @param msg The error message.
*/
explicit MissingKeyReactionError(const std::string& msg) : PolicyError(msg) {}
}; };
} }

View File

@@ -4,16 +4,19 @@
#include <string> #include <string>
#include <format> #include <format>
#include "gridfire/exceptions/gridfire_exception.h"
namespace gridfire::exceptions { namespace gridfire::exceptions {
class ReactionError : public std::exception { class ReactionError : public GridFireError {
private: private:
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) { ReactionError(const std::string& msg, const std::string& reactionId) : GridFireError(msg) {
m_reactionID = reactionId; m_reactionID = reactionId;
m_message = std::format("Reaction {}: {}", reactionId, msg); m_message = std::format("Reaction {}: {}", reactionId, msg);
} }
const char* what() const noexcept override { const char* what() const noexcept override {
return m_message.c_str(); return m_message.c_str();
} }

View File

@@ -1,22 +1,10 @@
#pragma once #pragma once
#include <exception> #include "gridfire/exceptions/gridfire_exception.h"
#include <string>
namespace gridfire::exceptions { namespace gridfire::exceptions {
class SolverError : public std::exception { class SolverError : GridFireError {
public: using GridFireError::GridFireError;
SolverError(std::string msg) : m_msg(std::move(msg)) {}
[[nodiscard]] const char* what() const noexcept override {
return m_msg.c_str();
}
private:
std::string m_msg;
};
class CVODESolverFailureError final : public SolverError {
using SolverError::SolverError;
}; };
class SingularJacobianError final : public SolverError { class SingularJacobianError final : public SolverError {
@@ -26,4 +14,17 @@ namespace gridfire::exceptions {
class IllConditionedJacobianError final : public SolverError { class IllConditionedJacobianError final : public SolverError {
using SolverError::SolverError; using SolverError::SolverError;
}; };
class SUNDIALSError : public SolverError {
using SolverError::SolverError;
};
class CVODESolverFailureError final : public SUNDIALSError {
using SUNDIALSError::SUNDIALSError;
};
class KINSolSolverFailureError final : public SUNDIALSError {
using SUNDIALSError::SUNDIALSError;
};
} }

View File

@@ -4,22 +4,14 @@
#include <string> #include <string>
#include <utility> #include <utility>
namespace gridfire::exceptions { #include "gridfire/exceptions/gridfire_exception.h"
class UtilityError : public std::exception {
public:
explicit UtilityError(std::string message) : m_message(std::move(message)) {}
[[nodiscard]] const char* what() const noexcept override { namespace gridfire::exceptions {
return m_message.c_str(); class UtilityError : public GridFireError {
} using GridFireError::GridFireError;
private:
std::string m_message;
}; };
class HashingError final : public UtilityError { class HashingError final : public UtilityError {
public: using UtilityError::UtilityError;
explicit HashingError(const std::string &message) : UtilityError(message) {}
}; };
} }

View File

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

View File

@@ -0,0 +1,17 @@
#pragma once
#include <exception>
#include <string>
namespace gridfire::exceptions {
class GridFireError : public std::exception {
public:
explicit GridFireError(std::string msg) : m_msg(std::move(msg)) {}
[[nodiscard]] const char* what() const noexcept override {
return m_msg.c_str();
}
private:
std::string m_msg;
};
}

View File

View File

View File

View File