Skip to content

Commit

Permalink
Merge pull request #101 from AGH-Code-Industry/twarug/exceptions
Browse files Browse the repository at this point in the history
Refactor Archimedes Exceptions
  • Loading branch information
Twarug authored Apr 27, 2024
2 parents dde4ae4 + 257383e commit eef922a
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 55 deletions.
53 changes: 47 additions & 6 deletions include/Exception.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,61 @@
#pragma once

#include <exception>
#include <string>
#include <source_location>

#include "Logger.h"

namespace arch {

/// Base class for all exceptions in the project.
/// Do not use this class directly, use derived classes instead. (@see InitException)
///
class Exception: public std::exception {
protected:

/// Constructor.
/// @param title Title of the exception.
/// @param location Source location of the exception.
///
Exception(const std::string& title, const std::source_location& location);

/// Constructor.
/// @param title Title of the exception.
/// @param message Message of the exception.
/// @param location Source location of the exception.
///
Exception(const std::string& title, const std::string& message, const std::source_location& location);

public:
Exception(const std::string& title);
/// print the exception to the console.
/// @param level Log level of the exception.
///
void print(LogLevel level = LogLevel::error) const;

// Inherited form std::exception Get Exception message.
/// @return Exception message.
///
const char* what() const noexcept override;

protected:
void _appendMsg(const std::string& msg);
/// Get the title of the exception.
/// @return Title of the exception.
///
std::string_view title() const;

/// Get the message of the exception.
/// @return Message of the exception.
///
std::string_view message() const;

private:
std::string _msg;
/// Get the source location of the exception.
/// @return Source location of the exception.
///
const std::source_location& location() const;

protected:
std::string _title;
std::string _message;
std::source_location _location;
};

} // namespace arch
19 changes: 11 additions & 8 deletions include/exceptions/ConfigException.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#pragma once

#include <exception>
#include <string>

#include "Exception.h"

