2024-01-06 00:42:54 +08:00
|
|
|
// Copyright 2024 Memgraph Ltd.
|
2022-09-15 17:26:26 +08:00
|
|
|
//
|
|
|
|
// Use of this software is governed by the Business Source License
|
|
|
|
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
|
|
|
|
// License, and you may not use this file except in compliance with the Business Source License.
|
|
|
|
//
|
|
|
|
// As of the Change Date specified in that file, in accordance with
|
|
|
|
// the Business Source License, use of this software will be governed
|
|
|
|
// by the Apache License, Version 2.0, included in the file
|
|
|
|
// licenses/APL.txt.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <exception>
|
|
|
|
#include <iostream>
|
2023-08-01 06:05:44 +08:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2022-09-15 17:26:26 +08:00
|
|
|
|
|
|
|
namespace mg_exception {
|
2023-08-01 06:05:44 +08:00
|
|
|
|
|
|
|
// Instead of writing this utility function, we could have used `fmt::format`, but that's not an ideal option here
|
|
|
|
// because that would introduce dependency that would be propagated to the client code (if exceptions here would be
|
|
|
|
// used). Since the functionality here is not complex + the code is not on a critical path, we opted for a pure C++
|
|
|
|
// solution.
|
|
|
|
template <typename FirstArg, typename... Args>
|
|
|
|
std::string StringSerialize(FirstArg &&firstArg, Args &&...args) {
|
|
|
|
std::stringstream stream;
|
2024-01-06 00:42:54 +08:00
|
|
|
stream << std::forward<FirstArg>(firstArg);
|
2023-08-01 06:05:44 +08:00
|
|
|
((stream << " " << args), ...);
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
|
2022-09-15 17:26:26 +08:00
|
|
|
struct UnknownException : public std::exception {
|
2023-08-01 06:05:44 +08:00
|
|
|
const char *what() const noexcept override { return "Unknown exception!"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NotEnoughMemoryException : public std::exception {
|
|
|
|
NotEnoughMemoryException()
|
|
|
|
: message_{
|
|
|
|
StringSerialize("Not enough memory! For more details please visit", "https://memgr.ph/memory-control")} {}
|
|
|
|
const char *what() const noexcept override { return message_.c_str(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string message_;
|
2022-09-15 17:26:26 +08:00
|
|
|
};
|
2023-08-01 06:05:44 +08:00
|
|
|
|
2022-09-15 17:26:26 +08:00
|
|
|
struct AllocationException : public std::exception {
|
2023-08-01 06:05:44 +08:00
|
|
|
AllocationException()
|
|
|
|
: message_{StringSerialize("Could not allocate memory. For more details please visit",
|
|
|
|
"https://memgr.ph/memory-control")} {}
|
|
|
|
const char *what() const noexcept override { return message_.c_str(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string message_;
|
2022-09-15 17:26:26 +08:00
|
|
|
};
|
2023-08-01 06:05:44 +08:00
|
|
|
|
2022-09-15 17:26:26 +08:00
|
|
|
struct InsufficientBufferException : public std::exception {
|
2023-08-01 06:05:44 +08:00
|
|
|
const char *what() const noexcept override { return "Buffer is not sufficient to process procedure!"; }
|
2022-09-15 17:26:26 +08:00
|
|
|
};
|
2023-08-01 06:05:44 +08:00
|
|
|
|
2022-09-15 17:26:26 +08:00
|
|
|
struct OutOfRangeException : public std::exception {
|
2023-08-01 06:05:44 +08:00
|
|
|
const char *what() const noexcept override { return "Index out of range!"; }
|
2022-09-15 17:26:26 +08:00
|
|
|
};
|
2023-08-01 06:05:44 +08:00
|
|
|
|
2022-09-15 17:26:26 +08:00
|
|
|
struct LogicException : public std::exception {
|
2023-08-01 06:05:44 +08:00
|
|
|
const char *what() const noexcept override { return "Logic exception, check the procedure signature!"; }
|
2022-09-15 17:26:26 +08:00
|
|
|
};
|
2023-08-01 06:05:44 +08:00
|
|
|
|
2022-09-15 17:26:26 +08:00
|
|
|
struct DeletedObjectException : public std::exception {
|
2023-08-01 06:05:44 +08:00
|
|
|
const char *what() const noexcept override { return "Object is deleted!"; }
|
2022-09-15 17:26:26 +08:00
|
|
|
};
|
2023-08-01 06:05:44 +08:00
|
|
|
|
2022-09-15 17:26:26 +08:00
|
|
|
struct InvalidArgumentException : public std::exception {
|
2023-08-01 06:05:44 +08:00
|
|
|
const char *what() const noexcept override { return "Invalid argument!"; }
|
2022-09-15 17:26:26 +08:00
|
|
|
};
|
2023-08-01 06:05:44 +08:00
|
|
|
|
2022-09-15 17:26:26 +08:00
|
|
|
struct InvalidIDException : public std::exception {
|
2023-08-01 06:05:44 +08:00
|
|
|
InvalidIDException() : message_{"Invalid ID!"} {}
|
|
|
|
explicit InvalidIDException(std::uint64_t identifier) : message_{StringSerialize("Invalid ID =", identifier)} {}
|
|
|
|
const char *what() const noexcept override { return message_.c_str(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string message_;
|
2022-09-15 17:26:26 +08:00
|
|
|
};
|
2023-08-01 06:05:44 +08:00
|
|
|
|
2022-09-15 17:26:26 +08:00
|
|
|
struct KeyAlreadyExistsException : public std::exception {
|
2023-08-01 06:05:44 +08:00
|
|
|
KeyAlreadyExistsException() : message_{"Key you are trying to set already exists!"} {}
|
|
|
|
explicit KeyAlreadyExistsException(const std::string &key)
|
|
|
|
: message_{StringSerialize("Key you are trying to set already exists! KEY = ", key)} {}
|
|
|
|
const char *what() const noexcept override { return message_.c_str(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string message_;
|
2022-09-15 17:26:26 +08:00
|
|
|
};
|
2023-08-01 06:05:44 +08:00
|
|
|
|
2022-09-15 17:26:26 +08:00
|
|
|
struct ImmutableObjectException : public std::exception {
|
2023-08-01 06:05:44 +08:00
|
|
|
const char *what() const noexcept override { return "Object you are trying to change is immutable!"; }
|
2022-09-15 17:26:26 +08:00
|
|
|
};
|
2023-08-01 06:05:44 +08:00
|
|
|
|
2022-09-15 17:26:26 +08:00
|
|
|
struct ValueConversionException : public std::exception {
|
2023-08-01 06:05:44 +08:00
|
|
|
const char *what() const noexcept override { return "Error in value conversion!"; }
|
2022-09-15 17:26:26 +08:00
|
|
|
};
|
2023-08-01 06:05:44 +08:00
|
|
|
|
2022-09-15 17:26:26 +08:00
|
|
|
struct SerializationException : public std::exception {
|
2023-08-01 06:05:44 +08:00
|
|
|
const char *what() const noexcept override { return "Error in serialization!"; }
|
2022-09-15 17:26:26 +08:00
|
|
|
};
|
2023-08-01 06:05:44 +08:00
|
|
|
|
2022-09-15 17:26:26 +08:00
|
|
|
} // namespace mg_exception
|