Skip to content

Commit ba7c985

Browse files
Merge pull request #57 from Reim-developer/dev
Implement new error handling with `ErrorTypes`
2 parents cf52f7a + fa7ae73 commit ba7c985

6 files changed

Lines changed: 94 additions & 39 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef SQLITE_HXX
2+
#define SQLITE_HXX
3+
#include "../../raw/sqlite.hxx"
4+
5+
namespace Lazyboard::ffi {
6+
inline auto init_clipboard_cache(const char *path) -> InitDatabaseStatus {
7+
auto status = raw_init_clipboard_cache(path);
8+
9+
return status;
10+
}
11+
} // namespace Lazyboard::ffi
12+
13+
#endif // SQLITE_HXX

src/ffi/raw/sqlite.hxx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef RAW_SQLITE_HXX
2+
#define RAW_SQLITE_HXX
3+
4+
#if defined(__cplusplus)
5+
extern "C" {
6+
#endif
7+
8+
#include <cstdint>
9+
using std::uint8_t;
10+
11+
enum class InitDatabaseStatus : uint8_t {
12+
OK,
13+
CREATE_DATABASE_FAILED,
14+
C_STR_CONVERT_FAILED,
15+
EXECUTE_SQL_FAILED
16+
};
17+
18+
auto raw_init_clipboard_cache(const char *path) -> InitDatabaseStatus;
19+
20+
#if defined(__cplusplus)
21+
}
22+
#endif
23+
24+
#endif // RAW_SQLITE_HXX

src/front_end/main_window_preload.cxx

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ using std::cout;
2020
#endif
2121

2222
#include "../front_end_utils/include/error_types.hxx"
23+
#include "../front_end_utils/include/utils.hxx"
2324

2425
using Lazyboard::front_end::MainWindowPreload;
2526
using Self = MainWindowPreload;
@@ -28,74 +29,67 @@ using std::make_unique;
2829
using std::string;
2930
using std::stringstream;
3031
using namespace Lazyboard::ffi;
31-
using Lazyboard::front_end_utils::error_types_map;
32+
using Lazyboard::front_end_utils::error_dialog_show;
33+
using Lazyboard::front_end_utils::ErrorTypes;
3234

3335
void Self::on_gen_default_cfg_error(WriteConfigStatus status,
3436
QMainWindow *main_window) {
3537
switch (status) {
38+
using E = ErrorTypes;
39+
3640
case WriteConfigStatus::OK:
3741
break;
3842

3943
case WriteConfigStatus::CREATE_DIR_FAILED:
40-
QMessageBox::critical(main_window, "Error",
41-
"Could not create config directory");
44+
error_dialog_show(main_window, E::CREATE_DIR_FAILED);
4245

4346
break;
4447

4548
case WriteConfigStatus::GET_CONFIG_DIR_FAILED:
46-
QMessageBox::critical(main_window, "Error",
47-
"Could not get application local data");
49+
error_dialog_show(main_window, E::GET_CONFIG_DIR_FAILED);
4850
break;
4951

5052
case WriteConfigStatus::CREATE_FILE_FAILED:
51-
QMessageBox::critical(main_window, "Error",
52-
"Could not create configuration file");
53+
error_dialog_show(main_window, E::CREATE_FILE_FAILED);
5354
break;
5455

5556
case WriteConfigStatus::WRITE_FILE_FAILED:
56-
QMessageBox::critical(main_window, "Error",
57-
"Could not write configuration");
57+
error_dialog_show(main_window, E::WRITE_FILE_FAILED);
5858
break;
5959

6060
case WriteConfigStatus::TOML_TO_STRING_FAILED:
61-
QMessageBox::critical(main_window, "Error",
62-
"Could not convert TOML data to string");
61+
error_dialog_show(main_window, E::TOML_TO_STRING_FAILED);
6362
break;
6463
}
6564
}
6665

