From: Daniel Karbach Date: Mon, 21 Nov 2016 09:22:49 +0000 (+0100) Subject: move common exceptions to app/error X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=c0a5ece0f6bacea1b85157a908d710070fb0affd;p=blank.git move common exceptions to app/error --- diff --git a/src/app/error.cpp b/src/app/error.cpp new file mode 100644 index 0000000..3b29a34 --- /dev/null +++ b/src/app/error.cpp @@ -0,0 +1,159 @@ +#include "error.hpp" + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + +namespace { + +string alut_error_append(ALenum num, string msg) { + const char *error = alutGetErrorString(num); + if (error && *error != '\0') { + msg += ": "; + msg += error; + } + return msg; +} + +string gl_error_append(string msg) { + const GLubyte *error = gluErrorString(glGetError()); + if (error && *error != '\0') { + const GLubyte *errEnd = error; + while (*errEnd != '\0') { + ++errEnd; + } + msg += ": "; + msg.append(error, errEnd); + } + return msg; +} + +string gl_error_get() { + string msg; + const GLubyte *error = gluErrorString(glGetError()); + if (error && *error != '\0') { + const GLubyte *errEnd = error; + while (*errEnd != '\0') { + ++errEnd; + } + msg.assign(error, errEnd); + } + return msg; +} + +string net_error_append(string msg) { + const char *error = SDLNet_GetError(); + if (*error != '\0') { + msg += ": "; + msg += error; + } + return msg; +} + +string sdl_error_append(string msg) { + const char *error = SDL_GetError(); + if (error && *error != '\0') { + msg += ": "; + msg += error; + SDL_ClearError(); + } + return msg; +} + +string ttf_error_append(string msg) { + const char *error = TTF_GetError(); + if (error && *error != '\0') { + msg += ": "; + msg += error; + } + return msg; +} + +} + + +namespace blank { + +AlutError::AlutError(ALenum num) +: runtime_error(alutGetErrorString(num)) { + +} + +AlutError::AlutError(ALenum num, const string &msg) +: runtime_error(alut_error_append(num, msg)) { + +} + + +GLError::GLError() +: runtime_error(gl_error_get()) { + +} + +GLError::GLError(const string &msg) +: runtime_error(gl_error_append(msg)) { + +} + + +NetError::NetError() +: runtime_error(SDLNet_GetError()) { + +} + +NetError::NetError(const string &msg) +: runtime_error(net_error_append(msg)) { + +} + + +SDLError::SDLError() +: runtime_error(SDL_GetError()) { + +} + +SDLError::SDLError(const string &msg) +: runtime_error(sdl_error_append(msg)) { + +} + + +SysError::SysError() +: SysError(errno) { + +} + +SysError::SysError(const string &msg) +: SysError(errno, msg) { + +} + +SysError::SysError(int err_num) +: runtime_error(strerror(err_num)) { + +} + +SysError::SysError(int err_num, const string &msg) +: runtime_error(msg + ": " + strerror(err_num)) { + +} + + +TTFError::TTFError() +: runtime_error(TTF_GetError()) { + +} + +TTFError::TTFError(const string &msg) +: runtime_error(ttf_error_append(msg)) { + +} + +} diff --git a/src/app/error.hpp b/src/app/error.hpp new file mode 100644 index 0000000..0f22f8a --- /dev/null +++ b/src/app/error.hpp @@ -0,0 +1,74 @@ +#ifndef BLANK_APP_ERROR_HPP_ +#define BLANK_APP_ERROR_HPP_ + +#include +#include +#include + + +namespace blank { + +class AlutError +: public std::runtime_error { + +public: + explicit AlutError(ALenum); + AlutError(ALenum, const std::string &); + +}; + + +class GLError +: public std::runtime_error { + +public: + GLError(); + explicit GLError(const std::string &); + +}; + + +class NetError +: public std::runtime_error { + +public: + NetError(); + explicit NetError(const std::string &); + +}; + + +class SDLError +: public std::runtime_error { + +public: + SDLError(); + explicit SDLError(const std::string &); + +}; + + +class SysError +: public std::runtime_error { + +public: + SysError(); + explicit SysError(const std::string &); + explicit SysError(int err_num); + SysError(int err_num, const std::string &); + +}; + + +class TTFError +: public std::runtime_error { + +public: + TTFError(); + explicit TTFError(const std::string &); + +}; + +} + +#endif diff --git a/src/app/init.cpp b/src/app/init.cpp index 78a83e1..dfadffe 100644 --- a/src/app/init.cpp +++ b/src/app/init.cpp @@ -6,76 +6,12 @@ #include #include #include +#include #include -namespace { - -std::string sdl_error_append(std::string msg) { - const char *error = SDL_GetError(); - if (*error != '\0') { - msg += ": "; - msg += error; - SDL_ClearError(); - } - return msg; -} - -std::string net_error_append(std::string msg) { - const char *error = SDLNet_GetError(); - if (*error != '\0') { - msg += ": "; - msg += error; - } - return msg; -} - -std::string alut_error_append(ALenum num, std::string msg) { - const char *error = alutGetErrorString(num); - if (*error != '\0') { - msg += ": "; - msg += error; - } - return msg; -} - -} - namespace blank { -AlutError::AlutError(ALenum num) -: std::runtime_error(alutGetErrorString(num)) { - -} - -AlutError::AlutError(ALenum num, const std::string &msg) -: std::runtime_error(alut_error_append(num, msg)) { - -} - - -NetError::NetError() -: std::runtime_error(SDLNet_GetError()) { - -} - -NetError::NetError(const std::string &msg) -: std::runtime_error(net_error_append(msg)) { - -} - - -SDLError::SDLError() -: std::runtime_error(SDL_GetError()) { - -} - -SDLError::SDLError(const std::string &msg) -: std::runtime_error(sdl_error_append(msg)) { - -} - - InitSDL::InitSDL() { if (SDL_Init(SDL_INIT_EVENTS) != 0) { throw SDLError("SDL_Init(SDL_INIT_EVENTS)"); diff --git a/src/app/init.hpp b/src/app/init.hpp index 9385d44..42a4176 100644 --- a/src/app/init.hpp +++ b/src/app/init.hpp @@ -1,42 +1,14 @@ #ifndef BLANK_APP_INIT_HPP_ #define BLANK_APP_INIT_HPP_ +#include "error.hpp" + #include #include -#include -#include namespace blank { -class AlutError -: public std::runtime_error { - -public: - explicit AlutError(ALenum); - AlutError(ALenum, const std::string &); - -}; - -class SDLError -: public std::runtime_error { - -public: - SDLError(); - explicit SDLError(const std::string &); - -}; - -class NetError -: public std::runtime_error { - -public: - NetError(); - explicit NetError(const std::string &); - -}; - - class InitSDL { public: diff --git a/src/app/proc.cpp b/src/app/proc.cpp index f30c0cb..c219c72 100644 --- a/src/app/proc.cpp +++ b/src/app/proc.cpp @@ -1,5 +1,7 @@ #include "Process.hpp" +#include "error.hpp" + #ifdef _WIN32 # include # include @@ -166,18 +168,18 @@ Process::Impl::Impl( argv[args.size()] = nullptr; if (pipe(fd_in) != 0) { - throw runtime_error("failed to open pipe for child process' stdin"); + throw SysError("failed to open pipe for child process' stdin"); } if (pipe(fd_out) != 0) { - throw runtime_error("failed to open pipe for child process' stdout"); + throw SysError("failed to open pipe for child process' stdout"); } if (pipe(fd_err) != 0) { - throw runtime_error("failed to open pipe for child process' stderr"); + throw SysError("failed to open pipe for child process' stderr"); } pid = fork(); if (pid == -1) { - throw runtime_error("fork"); + throw SysError("fork"); } else if (pid == 0) { if (dup2(fd_in[0], STDIN_FILENO) == -1) { @@ -228,7 +230,7 @@ size_t Process::Impl::WriteIn(const void *buffer, size_t max_len) { if (errno == EAGAIN) { return 0; } else { - throw runtime_error("failed to write to child process' input stream"); + throw SysError("failed to write to child process' input stream"); } } return written; @@ -248,7 +250,7 @@ size_t Process::Impl::ReadOut(void *buffer, size_t max_len) { if (errno == EAGAIN) { return 0; } else { - throw runtime_error("failed to read from child process' output stream"); + throw SysError("failed to read from child process' output stream"); } } return ret; @@ -268,7 +270,7 @@ size_t Process::Impl::ReadErr(void *buffer, size_t max_len) { if (errno == EAGAIN) { return 0; } else { - throw runtime_error("failed to read from child process' error stream"); + throw SysError("failed to read from child process' error stream"); } } return ret; @@ -281,7 +283,7 @@ void Process::Impl::Terminate() { #else if (kill(pid, SIGTERM) == -1) { #endif - throw runtime_error("failed to terminate child process"); + throw SysError("failed to terminate child process"); } } @@ -307,7 +309,7 @@ int Process::Impl::Join() { int status; int result = waitpid(pid, &status, 0); if (result == -1) { - throw runtime_error("error waiting on child process"); + throw SysError("error waiting on child process"); } if (result == pid && WIFEXITED(status)) { return WEXITSTATUS(status); diff --git a/src/client/net.cpp b/src/client/net.cpp index f1e5553..879dfb1 100644 --- a/src/client/net.cpp +++ b/src/client/net.cpp @@ -3,7 +3,7 @@ #include "Client.hpp" #include "NetworkedInput.hpp" -#include "../app/init.hpp" +#include "../app/error.hpp" #include "../geometry/distance.hpp" #include "../io/WorldSave.hpp" #include "../net/Packet.hpp" diff --git a/src/graphics/render.cpp b/src/graphics/render.cpp index d1913ad..f0ead9b 100644 --- a/src/graphics/render.cpp +++ b/src/graphics/render.cpp @@ -6,7 +6,7 @@ #include "TextureBase.hpp" #include "Viewport.hpp" -#include "../app/init.hpp" +#include "../app/error.hpp" #include #include @@ -20,7 +20,7 @@ namespace blank { Font::Font(const char *src, int size, long index) : handle(TTF_OpenFontIndex(src, size, index)) { if (!handle) { - throw std::runtime_error(TTF_GetError()); + throw TTFError("TTF_OpenFontIndex"); } } @@ -109,7 +109,7 @@ bool Font::HasGlyph(Uint16 c) const noexcept { glm::ivec2 Font::TextSize(const char *text) const { glm::ivec2 size; if (TTF_SizeUTF8(handle, text, &size.x, &size.y) != 0) { - throw std::runtime_error(TTF_GetError()); + throw TTFError("TTF_SizeUTF8"); } return size; } @@ -123,7 +123,7 @@ Texture Font::Render(const char *text) const { void Font::Render(const char *text, Texture &tex) const { SDL_Surface *srf = TTF_RenderUTF8_Blended(handle, text, { 0xFF, 0xFF, 0xFF, 0xFF }); if (!srf) { - throw std::runtime_error(TTF_GetError()); + throw TTFError("TTF_RenderUTF8_Blended"); } tex.Bind(); tex.Data(*srf, false); diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index 85f0184..5a07143 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -9,7 +9,7 @@ #include "ArrayTexture.hpp" #include "CubeMap.hpp" #include "Texture.hpp" -#include "../app/init.hpp" +#include "../app/error.hpp" #include #include @@ -20,29 +20,12 @@ #include -namespace { - -void gl_error(std::string msg) { - const GLubyte *errBegin = gluErrorString(glGetError()); - if (errBegin && *errBegin != '\0') { - const GLubyte *errEnd = errBegin; - while (*errEnd != '\0') { - ++errEnd; - } - msg += ": "; - msg.append(errBegin, errEnd); - } - throw std::runtime_error(msg); -} - -} - namespace blank { Shader::Shader(GLenum type) : handle(glCreateShader(type)) { if (handle == 0) { - gl_error("glCreateShader"); + throw GLError("glCreateShader"); } } @@ -95,7 +78,7 @@ void Shader::AttachToProgram(GLuint id) const noexcept { Program::Program() : handle(glCreateProgram()) { if (handle == 0) { - gl_error("glCreateProgram"); + throw GLError("glCreateProgram"); } } diff --git a/src/graphics/viewport.cpp b/src/graphics/viewport.cpp index cb44619..a475f3e 100644 --- a/src/graphics/viewport.cpp +++ b/src/graphics/viewport.cpp @@ -3,7 +3,7 @@ #include "SkyBox.hpp" #include "Viewport.hpp" -#include "../app/init.hpp" +#include "../app/error.hpp" #include "../geometry/const.hpp" #include diff --git a/src/io/WorldSave.cpp b/src/io/WorldSave.cpp index ce7c71d..529d0d9 100644 --- a/src/io/WorldSave.cpp +++ b/src/io/WorldSave.cpp @@ -2,6 +2,7 @@ #include "filesystem.hpp" #include "TokenStreamReader.hpp" +#include "../app/error.hpp" #include #include @@ -196,7 +197,7 @@ void WorldSave::Write(Chunk &chunk) const { // check if it's because of a missing path component if (errno != ENOENT) { // nope, fatal - throw runtime_error(strerror(errno)); + throw SysError(); } string dir_path(path); dir_path.erase(dir_path.find_last_of("\\/")); diff --git a/src/io/filesystem.cpp b/src/io/filesystem.cpp index 5a4515e..bdea3a5 100644 --- a/src/io/filesystem.cpp +++ b/src/io/filesystem.cpp @@ -1,5 +1,7 @@ #include "filesystem.hpp" +#include "../app/error.hpp" + #include #include #include @@ -219,7 +221,7 @@ TempDir::TempDir() { char tmpl[] = "blank.XXXXXX"; const char *name = mkdtemp(tmpl); if (!name) { - throw runtime_error("unable to create temporary directory"); + throw SysError("unable to create temporary directory"); } path = name; #else diff --git a/src/net/net.cpp b/src/net/net.cpp index 4f7fc66..1dcb9d0 100644 --- a/src/net/net.cpp +++ b/src/net/net.cpp @@ -4,7 +4,7 @@ #include "io.hpp" #include "Packet.hpp" -#include "../app/init.hpp" +#include "../app/error.hpp" #include "../geometry/const.hpp" #include "../model/Model.hpp" #include "../world/Entity.hpp" diff --git a/src/net/tcp.cpp b/src/net/tcp.cpp index 7d8862a..6ecd2cd 100644 --- a/src/net/tcp.cpp +++ b/src/net/tcp.cpp @@ -1,6 +1,6 @@ #include "tcp.hpp" -#include "../app/init.hpp" +#include "../app/error.hpp" #include diff --git a/src/server/net.cpp b/src/server/net.cpp index f5e974d..b5590ee 100644 --- a/src/server/net.cpp +++ b/src/server/net.cpp @@ -2,7 +2,7 @@ #include "ChunkTransmitter.hpp" #include "Server.hpp" -#include "../app/init.hpp" +#include "../app/error.hpp" #include "../geometry/distance.hpp" #include "../io/WorldSave.hpp" #include "../model/Model.hpp" diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index 5ff408b..2fffcbd 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -10,7 +10,6 @@ #include "../app/Config.hpp" #include "../app/Environment.hpp" #include "../app/FrameCounter.hpp" -#include "../app/init.hpp" #include "../audio/Audio.hpp" #include "../audio/SoundBank.hpp" #include "../geometry/distance.hpp"