lmdbal/src/exceptions.h

183 lines
5.1 KiB
C
Raw Normal View History

/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-09-05 20:25:39 +00:00
2023-03-21 11:05:54 +00:00
#ifndef LMDBAL_EXCEPTIONS_H
#define LMDBAL_EXCEPTIONS_H
2022-09-05 20:25:39 +00:00
#include <stdexcept>
#include <string>
#include <optional>
2023-03-21 11:05:54 +00:00
namespace LMDBAL {
2022-09-05 20:25:39 +00:00
2023-04-14 14:44:46 +00:00
/**
* \brief Parent abstract class for all LMDBAL exceptions
*/
2023-03-20 15:37:13 +00:00
class Exception : public std::exception {
public:
Exception();
virtual ~Exception();
2023-04-14 14:44:46 +00:00
virtual std::string getMessage() const = 0; /**<\brief returns exception message*/
2023-04-14 14:44:46 +00:00
const char* what() const noexcept( true ) override;
};
2023-04-14 14:44:46 +00:00
/**
* \brief Thrown if LMDBAL had issues creating or opening database directory
*/
2023-03-20 15:37:13 +00:00
class Directory: public Exception {
2022-09-05 20:25:39 +00:00
public:
2023-04-14 14:44:46 +00:00
/**
* \brief Creates exception
*
* \param path - path of the directory that was supposed to be used to store the database
*/
2022-09-09 17:15:40 +00:00
Directory(const std::string& path);
2022-09-05 20:25:39 +00:00
2022-09-09 17:15:40 +00:00
std::string getMessage() const;
2022-09-05 20:25:39 +00:00
private:
std::string path;
};
2023-04-14 14:44:46 +00:00
/**
* \brief Thrown if something in the database was called on closed state and it is not supported
*/
2023-03-20 15:37:13 +00:00
class Closed : public Exception {
2022-09-05 20:25:39 +00:00
public:
2023-04-14 14:44:46 +00:00
/**
* \brief Creates exception
*
* \param operation - text name of the method that was called on closed database
* \param dbName - name of the database
* \param tableName - name of the storage which called that method, abscent if it's untracable or if it's thrown by the database
*/
Closed(const std::string& operation, const std::string& dbName, const std::optional<std::string>& tableName = std::nullopt);
2022-09-05 20:25:39 +00:00
2022-09-09 17:15:40 +00:00
std::string getMessage() const;
2022-09-05 20:25:39 +00:00
private:
std::string operation;
2022-09-09 17:15:40 +00:00
std::string dbName;
std::optional<std::string> tableName;
2022-09-05 20:25:39 +00:00
};
2023-04-14 14:44:46 +00:00
/**
* \brief Thrown if something in the database was called on opened state and it is not supported
*/
2023-03-20 15:37:13 +00:00
class Opened : Exception {
2022-09-05 20:25:39 +00:00
public:
2023-04-14 14:44:46 +00:00
/**
* \brief Creates exception
*
* \param action - text name of the method that was called on opened database
* \param dbName - name of the database
*/
Opened(const std::string& dbName, const std::string& action);
2022-09-05 20:25:39 +00:00
2022-09-09 17:15:40 +00:00
std::string getMessage() const;
2022-09-05 20:25:39 +00:00
private:
2022-09-09 17:15:40 +00:00
std::string dbName;
std::string action;
2022-09-05 20:25:39 +00:00
};
2023-04-14 14:44:46 +00:00
/**
* \brief Thrown if something in the database was not found
*/
2023-03-20 15:37:13 +00:00
class NotFound : public Exception {
2022-09-05 20:25:39 +00:00
public:
2023-04-14 14:44:46 +00:00
/**
* \brief Creates exception
*
* \param key - record key that was not found
* \param dbName - name of the database
* \param tableName - name of the storage that was looked for a record
*/
2022-09-09 17:15:40 +00:00
NotFound(const std::string& key, const std::string& dbName, const std::string& tableName);
2022-09-05 20:25:39 +00:00
2022-09-09 17:15:40 +00:00
std::string getMessage() const;
2022-09-05 20:25:39 +00:00
private:
std::string key;
2022-09-09 17:15:40 +00:00
std::string dbName;
std::string tableName;
2022-09-05 20:25:39 +00:00
};
2023-04-14 14:44:46 +00:00
/**
* \brief Thrown if there was attempt to define storages with conflicting names
*/
class StorageDuplicate : public Exception {
public:
2023-04-14 14:44:46 +00:00
/**
* \brief Creates exception
*
* \param dbName - name of the database
* \param tableName - that name that was conflicting
*/
StorageDuplicate(const std::string& dbName, const std::string& tableName);
std::string getMessage() const;
private:
std::string dbName;
std::string tableName;
};
2023-04-14 14:44:46 +00:00
/**
* \brief Thrown if there was a key conflict in one of the storages
*/
2023-03-20 15:37:13 +00:00
class Exist : public Exception {
2022-09-05 20:25:39 +00:00
public:
2023-04-14 14:44:46 +00:00
/**
* \brief Creates exception
*
* \param key - record key that caused the conflict
* \param dbName - name of the database
* \param tableName - name of the storage that was operated with
*/
2022-09-09 17:15:40 +00:00
Exist(const std::string& key, const std::string& dbName, const std::string& tableName);
2022-09-05 20:25:39 +00:00
2022-09-09 17:15:40 +00:00
std::string getMessage() const;
2022-09-05 20:25:39 +00:00
private:
std::string key;
2022-09-09 17:15:40 +00:00
std::string dbName;
std::string tableName;
2022-09-05 20:25:39 +00:00
};
2023-04-14 14:44:46 +00:00
/**
* \brief Thrown if something unexpected happened
*/
2023-03-20 15:37:13 +00:00
class Unknown : public Exception {
2022-09-05 20:25:39 +00:00
public:
2023-04-14 14:44:46 +00:00
/**
* \brief Creates exception
*
* \param message - text description of the error, most of the times contains the result of <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#ga569e66c1e3edc1a6016b86719ee3d098">mdb_strerror</a>
* \param dbName - name of the database
* \param tableName - name of the storage that was operated with, abscent if the operation was with the database itself
*/
2022-09-09 17:15:40 +00:00
Unknown(const std::string& dbName, const std::string& message, const std::optional<std::string>& tableName = std::nullopt);
2022-09-05 20:25:39 +00:00
2022-09-09 17:15:40 +00:00
std::string getMessage() const;
2022-09-05 20:25:39 +00:00
private:
2022-09-09 17:15:40 +00:00
std::string dbName;
std::optional<std::string> tableName;
2022-09-05 20:25:39 +00:00
std::string msg;
};
2022-09-14 22:18:31 +00:00
2023-03-20 15:37:13 +00:00
}
2023-03-21 11:05:54 +00:00
#endif //LMDBAL_EXCEPTIONS_H