6766
void Self::on_read_exists_cfg_error(RawReadAppConfigStatus status,
6867
QMainWindow *main_window) {
6968
using Status = RawReadAppConfigStatus;
69+
using E = ErrorTypes;
7070

7171
switch (status) {
7272
case Status::READ_OK:
7373
break;
7474

7575
case Status::CONVERT_TO_MUT_FAILED:
76-
QMessageBox::critical(main_window, "Error",
77-
"Could not convert to *mut c_char");
76+
error_dialog_show(main_window, E::CONVERT_TO_MUT_FAILED);
7877
break;
7978

8079
case Status::PARSE_TOML_FAILED:
81-
QMessageBox::critical(main_window, "Error",
82-
"Parse TOML configuration failed");
80+
error_dialog_show(main_window, E::PARSE_TOML_FAILED);
8381
break;
8482

8583
case Status::READ_FILE_FAILED:
86-
QMessageBox::critical(main_window, "Error",
87-
"Could not read configuration file");
84+
error_dialog_show(main_window, E::READ_FILE_FAILED);
8885
break;
8986

9087
case Status::UTF_8_ERROR:
91-
QMessageBox::critical(main_window, "Error",
92-
"UTF-8 error in configuration file");
88+
error_dialog_show(main_window, E::UTF_8_ERROR);
9389
break;
9490

9591
case Status::CONVERT_TO_C_STR_FAILED:
96-
QMessageBox::critical(
97-
main_window, "Error",
98-
"Could not convert value configuration to C string");
92+
error_dialog_show(main_window, E::CONVERT_TO_C_STR_FAILED);
9993
break;
10094
}
10195
}
@@ -122,9 +116,8 @@ void Self::create_default_config(QMainWindow *main_window) {
122116

123117
// clang-format off
124118
#if defined(LAZY_DEBUG)
125-
cout << "[DEBUG] " << "Config path not found, generate at: " << config_path
126-
<< "\n";
127-
#endif
119+
cout << "[DEBUG] " << "Config path not found, generate at: " << config_path << "\n";
120+
#endif
128121

129122
return;
130123
}
@@ -137,8 +130,8 @@ void Self::create_default_config(QMainWindow *main_window) {
137130
void Self::read_if_exists_config(QMainWindow *main_window) {
138131
raw_app_config = make_unique<RawAppConfig>();
139132
theme_manager = make_unique<ThemeManager>();
140-
auto config_path = this->application_config();
141133

134+
auto config_path = this->application_config();
142135
auto status = exists_config(config_path.data(), raw_app_config.get());
143136
this->on_read_exists_cfg_error(status, main_window);
144137

src/front_end/theme_manager.cxx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ using std::cout;
1919
#endif
2020

2121
using namespace Lazyboard::ffi;
22+
using Lazyboard::front_end_utils::error_dialog_show;
2223
using Lazyboard::front_end_utils::error_to_string;
2324
using Lazyboard::front_end_utils::ErrorTypes;
2425
using Lazyboard::front_end_utils::is_valid_hex_color;
@@ -51,14 +52,9 @@ auto Self::gui_settings(RawAppConfig *raw_app_config) noexcept -> GuiSettings {
5152
}
5253

5354
void Self::on_invalid_hex_color_error(QMainWindow *main_window) {
54-
// clang-format off
55-
const auto error_name = error_to_string(ErrorTypes::INVALID_HEX_COLOR);
56-
const auto error_message = QString(R"(
57-
Invalid HEX color, please check your TOML configuration
58-
Error name: %1
59-
)").arg(error_name.data()); // clang-format on
55+
using E = ErrorTypes;
6056

61-
QMessageBox::critical(main_window, "Error", error_message);
57+
error_dialog_show(main_window, E::INVALID_HEX_COLOR);
6258
}
6359

6460
void Self::set_main_window_theme(QMainWindow *main_window,

src/front_end_utils/include/error_types.hxx

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,35 @@ enum class ErrorTypes : uint8_t {
2323
GET_CONFIG_DIR_FAILED,
2424
CREATE_FILE_FAILED,
2525
TOML_TO_STRING_FAILED,
26+
WRITE_FILE_FAILED,
27+
CONVERT_TO_MUT_FAILED,
28+
CONVERT_TO_C_STR_FAILED,
29+
PARSE_TOML_FAILED,
30+
READ_FILE_FAILED,
31+
UTF_8_ERROR,
2632
};
2733

2834
using Types = initializer_list<pair<ErrorTypes, string_view>>;
2935
inline constexpr Types error_types_map() noexcept {
3036
using E = ErrorTypes;
3137

3238
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"},
39+
{E::INVALID_HEX_COLOR,
40+
"Invalid HEX color, please check your TOML configuration and try "
41+
"again"},
42+
{E::CREATE_DIR_FAILED, "Create directory failed"},
43+
{E::GET_CONFIG_DIR_FAILED, "Get configuration directory failed"},
44+
{E::CREATE_FILE_FAILED, "Create file faled"},
45+
{E::TOML_TO_STRING_FAILED, "Could not convert TOML to string"},
46+
{E::WRITE_FILE_FAILED, "Could not write file"},
47+
{E::CONVERT_TO_MUT_FAILED, "Could not convert value to '*mut c_char'"},
48+
{E::CONVERT_TO_C_STR_FAILED, "Could not convert value to 'c_str'"},
49+
{E::PARSE_TOML_FAILED,
50+
"Could not parse TOML, please check your configuration and try "
51+
"again"},
52+
{E::READ_FILE_FAILED, "Could not read file"},
53+
{E::UTF_8_ERROR, "UTF-8 error"},
54+
3855
};
3956

4057
return error_types;

src/front_end_utils/include/utils.hxx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22
#define FRONT_END_UTILS_HXX
33

44
#include <qcolor.h>
5+
#include <qcontainerfwd.h>
56
#include <qicon.h>
7+
#include <qmessagebox.h>
68
#include <qpixmap.h>
79
#include <qstringview.h>
10+
#include <qwidget.h>
811

912
#include <initializer_list>
1013
#include <type_traits>
1114
#include <vector>
1215

1316
#include "error_types.hxx"
1417

18+
using std::abort;
1519
using std::initializer_list;
1620
using std::is_same_v;
1721
using std::stringstream;
@@ -37,7 +41,7 @@ inline constexpr bool is_valid_hex_color(const Args&... args) noexcept {
3741
...);
3842
}
3943

40-
inline QIcon image_from_bytes(const initializer_list<uint8_t>& data) {
44+
inline QIcon image_from_bytes(const initializer_list<uint8_t>& data) noexcept {
4145
QByteArray bytes_array;
4246
bytes_array.reserve(static_cast<int>(data.size()));
4347

@@ -53,6 +57,14 @@ inline QIcon image_from_bytes(const initializer_list<uint8_t>& data) {
5357
return QIcon();
5458
}
5559

60+
inline void error_dialog_show(QWidget* parent,
61+
ErrorTypes error_types) noexcept {
62+
auto error_string = error_to_string(error_types).data();
63+
64+
QMessageBox::critical(parent, "Error", error_string);
65+
abort();
66+
}
67+
5668
} // namespace Lazyboard::front_end_utils
5769

5870
#endif

0 commit comments

Comments
 (0)