Skip to content

Commit

Permalink
feat: sdl::Error() exception class
Browse files Browse the repository at this point in the history
This is very similar to lege::lua::Error(), and simplifies throwing
errors originating from SDL code.
  • Loading branch information
mcb2003 committed Feb 27, 2024
1 parent 572b642 commit db657c5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_library(lege SHARED
modules/strict.cpp
modules/struct.cpp
modules/weak.cpp
sdl/error.cpp
util.cpp
)

Expand Down
8 changes: 4 additions & 4 deletions lib/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
#include "engine.hpp"
#include "lege.hpp"
#include "lua/helpers.hpp"
#include "sdl/helpers.hpp"
#include "util.hpp"

namespace lua = lege::lua;
namespace sdl = lege::sdl;

namespace lege {

Expand Down Expand Up @@ -57,8 +59,7 @@ EngineImpl::EngineImpl()
lua_atpanic(L, lua::on_error);
luaL_openlibs(L);
if (SDL_InitSubSystem(m_sdl_subsystems) < 0) {
throw std::runtime_error(
fmt::format("Could not initialize SDL: {}", SDL_GetError()));
throw sdl::Error("Could not initialize SDL");
}
}

Expand All @@ -82,8 +83,7 @@ void EngineImpl::loadFile(const char *filename, const char *mode,
std::size_t sz;
auto contents = (char *)SDL_LoadFile(filename, &sz);
if (!contents) {
throw std::runtime_error(fmt::format("could not load chunk \"{}\": {}",
filename, SDL_GetError()));
throw sdl::Error(fmt::format("could not load chunk \"{}\"", filename));
}
// We do this instead of manual memory management to avoid leaking memory if
// load() throws an exception
Expand Down
20 changes: 20 additions & 0 deletions lib/sdl/error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <cstddef>

#include <SDL_error.h>

#include "sdl/error.hpp"

namespace lege::sdl {

Error::Error(std::string prefix) {
const char *err = SDL_GetError();
m_msg = prefix;
m_msg += ": ";
m_msg += err;
}

Error::~Error() {}

const char *Error::what() const noexcept { return m_msg.c_str(); }

} // namespace lege::sdl
22 changes: 22 additions & 0 deletions lib/sdl/error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef LIBLEGE_sdl_ERROR_HPP
#define LIBLEGE_sdl_ERROR_HPP

#include <exception>
#include <string>

namespace lege::sdl {

class Error : public std::exception {
public:
Error(std::string prefix = "sdl error");
virtual ~Error() override;

virtual const char *what() const noexcept override;

private:
std::string m_msg;
};

} // namespace lege::sdl

#endif
6 changes: 6 additions & 0 deletions lib/sdl/helpers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef LIBLEGE_SDL_HELPERS_HPP
#define LIBLEGE_SDL_HELPERS_HPP

#include "sdl/error.hpp"

#endif

0 comments on commit db657c5

Please sign in to comment.