Skip to content

Commit

Permalink
Add Error struct and error defining macro
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoryKogan committed Dec 30, 2023
1 parent 5bdcdc6 commit d9a8494
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/FileSystem/Error/Error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <string>
#include <utility>

struct Error {
virtual ~Error() = default;
Error() = default;
Error(const Error &) = default;
Error(Error &&) = default;
auto operator=(const Error &) -> Error & = delete;
auto operator=(Error &&) -> Error & = delete;

explicit Error(std::string in_message) : message(std::move(in_message)) {}
[[nodiscard]] auto get_error_type() const noexcept -> std::string { // NOLINT (bugprone-exception-escape)
return "Error";
}

[[nodiscard]] auto what() const noexcept -> std::string { return get_error_type() + ": " + message; }

private:
std::string message;
};

#define DEFINE_ERROR_TYPE(name) /* NOLINT (cppcoreguidelines-macro-usage) */ \
struct name##Err : public Error { \
explicit name##Err(std::string in_message) : Error(std::move(in_message)) {} \
[[nodiscard]] auto get_error_type() const noexcept /* NOLINT (bugprone-exception-escape) */ \
-> std::string { \
return #name; \
} \
};

0 comments on commit d9a8494

Please sign in to comment.