Rework how the C Interface shows exceptions
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-07-26 17:22:36 +02:00
parent 418d811d18
commit 9ec41fdd2d
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 10 additions and 5 deletions

View File

@ -1,4 +1,3 @@
#include "Core.hpp" #include "Core.hpp"
char* ExceptionHandler::_lastException = 0;
export const char* CreatureLib_C_GetLastException() { return ExceptionHandler::GetLastException(); } export const char* CreatureLib_C_GetLastException() { return ExceptionHandler::GetLastException(); }

View File

@ -3,17 +3,23 @@
#include <cstring> #include <cstring>
#include <exception> #include <exception>
#include <sstream>
#include <string> #include <string>
#define export extern "C" [[maybe_unused]] #define export extern "C" [[maybe_unused]]
#define CreatureLibException 1; #define CreatureLibException 1;
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
class ExceptionHandler { class ExceptionHandler {
static char* _lastException; static std::string _lastException;
public: public:
static void SetLastException(const std::exception& e) { _lastException = strcpy(_lastException, e.what()); } static void SetLastException(std::string file, std::string function, int line, const std::exception& e) {
static const char* GetLastException() { return _lastException; } std::stringstream ss;
ss << "[" << file << ", " << function << "-" << line << "] " << e.what();
_lastException = ss.str();
}
static const char* GetLastException() { return _lastException.c_str(); }
}; };
#define Try(data) \ #define Try(data) \
@ -21,7 +27,7 @@ public:
data; \ data; \
return 0; \ return 0; \
} catch (const std::exception& e) { \ } catch (const std::exception& e) { \
ExceptionHandler::SetLastException(e); \ ExceptionHandler::SetLastException(__FILENAME__, __FUNCTION__, __LINE__, e); \
return CreatureLibException; \ return CreatureLibException; \
} }