-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathNitroSQLiteException.hpp
More file actions
70 lines (57 loc) · 2.75 KB
/
NitroSQLiteException.hpp
File metadata and controls
70 lines (57 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#pragma once
#include <exception>
#include <iostream>
#include <string>
#include <optional>
const std::string NITRO_SQLITE_EXCEPTION_PREFIX = "[NativeNitroSQLiteException]";
enum NitroSQLiteExceptionType {
UnknownError,
DatabaseCannotBeOpened,
EncryptionNotEnabled,
DatabaseCannotBeDecrypted,
DatabaseNotOpen,
UnableToAttachToDatabase,
SqlExecutionError,
CouldNotLoadFile,
NoBatchCommandsProvided
};
inline std::unordered_map<NitroSQLiteExceptionType, std::string> exceptionTypeStrings = {
{UnknownError, "UnknownError"},
{DatabaseCannotBeOpened, "DatabaseCannotBeOpened"},
{EncryptionNotEnabled, "EncryptionNotEnabled"},
{DatabaseCannotBeDecrypted, "DatabaseCannotBeDecrypted"},
{DatabaseNotOpen, "DatabaseNotOpen"},
{UnableToAttachToDatabase, "UnableToAttachToDatabase"},
{SqlExecutionError, "SqlExecutionError"},
{CouldNotLoadFile, "CouldNotLoadFile"},
{NoBatchCommandsProvided, "NoBatchCommandsProvided"}};
inline std::string typeToString(NitroSQLiteExceptionType type) {
return exceptionTypeStrings[type];
}
class NitroSQLiteException : public std::exception {
public:
explicit NitroSQLiteException(const char* message) : NitroSQLiteException(NitroSQLiteExceptionType::UnknownError, message) {}
explicit NitroSQLiteException(const std::string& message) : NitroSQLiteException(NitroSQLiteExceptionType::UnknownError, message) {}
NitroSQLiteException(const NitroSQLiteExceptionType& type, const char* message) : NitroSQLiteException(type, std::string(message)) {}
NitroSQLiteException(const NitroSQLiteExceptionType& type, const std::string& message)
: _exceptionString(NITRO_SQLITE_EXCEPTION_PREFIX + "[" + typeToString(type) + "] " + message) {}
private:
const std::string _exceptionString;
public:
[[nodiscard]] const char* what() const noexcept override {
return this->_exceptionString.c_str();
}
static NitroSQLiteException DatabaseNotOpen(const std::string& dbName) {
return NitroSQLiteException(NitroSQLiteExceptionType::UnableToAttachToDatabase, dbName + " is not open");
}
static NitroSQLiteException SqlExecution(const std::string& errorMessage) {
return NitroSQLiteException(NitroSQLiteExceptionType::SqlExecutionError, errorMessage);
}
static NitroSQLiteException DatabaseFileNotFound(const std::string& dbFilePath) {
return NitroSQLiteException(NitroSQLiteExceptionType::SqlExecutionError, "Database file not found: " + dbFilePath);
}
static NitroSQLiteException CouldNotLoadFile(const std::string& fileLocation, std::optional<std::string> additionalLine = std::nullopt) {
const auto message = "Could not load file: " + fileLocation + (additionalLine ? (". " + *additionalLine) : "");
return NitroSQLiteException(NitroSQLiteExceptionType::CouldNotLoadFile, message);
}
};