From 15436ad229cf69695ca97ad2cbbb0b75a1304da5 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sun, 14 Aug 2016 13:00:38 +0100 Subject: [PATCH] Refactor error class into API headers --- CMakeLists.txt | 3 +- docs/api/reference.rst | 3 + include/loot/api.h | 1 + include/loot/error.h | 154 ++++++++++++++++++ src/api/api.cpp | 2 +- src/api/api_database.cpp | 2 +- src/backend/app/loot_paths.cpp | 2 +- src/backend/app/loot_state.cpp | 2 +- src/backend/error.h | 73 --------- src/backend/game/game.cpp | 2 +- src/backend/game/game_cache.cpp | 2 +- src/backend/game/game_settings.cpp | 2 +- src/backend/game/load_order_handler.cpp | 2 +- src/backend/helpers/git_helper.cpp | 2 +- src/backend/helpers/helpers.cpp | 2 +- src/backend/masterlist.cpp | 2 +- src/backend/metadata/condition_evaluator.cpp | 2 +- src/backend/metadata/condition_grammar.h | 2 +- src/backend/metadata/message.cpp | 2 +- src/backend/metadata/plugin_metadata.cpp | 2 +- src/backend/metadata_list.cpp | 2 +- src/backend/plugin/plugin_sorter.cpp | 2 +- src/gui/query_handler.cpp | 2 +- src/tests/backend/game/game_test.h | 2 +- .../backend/game/load_order_handler_test.h | 2 +- src/tests/backend/helpers/git_helper_test.h | 2 +- src/tests/backend/helpers/helpers_test.h | 2 +- .../backend/metadata/condition_grammar_test.h | 2 +- .../metadata/conditional_metadata_test.h | 2 +- 29 files changed, 184 insertions(+), 98 deletions(-) create mode 100644 include/loot/error.h delete mode 100644 src/backend/error.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5989688a..eb084cf7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -217,7 +217,7 @@ set (LOOT_HEADERS "${CMAKE_SOURCE_DIR}/src/backend/app/loot_paths.h" "${CMAKE_SOURCE_DIR}/src/backend/helpers/language.h" "${CMAKE_SOURCE_DIR}/src/backend/helpers/version.h" "${CMAKE_SOURCE_DIR}/src/backend/helpers/yaml_set_helpers.h" - "${CMAKE_SOURCE_DIR}/src/backend/error.h" + "${CMAKE_SOURCE_DIR}/include/loot/error.h" "${CMAKE_SOURCE_DIR}/include/loot/game_type.h" "${CMAKE_SOURCE_DIR}/include/loot/language_code.h" "${CMAKE_SOURCE_DIR}/include/loot/loot_version.h" @@ -242,6 +242,7 @@ set (LOOT_API_SRC "${CMAKE_BINARY_DIR}/generated/loot_version.cpp" set (LOOT_API_HEADERS "${CMAKE_SOURCE_DIR}/include/loot/api.h" "${CMAKE_SOURCE_DIR}/include/loot/database_interface.h" + "${CMAKE_SOURCE_DIR}/include/loot/error.h" "${CMAKE_SOURCE_DIR}/include/loot/game_type.h" "${CMAKE_SOURCE_DIR}/include/loot/language_code.h" "${CMAKE_SOURCE_DIR}/include/loot/loot_version.h" diff --git a/docs/api/reference.rst b/docs/api/reference.rst index 6c7d4619..2c3e6ce7 100644 --- a/docs/api/reference.rst +++ b/docs/api/reference.rst @@ -43,5 +43,8 @@ Interfaces Classes ======= +.. doxygenclass:: loot::Error + :members: + .. doxygenclass:: loot::LootVersion :members: diff --git a/include/loot/api.h b/include/loot/api.h index bff43b5e..1acfa287 100644 --- a/include/loot/api.h +++ b/include/loot/api.h @@ -33,6 +33,7 @@ #include #include "loot/database_interface.h" +#include "loot/error.h" #include "loot/game_type.h" #include "loot/loot_version.h" diff --git a/include/loot/error.h b/include/loot/error.h new file mode 100644 index 00000000..c4ca159a --- /dev/null +++ b/include/loot/error.h @@ -0,0 +1,154 @@ +/* LOOT + + A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and + Fallout: New Vegas. + + Copyright (C) 2012-2016 WrinklyNinja + + This file is part of LOOT. + + LOOT is free software: you can redistribute + it and/or modify it under the terms of the GNU General Public License + as published by the Free Software Foundation, either version 3 of + the License, or (at your option) any later version. + + LOOT is distributed in the hope that it will + be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LOOT. If not, see + . + */ + +#ifndef LOOT_ERROR +#define LOOT_ERROR + +#include +#include + +/* set up dll import/export decorators +when compiling the dll on windows, ensure LOOT_EXPORT is defined. clients +that use this header do not need to define anything to import the symbols +properly. */ +#if defined(_WIN32) +# ifdef LOOT_STATIC +# define LOOT_API +# elif defined LOOT_EXPORT +# define LOOT_API __declspec(dllexport) +# else +# define LOOT_API __declspec(dllimport) +# endif +#else +# define LOOT_API +#endif + +/** + * @file + * @brief Contains the Error class used for some exceptions thrown. + */ + +namespace loot { +/** + * @brief A class that defines the type of objects thrown as errors by the LOOT + * API. + * @details Note that not all exceptions thrown from the LOOT API use this type, + * so catch `std::exception` too. + */ +class Error : public std::exception { +public: + /** + * @brief A code indicating the type of error that occurred. + */ + enum struct Code : unsigned int { + /** + * No error occurred. This is used internally to indicate that an operation + * failed, but its failure was not fatal to the task being performed. + */ + ok = 0, + /** An error was encountered when reading or writing the load order. */ + liblo_error = 1, + /** An error was encountered when writing a file. */ + path_write_fail = 2, + /** An error was encountered when reading a file. */ + path_read_fail = 3, + /** + * An error was encountered when attempting to evaluate a metadata + * condition. + */ + condition_eval_fail = 4, + /** An error was encountered when parsing a regular expression. */ + regex_eval_fail = 5, + /** An error was encountered when trying to allocate memory. */ + no_mem = 6, + /** An error was encountered due to invalid function arguments. */ + invalid_args = 7, + /** + * @deprecated + * @brief An error was encountered when getting plugin tags as no tag map + * was built. **This code is obsolete and no longer used.** + */ + no_tag_map = 8, + /** A path could not be found. */ + path_not_found = 9, + /** None of LOOT's supported games could be detected. */ + no_game_detected = 10, + /** + * @deprecated + * @brief An error was encountered while trying to run a Subversion command. + * **This code is obsolete and no longer used.** + */ + subversion_error = 11, + /** + * An error was encountered while trying to create or interact with a Git + * repository. + */ + git_error = 12, + /** A miscellaneous error occurred when using an operating system API. */ + windows_error = 13, + /** An error occurred while trying to sort the load order. */ + sorting_error = 14, + }; + + /** + * @brief Construct an error object giving a code and error message. + */ + Error(const Code code_arg, const std::string& what_arg) : code_(code_arg), what_(what_arg) {} + ~Error() throw() {}; + + /** + * @brief Get the code describing the type of error that occurred. + * @return An error code. + */ + Code code() const { return code_; } + + /** + * @brief A utility function that casts the code to an unsigned int. + * @return An error code's unsigned integer value. + */ + unsigned int codeAsUnsignedInt() const { + return asUnsignedInt(code_); + } + + /** + * @brief Get the message that describes the error details. + * @return A string describing the error that occurred. + */ + const char * what() const throw() { return what_.c_str(); } + + /** + * A utility function that converts a given error code to an unsigned int. + * @param code The code to convert. + * @return The error code's unsigned integer value. + */ + static unsigned int asUnsignedInt(Code code) { + return static_cast(code); + } +private: + Code code_; + std::string what_; +}; +} + +#endif diff --git a/src/api/api.cpp b/src/api/api.cpp index 140e50bc..433f3e10 100644 --- a/src/api/api.cpp +++ b/src/api/api.cpp @@ -28,7 +28,7 @@ #include #include "api/api_database.h" -#include "backend/error.h" +#include "loot/error.h" #include "backend/app/loot_paths.h" namespace loot { diff --git a/src/api/api_database.cpp b/src/api/api_database.cpp index 58d6cc13..fdcfb749 100644 --- a/src/api/api_database.cpp +++ b/src/api/api_database.cpp @@ -27,7 +27,7 @@ #include #include -#include "backend/error.h" +#include "loot/error.h" #include "backend/game/game.h" #include "backend/plugin/plugin_sorter.h" diff --git a/src/backend/app/loot_paths.cpp b/src/backend/app/loot_paths.cpp index 216b73df..11618057 100644 --- a/src/backend/app/loot_paths.cpp +++ b/src/backend/app/loot_paths.cpp @@ -28,7 +28,7 @@ Fallout: New Vegas. #include -#include "backend/error.h" +#include "loot/error.h" #include "backend/helpers/helpers.h" #ifdef _WIN32 diff --git a/src/backend/app/loot_state.cpp b/src/backend/app/loot_state.cpp index 035a456c..bdae4012 100644 --- a/src/backend/app/loot_state.cpp +++ b/src/backend/app/loot_state.cpp @@ -34,7 +34,7 @@ #include #include -#include "backend/error.h" +#include "loot/error.h" #include "backend/app/loot_paths.h" #include "backend/helpers/helpers.h" #include "backend/helpers/language.h" diff --git a/src/backend/error.h b/src/backend/error.h deleted file mode 100644 index 3b44e652..00000000 --- a/src/backend/error.h +++ /dev/null @@ -1,73 +0,0 @@ -/* LOOT - - A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and - Fallout: New Vegas. - - Copyright (C) 2012-2016 WrinklyNinja - - This file is part of LOOT. - - LOOT is free software: you can redistribute - it and/or modify it under the terms of the GNU General Public License - as published by the Free Software Foundation, either version 3 of - the License, or (at your option) any later version. - - LOOT is distributed in the hope that it will - be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with LOOT. If not, see - . - */ - -#ifndef LOOT_BACKEND_ERROR -#define LOOT_BACKEND_ERROR - -#include -#include - -namespace loot { -class Error : public std::exception { -public: - enum struct Code : unsigned int { - // These must not be changed for API stability. - ok = 0, - liblo_error = 1, - path_write_fail = 2, - path_read_fail = 3, - condition_eval_fail = 4, - regex_eval_fail = 5, - no_mem = 6, - invalid_args = 7, - no_tag_map = 8, - path_not_found = 9, - no_game_detected = 10, - //11 was subversion_error, and was removed along with svn support. - git_error = 12, - windows_error = 13, - sorting_error = 14, - }; - - Error(const Code code_arg, const std::string& what_arg) : code_(code_arg), what_(what_arg) {} - ~Error() throw() {}; - - Code code() const { return code_; } - - unsigned int codeAsUnsignedInt() const { - return asUnsignedInt(code_); - } - - const char * what() const throw() { return what_.c_str(); } - - static unsigned int asUnsignedInt(Code code) { - return static_cast(code); - } -private: - Code code_; - std::string what_; -}; -} - -#endif diff --git a/src/backend/game/game.cpp b/src/backend/game/game.cpp index 35d9e54d..dc7fbdd3 100644 --- a/src/backend/game/game.cpp +++ b/src/backend/game/game.cpp @@ -32,7 +32,7 @@ #include #include "backend/app/loot_paths.h" -#include "backend/error.h" +#include "loot/error.h" #include "backend/helpers/helpers.h" using boost::locale::translate; diff --git a/src/backend/game/game_cache.cpp b/src/backend/game/game_cache.cpp index 2fc4c824..a49bb377 100644 --- a/src/backend/game/game_cache.cpp +++ b/src/backend/game/game_cache.cpp @@ -30,7 +30,7 @@ #include #include -#include "backend/error.h" +#include "loot/error.h" #include "backend/helpers/helpers.h" using boost::locale::to_lower; diff --git a/src/backend/game/game_settings.cpp b/src/backend/game/game_settings.cpp index d9555a7f..36e7aea6 100644 --- a/src/backend/game/game_settings.cpp +++ b/src/backend/game/game_settings.cpp @@ -29,7 +29,7 @@ #include #include "backend/app/loot_paths.h" -#include "backend/error.h" +#include "loot/error.h" #include "backend/helpers/helpers.h" namespace fs = boost::filesystem; diff --git a/src/backend/game/load_order_handler.cpp b/src/backend/game/load_order_handler.cpp index 03879811..528db8f9 100644 --- a/src/backend/game/load_order_handler.cpp +++ b/src/backend/game/load_order_handler.cpp @@ -28,7 +28,7 @@ #include #include -#include "backend/error.h" +#include "loot/error.h" using boost::locale::translate; using std::string; diff --git a/src/backend/helpers/git_helper.cpp b/src/backend/helpers/git_helper.cpp index aac0c9a9..ed227e1e 100644 --- a/src/backend/helpers/git_helper.cpp +++ b/src/backend/helpers/git_helper.cpp @@ -28,7 +28,7 @@ #include #include -#include "backend/error.h" +#include "loot/error.h" using boost::locale::translate; using std::string; diff --git a/src/backend/helpers/helpers.cpp b/src/backend/helpers/helpers.cpp index 951045aa..48cf6f0b 100644 --- a/src/backend/helpers/helpers.cpp +++ b/src/backend/helpers/helpers.cpp @@ -40,7 +40,7 @@ #include #include -#include "backend/error.h" +#include "loot/error.h" #ifdef _WIN32 # ifndef UNICODE diff --git a/src/backend/masterlist.cpp b/src/backend/masterlist.cpp index 2b19556d..06ee9700 100644 --- a/src/backend/masterlist.cpp +++ b/src/backend/masterlist.cpp @@ -26,7 +26,7 @@ #include -#include "backend/error.h" +#include "loot/error.h" #include "backend/game/game.h" #include "backend/helpers/git_helper.h" diff --git a/src/backend/metadata/condition_evaluator.cpp b/src/backend/metadata/condition_evaluator.cpp index c4565c83..f922d01e 100644 --- a/src/backend/metadata/condition_evaluator.cpp +++ b/src/backend/metadata/condition_evaluator.cpp @@ -26,7 +26,7 @@ #include -#include "backend/error.h" +#include "loot/error.h" #include "backend/helpers/helpers.h" namespace loot { diff --git a/src/backend/metadata/condition_grammar.h b/src/backend/metadata/condition_grammar.h index 5fb0db40..459b08cd 100644 --- a/src/backend/metadata/condition_grammar.h +++ b/src/backend/metadata/condition_grammar.h @@ -46,7 +46,7 @@ #include #include -#include "backend/error.h" +#include "loot/error.h" #include "backend/game/game.h" #include "backend/helpers/helpers.h" #include "backend/helpers/version.h" diff --git a/src/backend/metadata/message.cpp b/src/backend/metadata/message.cpp index 6cfacc26..9a114049 100644 --- a/src/backend/metadata/message.cpp +++ b/src/backend/metadata/message.cpp @@ -26,7 +26,7 @@ #include -#include "backend/error.h" +#include "loot/error.h" #include "backend/game/game.h" #include "backend/helpers/language.h" diff --git a/src/backend/metadata/plugin_metadata.cpp b/src/backend/metadata/plugin_metadata.cpp index 12606647..6aec7bb6 100644 --- a/src/backend/metadata/plugin_metadata.cpp +++ b/src/backend/metadata/plugin_metadata.cpp @@ -32,7 +32,7 @@ #include #include -#include "backend/error.h" +#include "loot/error.h" #include "backend/game/game.h" #include "backend/helpers/helpers.h" diff --git a/src/backend/metadata_list.cpp b/src/backend/metadata_list.cpp index b595b7fa..95b0416d 100644 --- a/src/backend/metadata_list.cpp +++ b/src/backend/metadata_list.cpp @@ -28,7 +28,7 @@ #include #include -#include "backend/error.h" +#include "loot/error.h" #include "backend/game/game.h" using std::list; diff --git a/src/backend/plugin/plugin_sorter.cpp b/src/backend/plugin/plugin_sorter.cpp index 5a3ae221..5b212464 100644 --- a/src/backend/plugin/plugin_sorter.cpp +++ b/src/backend/plugin/plugin_sorter.cpp @@ -34,7 +34,7 @@ #include #include -#include "backend/error.h" +#include "loot/error.h" #include "backend/game/game.h" #include "backend/helpers/helpers.h" diff --git a/src/gui/query_handler.cpp b/src/gui/query_handler.cpp index 6b810838..5bc75878 100644 --- a/src/gui/query_handler.cpp +++ b/src/gui/query_handler.cpp @@ -41,7 +41,7 @@ #include "gui/loot_handler.h" #include "gui/resource.h" -#include "backend/error.h" +#include "loot/error.h" #include "backend/app/loot_paths.h" #include "backend/plugin/plugin_sorter.h" #include "backend/helpers/helpers.h" diff --git a/src/tests/backend/game/game_test.h b/src/tests/backend/game/game_test.h index 3b00f26d..bef3d7ce 100644 --- a/src/tests/backend/game/game_test.h +++ b/src/tests/backend/game/game_test.h @@ -28,7 +28,7 @@ along with LOOT. If not, see #include "backend/game/game.h" #include "backend/app/loot_paths.h" -#include "backend/error.h" +#include "loot/error.h" #include "tests/backend/game/load_order_handler_test.h" namespace loot { diff --git a/src/tests/backend/game/load_order_handler_test.h b/src/tests/backend/game/load_order_handler_test.h index 55bbbca2..5ab5aff9 100644 --- a/src/tests/backend/game/load_order_handler_test.h +++ b/src/tests/backend/game/load_order_handler_test.h @@ -27,7 +27,7 @@ along with LOOT. If not, see #include "backend/game/load_order_handler.h" -#include "backend/error.h" +#include "loot/error.h" #include "tests/backend/base_game_test.h" namespace loot { diff --git a/src/tests/backend/helpers/git_helper_test.h b/src/tests/backend/helpers/git_helper_test.h index b3de722c..8954f6e3 100644 --- a/src/tests/backend/helpers/git_helper_test.h +++ b/src/tests/backend/helpers/git_helper_test.h @@ -29,7 +29,7 @@ along with LOOT. If not, see #include -#include "backend/error.h" +#include "loot/error.h" namespace loot { namespace test { diff --git a/src/tests/backend/helpers/helpers_test.h b/src/tests/backend/helpers/helpers_test.h index c47b4d0c..76ead90f 100644 --- a/src/tests/backend/helpers/helpers_test.h +++ b/src/tests/backend/helpers/helpers_test.h @@ -27,7 +27,7 @@ along with LOOT. If not, see #include "backend/helpers/helpers.h" -#include "backend/error.h" +#include "loot/error.h" #include "tests/backend/base_game_test.h" namespace loot { diff --git a/src/tests/backend/metadata/condition_grammar_test.h b/src/tests/backend/metadata/condition_grammar_test.h index 086b5f32..f5300f46 100644 --- a/src/tests/backend/metadata/condition_grammar_test.h +++ b/src/tests/backend/metadata/condition_grammar_test.h @@ -27,7 +27,7 @@ along with LOOT. If not, see #include "backend/metadata/condition_grammar.h" -#include "backend/error.h" +#include "loot/error.h" #include "tests/backend/base_game_test.h" namespace loot { diff --git a/src/tests/backend/metadata/conditional_metadata_test.h b/src/tests/backend/metadata/conditional_metadata_test.h index 5111b1ae..988deb44 100644 --- a/src/tests/backend/metadata/conditional_metadata_test.h +++ b/src/tests/backend/metadata/conditional_metadata_test.h @@ -27,7 +27,7 @@ along with LOOT. If not, see #include "backend/metadata/conditional_metadata.h" -#include "backend/error.h" +#include "loot/error.h" #include "tests/backend/base_game_test.h" namespace loot {