From 5bee4515c1089ce3499bc3b74780db94f0c02306 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 15 Nov 2024 20:26:26 +0100 Subject: process feedback on #26 --- src/crepe/facade/DB.cpp | 24 +++++++++++++++--------- src/crepe/facade/DB.h | 7 ++++--- src/crepe/facade/SDLContext.cpp | 5 ++--- 3 files changed, 21 insertions(+), 15 deletions(-) (limited to 'src/crepe/facade') diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp index 80047a6..322ecc0 100644 --- a/src/crepe/facade/DB.cpp +++ b/src/crepe/facade/DB.cpp @@ -1,6 +1,5 @@ #include -#include "Exception.h" #include "util/Log.h" #include "DB.h" @@ -15,18 +14,18 @@ DB::DB(const string & path) { // init database struct libdb::DB * db; if ((ret = libdb::db_create(&db, NULL, 0)) != 0) - throw Exception("db_create: {}", libdb::db_strerror(ret)); + throw runtime_error(format("db_create: {}", libdb::db_strerror(ret))); this->db = {db, [](libdb::DB * db) { db->close(db, 0); }}; // load or create database file ret = this->db->open(this->db.get(), NULL, path.c_str(), NULL, libdb::DB_BTREE, DB_CREATE, 0); - if (ret != 0) throw Exception("db->open: {}", libdb::db_strerror(ret)); + if (ret != 0) throw runtime_error(format("db->open: {}", libdb::db_strerror(ret))); // create cursor libdb::DBC * cursor; ret = this->db->cursor(this->db.get(), NULL, &cursor, 0); - if (ret != 0) throw Exception("db->cursor: {}", libdb::db_strerror(ret)); + if (ret != 0) throw runtime_error(format("db->cursor: {}", libdb::db_strerror(ret))); this->cursor = {cursor, [](libdb::DBC * cursor) { cursor->close(cursor); }}; } @@ -44,21 +43,28 @@ string DB::get(const string & key) { memset(&db_val, 0, sizeof(libdb::DBT)); int ret = this->cursor->get(this->cursor.get(), &db_key, &db_val, DB_FIRST); - if (ret != 0) throw Exception("cursor->get: {}", libdb::db_strerror(ret)); - return {static_cast(db_val.data), db_val.size}; + if (ret == 0) + return {static_cast(db_val.data), db_val.size}; + + string err = format("cursor->get: {}", libdb::db_strerror(ret)); + if (ret == DB_NOTFOUND) + throw out_of_range(err); + else + throw runtime_error(err); } void DB::set(const string & key, const string & value) { libdb::DBT db_key = this->to_thing(key); libdb::DBT db_val = this->to_thing(value); int ret = this->db->put(this->db.get(), NULL, &db_key, &db_val, 0); - if (ret != 0) throw Exception("cursor->get: {}", libdb::db_strerror(ret)); + if (ret != 0) + throw runtime_error(format("cursor->get: {}", libdb::db_strerror(ret))); } -bool DB::has(const std::string & key) noexcept { +bool DB::has(const std::string & key) { try { this->get(key); - } catch (...) { + } catch (std::out_of_range &) { return false; } return true; diff --git a/src/crepe/facade/DB.h b/src/crepe/facade/DB.h index 7c757a2..2112216 100644 --- a/src/crepe/facade/DB.h +++ b/src/crepe/facade/DB.h @@ -34,7 +34,8 @@ public: * * \return The value * - * \throws Exception if value is not found in DB or other error occurs + * \throws std::out_of_range if value is not found in DB + * \throws std::runtime_error if other error occurs */ std::string get(const std::string & key); /** @@ -43,7 +44,7 @@ public: * \param key The value key * \param value The value to store * - * \throws Exception if an error occurs + * \throws std::runtime_error if an error occurs */ void set(const std::string & key, const std::string & value); /** @@ -53,7 +54,7 @@ public: * * \returns True if the key exists, or false if it does not */ - bool has(const std::string & key) noexcept; + bool has(const std::string & key); private: //! RAII wrapper around \c DB struct diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index a68d940..65ea962 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -10,17 +10,16 @@ #include #include #include -#include #include "../api/Sprite.h" #include "../api/Texture.h" #include "../api/Transform.h" #include "../util/Log.h" -#include "Exception.h" #include "SDLContext.h" using namespace crepe; +using namespace std; SDLContext & SDLContext::get_instance() { static SDLContext instance; @@ -171,7 +170,7 @@ SDLContext::texture_from_path(const std::string & path) { this->game_renderer.get(), img_surface.get()); if (tmp_texture == nullptr) { - throw Exception("Texture cannot be load from {}", path); + throw runtime_error(format("Texture cannot be load from {}", path)); } std::unique_ptr> -- cgit v1.2.3