namespace arch {

class ConfigException: public std::exception {
/// Exception thrown when an error occurs during loading of configuration.
/// @see Exception
///
class ConfigException final: public Exception {
public:
explicit ConfigException(const std::string& message = "");

[[nodiscard]] const char* what() const noexcept override;

private:
std::string _message;
/// Constructor.
/// @param message Message of the exception.
/// @param location Source location of the exception.
///
ConfigException(const std::string& message, const std::source_location& location = std::source_location::current());
};

} // namespace arch
9 changes: 7 additions & 2 deletions include/exceptions/GLFWException.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

namespace arch {

class GLFWException: public Exception {
/// Exception thrown when an error occurs in GLFW.
/// @see Exception
///
class GLFWException final: public Exception {
public:
GLFWException();
/// Constructor.
/// @param location Source location of the exception.
GLFWException(const std::source_location& location = std::source_location::current());
};

} // namespace arch
19 changes: 11 additions & 8 deletions include/exceptions/InitException.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#pragma once

#include <exception>
#include <string>

#include "Exception.h"

namespace arch {

class InitException: public std::exception {
/// Exception thrown when an error occurs during initialization.
/// @see Exception
///
class InitException final: public Exception {
public:
explicit InitException(const std::string& message = "");

[[nodiscard]] const char* what() const noexcept override;

private:
std::string _message;
/// Constructor.
/// @param message Message of the exception.
/// @param location Source location of the exception.
///
InitException(const std::string& message, const std::source_location& location = std::source_location::current());
};

} // namespace arch
10 changes: 9 additions & 1 deletion include/net/NetException.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

namespace arch::net {

/// Exception thrown when an error occurs in networking.
/// @see Exception
///
class NetException: public Exception {
public:
NetException(const std::string& title);
/// Constructor.
/// @param message Title of the exception.
/// @param location Source location of the exception.
///
NetException(const std::string& message, const std::source_location& location = std::source_location::current());
};

// Alias for ease of use.
using Exception = NetException;

} // namespace arch::net
12 changes: 7 additions & 5 deletions src/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ Engine::~Engine() {
}

void Engine::start() {
_initialize();

try {
_initialize();

_mainLoop();
} catch (Exception& e) {
e.print();
} catch (std::exception& e) {
Logger::error("Crashed with exception: {}", e.what());
} catch (...) {
Logger::error("Dupa");
Logger::error("Unhandled exception occurred");
}
}

Expand Down Expand Up @@ -57,8 +59,8 @@ void Engine::_mainLoop() {
};

std::vector<Vertex> vertices{
{ float3(0.5f, 0.5f, 0.0f), {}, float2(1.0f, 1.0f) },
{ float3(0.5f, -0.5f, 0.0f), {}, float2(1.0f, 0.0f) },
{ float3(0.5f, 0.5f, 0.0f), {}, float2(1.0f, 1.0f) },
{ float3(0.5f, -0.5f, 0.0f), {}, float2(1.0f, 0.0f) },
{ float3(-0.5f, -0.5f, 0.0f), {}, float2(0.0f, 0.0f) },
{ float3(-0.5f, 0.5f, 0.0f), {}, float2(0.0f, 1.0f) }
};
Expand Down
27 changes: 22 additions & 5 deletions src/Exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@

namespace arch {

Exception::Exception(const std::string& title): _msg(title) {
_msg.append(": ");
Exception::Exception(const std::string& title, const std::source_location& location):
_title(title),
_location(location) {}

Exception::Exception(const std::string& title, const std::string& message, const std::source_location& location):
_title(title),
_message(message),
_location(location) {}

void Exception::print(LogLevel level) const {
Logger::log<const std::string&, const std::string&>(level, "[{}]: {}", _title, _message, _location);
}

const char* Exception::what() const noexcept {
return _msg.c_str();
return _message.c_str();
}

std::string_view Exception::title() const {
return _title;
}

std::string_view Exception::message() const {
return _message;
}

void Exception::_appendMsg(const std::string& msg) {
_msg.append(msg);
const std::source_location& Exception::location() const {
return _location;
}

} // namespace arch
1 change: 1 addition & 0 deletions src/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void Logger::init(LogLevel logLevel, const std::string& name) {
{
// Add a file sink
char logPath[256];
// ReSharper disable once CppRedundantCastExpression
std::snprintf(logPath, sizeof(logPath), "Logs/%s-%lld.log", name.c_str(), (long long)std::time(nullptr));

auto sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(logPath, true);
Expand Down
9 changes: 2 additions & 7 deletions src/exceptions/ConfigException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

namespace arch {

ConfigException::ConfigException(const std::string& message) {
_message = "Failed load configuration file: " + message;
}

const char* ConfigException::what() const noexcept {
return _message.c_str();
}
ConfigException::ConfigException(const std::string& message, const std::source_location& location):
Exception("Config", message, location) {}

} // namespace arch
7 changes: 2 additions & 5 deletions src/exceptions/GLFWException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

namespace arch {

GLFWException::GLFWException(): Exception("GLFW") {
GLFWException::GLFWException(const std::source_location& location): Exception("GLFW", location) {
const char* description;
int code = glfwGetError(&description);
_appendMsg("code ");
_appendMsg(std::to_string(code));
_appendMsg(" description ");
_appendMsg(description);
_message = "(Code: " + std::to_string(code) + "): " + description;
}

} // namespace arch
9 changes: 2 additions & 7 deletions src/exceptions/InitException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

namespace arch {

InitException::InitException(const std::string& message) {
_message = "Failed while initialization: " + message;
}

const char* InitException::what() const noexcept {
return _message.c_str();
}
InitException::InitException(const std::string& message, const std::source_location& location):
Exception("Init", message, location) {}

} // namespace arch
3 changes: 2 additions & 1 deletion src/net/NetException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace arch::net {

NetException::NetException(const std::string& title): Exception("Network submodule error: " + title) {}
NetException::NetException(const std::string& message, const std::source_location& location):
Exception("Network", message, location) {}

} // namespace arch::net

0 comments on commit eef922a

Please sign in to comment.