Skip to content

Commit 6526836

Browse files
Merge pull request #53 from Reim-developer/dev
Implement new error handling with `ErrorTypes`
2 parents 17b10fd + a5a2d1c commit 6526836

5 files changed

Lines changed: 71 additions & 42 deletions

File tree

src/ffi/raw/config.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ enum WriteConfigStatus : uint8_t {
1414
CREATE_DIR_FAILED,
1515
CREATE_FILE_FAILED,
1616
WRITE_FILE_FAILED,
17-
GET_DATA_LOCAL_FAILED,
17+
GET_CONFIG_DIR_FAILED,
1818
};
1919

2020
char *raw_config_dir();

src/front_end/include/error_debug.hxx

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/front_end/main_window_preload.cxx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@
1616

1717
#if defined(LAZY_DEBUG)
1818
#include <iostream>
19-
20-
#include "include/error_debug.hxx"
2119
using std::cout;
2220
#endif
2321

22+
#include "../front_end_utils/include/error_types.hxx"
23+
2424
using Lazyboard::front_end::MainWindowPreload;
2525
using Self = MainWindowPreload;
2626

2727
using std::make_unique;
2828
using std::string;
2929
using std::stringstream;
3030
using namespace Lazyboard::ffi;
31+
using Lazyboard::front_end_utils::error_types_map;
3132

3233
void Self::on_gen_default_cfg_error(WriteConfigStatus status,
3334
QMainWindow *main_window) {
@@ -41,7 +42,7 @@ void Self::on_gen_default_cfg_error(WriteConfigStatus status,
4142

4243
break;
4344

44-
case WriteConfigStatus::GET_DATA_LOCAL_FAILED:
45+
case WriteConfigStatus::GET_CONFIG_DIR_FAILED:
4546
QMessageBox::critical(main_window, "Error",
4647
"Could not get application local data");
4748
break;

src/front_end/theme_manager.cxx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111

1212
#if defined(LAZY_DEBUG)
1313
#include <iostream>
14-
15-
#include "include/error_debug.hxx"
1614
using std::cout;
1715
#endif
1816

17+
#include "../front_end_utils/include/error_types.hxx"
1918
using namespace Lazyboard::ffi;
19+
using Lazyboard::front_end_utils::error_to_string;
20+
using Lazyboard::front_end_utils::ErrorTypes;
2021

2122
using std::make_unique;
2223
using std::string;
@@ -52,18 +53,21 @@ void Self::set_main_window_theme(QMainWindow *main_window,
5253
if (!bg_hex.isValid() || !fg_hex.isValid() || !bg_btn_hex.isValid() ||
5354
!fg_btn_hex.isValid() || !bg_header_table_hex.isValid() ||
5455
!fg_header_table_hex.isValid()) {
55-
QMessageBox::critical(
56-
main_window, "Error",
57-
"Invalid HEX color, please check your configuration");
56+
// clang-format off
57+
const auto error_name = error_to_string(ErrorTypes::INVALID_HEX_COLOR);
58+
const auto error_message = QString(R"(
59+
Invalid HEX color, please check your TOML configuration
60+
Error name: %1
61+
)").arg(error_name.data()); // clang-format on
62+
63+
QMessageBox::critical(main_window, "Error", error_message);
5864

5965
// clang-format off
6066
#if defined (LAZY_DEBUG)
61-
const auto &type_err = error_type().at(ErrorDebugType::INVALID_HEX_COLOR);
62-
6367
stringstream string_stream_debug;
6468
string_stream_debug
6569
<< "[DEBUG] Found error when load TOML configuration:\n"
66-
<< "[DEBUG] Error Type: " << type_err << "\n"
70+
<< "[DEBUG] Error Type: " << error_name << "\n"
6771
<< "[DEBUG] " << bg_color << "\n"
6872
<< "[DEBUG] " << fg_color << "\n"
6973
<< "[DEBUG] " << bg_button_color << "\n"
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#ifndef ERROR_TYPES_HXX
2+
#define ERROR_TYPES_HXX
3+
4+
#include <array>
5+
#include <cstdint>
6+
#include <initializer_list>
7+
#include <map>
8+
#include <string_view>
9+
#include <utility>
10+
11+
namespace Lazyboard::front_end_utils {
12+
13+
using std::array;
14+
using std::initializer_list;
15+
using std::map;
16+
using std::pair;
17+
using std::string_view;
18+
using std::uint8_t;
19+
20+
enum class ErrorTypes : uint8_t {
21+
INVALID_HEX_COLOR = 1,
22+
CREATE_DIR_FAILED,
23+
GET_CONFIG_DIR_FAILED,
24+
CREATE_FILE_FAILED,
25+
TOML_TO_STRING_FAILED,
26+
};
27+
28+
using Types = initializer_list<pair<ErrorTypes, string_view>>;
29+
inline constexpr Types error_types_map() noexcept {
30+
using E = ErrorTypes;
31+
32+
const Types error_types = {
33+
pair{E::INVALID_HEX_COLOR, "INVALID_HEX_COLOR"},
34+
pair{E::CREATE_DIR_FAILED, "CREATE_DIR_FAILED"},
35+
pair{E::GET_CONFIG_DIR_FAILED, "GET_CONFIG_DIR_FAILED"},
36+
pair{E::CREATE_FILE_FAILED, "CREATE_FILE_FAILED"},
37+
pair{E::TOML_TO_STRING_FAILED, "TOML_TO_STRING_FAILED"},
38+
};
39+
40+
return error_types;
41+
}
42+
43+
inline constexpr string_view error_to_string(ErrorTypes error) noexcept {
44+
for (const auto &error_types : error_types_map()) {
45+
if (error_types.first == error) {
46+
return error_types.second;
47+
}
48+
}
49+
50+
return "UNKOWN_ERROR";
51+
}
52+
53+
} // namespace Lazyboard::front_end_utils
54+
#endif // ERROR_TYPES_HXX

0 commit comments

Comments
 (0)