Rework exceptions.
continuous-integration/drone/push Build is failing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-08-15 16:34:52 +02:00
parent c921d3127b
commit 93a763ab78
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
12 changed files with 29 additions and 55 deletions

View File

@ -3,3 +3,4 @@
std::string ExceptionHandler::_creatureLibLastException = "";
export const char* CreatureLib_C_GetLastException() { return ExceptionHandler::GetLastException(); }
export const char* CreatureLib_C_GetLastExceptionStacktrace() { return ExceptionHandler::GetLastExceptionStacktrace(); }

View File

@ -13,20 +13,23 @@
class ExceptionHandler {
static std::string _creatureLibLastException;
static std::string _creatureLibLastExceptionStacktrace;
public:
static void SetLastException(std::string function, const ArbUt::Exception& e) {
static void SetLastArbUtException(const std::string& function, const ArbUt::Exception& e) {
std::stringstream ss;
ss << "[" << function << "] " << e.what() << std::endl;
ss << e.GetStacktrace();
_creatureLibLastException = ss.str();
_creatureLibLastExceptionStacktrace = e.GetStacktrace();
}
static void SetLastException(std::string function, const std::exception& e) {
static void SetLastException(const std::string& function, const std::exception& e) {
std::stringstream ss;
ss << "[" << function << "] " << e.what();
_creatureLibLastException = ss.str();
_creatureLibLastExceptionStacktrace = "";
}
static const char* GetLastException() { return _creatureLibLastException.c_str(); }
static const char* GetLastExceptionStacktrace() { return _creatureLibLastExceptionStacktrace.c_str(); }
};
#define Try(data) \
@ -34,7 +37,7 @@ public:
data; \
return 0; \
} catch (const ArbUt::Exception& e) { \
ExceptionHandler::SetLastException(__FUNCTION__, e); \
ExceptionHandler::SetLastArbUtException(__FUNCTION__, e); \
return CreatureLibException; \
} catch (const std::exception& e) { \
ExceptionHandler::SetLastException(__FUNCTION__, e); \

View File

@ -1,7 +1,6 @@
#include "TurnHandler.hpp"
#include <Arbutils/Assert.hpp>
#include <unordered_set>
#include "../../Library/Exceptions/NotImplementedException.hpp"
#include "../EventHooks/EventDataClasses.hpp"
#include "../History/HistoryElements/AttackUseHistory.hpp"
#include "../ScriptHandling/ScriptMacros.hpp"
@ -58,14 +57,14 @@ void TurnHandler::ExecuteChoice(ArbUt::BorrowedPtr<BaseTurnChoice> choice) {
}
switch (choiceKind) {
case TurnChoiceKind::Pass: throw NotReachableException();
case TurnChoiceKind::Pass: return;
case TurnChoiceKind::Attack:
try_creature(return ExecuteAttackChoice(choice.ForceAs<AttackTurnChoice>()),
"Encountered exception during attack choice execution");
case TurnChoiceKind::Switch: return ExecuteSwitchChoice(choice.ForceAs<SwitchTurnChoice>());
case TurnChoiceKind::Flee: return ExecuteFleeChoice(choice.ForceAs<FleeTurnChoice>());
case TurnChoiceKind::Item: throw NotImplementedException();
case TurnChoiceKind::Item: NOT_IMPLEMENTED;
}
}
@ -201,7 +200,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::Bo
if (hasSecondaryEffect) {
HOOK(OnSecondaryEffect, user, attack, target.GetRaw(), hitIndex);
}
} catch (const CreatureException& e) {
} catch (const ArbUt::Exception& e) {
throw e;
} catch (const std::exception& e) {
THROW_CREATURE("Exception during status attack effect handling: " << e.what() << ".");
@ -233,7 +232,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::Bo
if (hasSecondaryEffect) {
HOOK(OnSecondaryEffect, user, attack, target.GetRaw(), hitIndex);
}
} catch (const CreatureException& e) {
} catch (const ArbUt::Exception& e) {
throw e;
} catch (const std::exception& e) {
THROW_CREATURE("Exception during offensive attack secondary effect handling: " << e.what()

View File

@ -1,6 +1,5 @@
#include "BattleStatCalculator.hpp"
#include <Arbutils/Assert.hpp>
#include "../../Library/Exceptions/NotImplementedException.hpp"
#include "../Models/Creature.hpp"
using namespace CreatureLib;

View File

@ -40,7 +40,7 @@ void Battle::CheckChoicesSetAndRun() {
return;
}
}
} catch (const CreatureException& e) {
} catch (const ArbUt::Exception& e) {
throw e;
} catch (const std::exception& e) {
THROW_CREATURE("Exception during choices set validation: '" << e.what() << "'.")
@ -81,7 +81,7 @@ void Battle::CheckChoicesSetAndRun() {
TriggerEventListener<TurnStartEvent>();
try {
TurnHandler::RunTurn(this->_currentTurnQueue);
} catch (const CreatureException& e) {
} catch (const ArbUt::Exception& e) {
throw e;
} catch (const std::exception& e) {
THROW_CREATURE("Error during running a turn: '" << e.what() << "'.");

View File

@ -2,7 +2,6 @@
#define CREATURELIB_SCRIPTAGGREGATOR_HPP
#include <Arbutils/Collections/List.hpp>
#include "../../Library/Exceptions/NotReachableException.hpp"
#include "Script.hpp"
#include "ScriptSet.hpp"
#include "ScriptWrapper.hpp"

View File

@ -12,7 +12,7 @@
THROW_CREATURE("Exception running script hook '" #hookName "': " << e.what()) \
} \
} \
} catch (const CreatureException& e) { \
} catch (const ArbUt::Exception& e) { \
throw e; \
} catch (const std::exception& e) { \
THROW_CREATURE("Exception setting up script hook '" #hookName "': " << e.what()) \

View File

@ -1,7 +1,7 @@
#ifndef CREATURELIB_CLAMPEDSTATISTICSET_HPP
#define CREATURELIB_CLAMPEDSTATISTICSET_HPP
#include "Exceptions/NotReachableException.hpp"
#include "Exceptions/CreatureException.hpp"
#include "Statistic.hpp"
namespace CreatureLib::Library {
template <class T, int Min, int Max> class ClampedStatisticSet {
@ -36,7 +36,7 @@ namespace CreatureLib::Library {
case CreatureLib::Library::Statistic::MagicalAttack: return _magicalAttack;
case CreatureLib::Library::Statistic::MagicalDefense: return _magicalDefense;
case CreatureLib::Library::Statistic::Speed: return _speed;
default: throw NotReachableException();
default: NOT_REACHABLE;
}
}
@ -52,7 +52,7 @@ namespace CreatureLib::Library {
case CreatureLib::Library::Statistic::MagicalAttack: _magicalAttack = value; break;
case CreatureLib::Library::Statistic::MagicalDefense: _magicalDefense = value; break;
case CreatureLib::Library::Statistic::Speed: _speed = value; break;
default: throw NotReachableException();
default: NOT_REACHABLE;
}
}
@ -78,7 +78,7 @@ namespace CreatureLib::Library {
case CreatureLib::Library::Statistic::MagicalAttack: ModifyStat(_magicalAttack, +) break;
case CreatureLib::Library::Statistic::MagicalDefense: ModifyStat(_magicalDefense, +) break;
case CreatureLib::Library::Statistic::Speed: ModifyStat(_speed, +) break;
default: throw NotReachableException();
default: NOT_REACHABLE;
}
}
inline bool DecreaseStatBy(Statistic stat, T amount) {
@ -89,7 +89,7 @@ namespace CreatureLib::Library {
case CreatureLib::Library::Statistic::MagicalAttack: ModifyStat(_magicalAttack, -) break;
case CreatureLib::Library::Statistic::MagicalDefense: ModifyStat(_magicalDefense, -) break;
case CreatureLib::Library::Statistic::Speed: ModifyStat(_speed, -) break;
default: throw NotReachableException();
default: NOT_REACHABLE;
}
}
};

View File

@ -8,21 +8,17 @@
#include <string>
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
class CreatureException : public ArbUt::Exception {
public:
explicit CreatureException(const std::string& error) : ArbUt::Exception(error) {}
virtual ~CreatureException() = default;
};
#define THROW_CREATURE(message) \
std::stringstream ___ss; \
___ss << "[" << __FILENAME__ << ":" << __LINE__ << "] " << message; \
throw CreatureException(___ss.str());
throw ArbUt::Exception(___ss.str());
#define NOT_REACHABLE THROW_CREATURE("Not reachable");
#define NOT_IMPLEMENTED THROW_CREATURE("Not implemented");
#define try_creature(data, msg) \
try { \
data; \
} catch (const CreatureException& e) { \
} catch (const ArbUt::Exception& e) { \
throw e; \
} catch (const std::exception& e) { \
THROW_CREATURE(msg << ": '" << e.what() << "'."); \

View File

@ -1,12 +0,0 @@
#ifndef CREATURELIB_NOTIMPLEMENTEDEXCEPTION_HPP
#define CREATURELIB_NOTIMPLEMENTEDEXCEPTION_HPP
#include "CreatureException.hpp"
class NotImplementedException : CreatureException {
public:
NotImplementedException(std::string error) : CreatureException(error) {}
NotImplementedException() : CreatureException("Not Implemented") {}
};
#endif // CREATURELIB_NOTIMPLEMENTEDEXCEPTION_HPP

View File

@ -1,11 +0,0 @@
#ifndef CREATURELIB_NOTREACHABLEEXCEPTION_HPP
#define CREATURELIB_NOTREACHABLEEXCEPTION_HPP
#include "CreatureException.hpp"
class NotReachableException : CreatureException {
public:
NotReachableException() : CreatureException("Not Reachable"){};
};
#endif // CREATURELIB_NOTREACHABLEEXCEPTION_HPP

View File

@ -2,7 +2,7 @@
#define CREATURELIB_STATISTICSET_HPP
#include <exception>
#include <stdint.h>
#include "Exceptions/NotReachableException.hpp"
#include "Exceptions/CreatureException.hpp"
#include "Statistic.hpp"
namespace CreatureLib::Library {
@ -37,7 +37,7 @@ namespace CreatureLib::Library {
case CreatureLib::Library::Statistic::MagicalAttack: return _magicalAttack;
case CreatureLib::Library::Statistic::MagicalDefense: return _magicalDefense;
case CreatureLib::Library::Statistic::Speed: return _speed;
default: throw NotReachableException();
default: NOT_REACHABLE;
}
}
@ -49,7 +49,7 @@ namespace CreatureLib::Library {
case CreatureLib::Library::Statistic::MagicalAttack: _magicalAttack = value; break;
case CreatureLib::Library::Statistic::MagicalDefense: _magicalDefense = value; break;
case CreatureLib::Library::Statistic::Speed: _speed = value; break;
default: throw NotReachableException();
default: NOT_REACHABLE;
}
}
@ -61,7 +61,7 @@ namespace CreatureLib::Library {
case CreatureLib::Library::Statistic::MagicalAttack: _magicalAttack += amount; break;
case CreatureLib::Library::Statistic::MagicalDefense: _magicalDefense += amount; break;
case CreatureLib::Library::Statistic::Speed: _speed += amount; break;
default: throw NotReachableException();
default: NOT_REACHABLE;
}
}
inline void DecreaseStatBy(Statistic stat, T amount) {
@ -72,7 +72,7 @@ namespace CreatureLib::Library {
case CreatureLib::Library::Statistic::MagicalAttack: _magicalAttack -= amount; break;
case CreatureLib::Library::Statistic::MagicalDefense: _magicalDefense -= amount; break;
case CreatureLib::Library::Statistic::Speed: _speed -= amount; break;
default: throw NotReachableException();
default: NOT_REACHABLE;
}
}
};