Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
io_error.h
1#ifndef SOURCEMETA_CORE_IO_ERROR_H_
2#define SOURCEMETA_CORE_IO_ERROR_H_
3
4#ifndef SOURCEMETA_CORE_IO_EXPORT
5#include <sourcemeta/core/io_export.h>
6#endif
7
8#include <exception> // std::exception
9#include <filesystem> // std::filesystem::path
10#include <string> // std::string
11#include <string_view> // std::string_view
12#include <utility> // std::move
13
14namespace sourcemeta::core {
15
16// Exporting symbols that depends on the standard C++ library is considered
17// safe.
18// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
19#if defined(_MSC_VER)
20#pragma warning(disable : 4251 4275)
21#endif
22
25class SOURCEMETA_CORE_IO_EXPORT FileViewError : public std::exception {
26public:
28 FileViewError(std::filesystem::path path, const char *message)
29 : path_{std::move(path)}, message_{message} {}
30 FileViewError(std::filesystem::path path, std::string message) = delete;
31 FileViewError(std::filesystem::path path, std::string &&message) = delete;
32 FileViewError(std::filesystem::path path, std::string_view message) = delete;
33
34 [[nodiscard]] auto what() const noexcept -> const char * override {
35 return this->message_;
36 }
37
39 [[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
40 return this->path_;
41 }
42
43private:
44 std::filesystem::path path_;
45 const char *message_;
46};
47
50class SOURCEMETA_CORE_IO_EXPORT IOFileNotFoundError : public std::exception {
51public:
53 IOFileNotFoundError(std::filesystem::path path) : path_{std::move(path)} {}
54
55 [[nodiscard]] auto what() const noexcept -> const char * override {
56 return "File not found";
57 }
58
60 [[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
61 return this->path_;
62 }
63
64private:
65 std::filesystem::path path_;
66};
67
70class SOURCEMETA_CORE_IO_EXPORT IOFilePermissionError : public std::exception {
71public:
73 IOFilePermissionError(std::filesystem::path path) : path_{std::move(path)} {}
74
75 [[nodiscard]] auto what() const noexcept -> const char * override {
76 return "Permission denied";
77 }
78
80 [[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
81 return this->path_;
82 }
83
84private:
85 std::filesystem::path path_;
86};
87
90class SOURCEMETA_CORE_IO_EXPORT IOIsADirectoryError : public std::exception {
91public:
93 IOIsADirectoryError(std::filesystem::path path) : path_{std::move(path)} {}
94
95 [[nodiscard]] auto what() const noexcept -> const char * override {
96 return "Expected a file but got a directory";
97 }
98
100 [[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
101 return this->path_;
102 }
103
104private:
105 std::filesystem::path path_;
106};
107
110class SOURCEMETA_CORE_IO_EXPORT IONotADirectoryError : public std::exception {
111public:
113 IONotADirectoryError(std::filesystem::path path) : path_{std::move(path)} {}
114
115 [[nodiscard]] auto what() const noexcept -> const char * override {
116 return "Expected a directory but got a file";
117 }
118
120 [[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
121 return this->path_;
122 }
123
124private:
125 std::filesystem::path path_;
126};
127
130class SOURCEMETA_CORE_IO_EXPORT IOFileAlreadyExistsError
131 : public std::exception {
132public:
134 IOFileAlreadyExistsError(std::filesystem::path path)
135 : path_{std::move(path)} {}
136
137 [[nodiscard]] auto what() const noexcept -> const char * override {
138 return "File already exists";
139 }
140
142 [[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
143 return this->path_;
144 }
145
146private:
147 std::filesystem::path path_;
148};
149
153class SOURCEMETA_CORE_IO_EXPORT IOReadOutOfBoundsError : public std::exception {
154public:
155 [[nodiscard]] auto what() const noexcept -> const char * override {
156 return "Read past the end of the underlying data";
157 }
158};
159
162class SOURCEMETA_CORE_IO_EXPORT IOStreamWriteError : public std::exception {
163public:
164 [[nodiscard]] auto what() const noexcept -> const char * override {
165 return "Failed to write to stream";
166 }
167};
168
169#if defined(_MSC_VER)
170#pragma warning(default : 4251 4275)
171#endif
172
173} // namespace sourcemeta::core
174
175#endif
IOFileNotFoundError(std::filesystem::path path)
Construct the error given the offending path.
Definition io_error.h:53
IOFilePermissionError(std::filesystem::path path)
Construct the error given the offending path.
Definition io_error.h:73
auto path() const noexcept -> const std::filesystem::path &
The offending path.
Definition io_error.h:39
IOIsADirectoryError(std::filesystem::path path)
Construct the error given the offending path.
Definition io_error.h:93
FileViewError(std::filesystem::path path, const char *message)
Construct the error given the offending path and a message.
Definition io_error.h:28
auto path() const noexcept -> const std::filesystem::path &
The offending path.
Definition io_error.h:60
IOFileAlreadyExistsError(std::filesystem::path path)
Construct the error given the offending path.
Definition io_error.h:134
auto path() const noexcept -> const std::filesystem::path &
The offending path.
Definition io_error.h:100
auto path() const noexcept -> const std::filesystem::path &
The offending path.
Definition io_error.h:142
auto path() const noexcept -> const std::filesystem::path &
The offending path.
Definition io_error.h:120
auto path() const noexcept -> const std::filesystem::path &
The offending path.
Definition io_error.h:80
IONotADirectoryError(std::filesystem::path path)
Construct the error given the offending path.
Definition io_error.h:113
Definition io_error.h:25
Definition io_error.h:162