Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
uritemplate_error.h
1#ifndef SOURCEMETA_CORE_URITEMPLATE_ERROR_H_
2#define SOURCEMETA_CORE_URITEMPLATE_ERROR_H_
3
4#ifndef SOURCEMETA_CORE_URITEMPLATE_EXPORT
5#include <sourcemeta/core/uritemplate_export.h>
6#endif
7
8#include <cstdint> // std::uint64_t
9#include <exception> // std::exception
10#include <filesystem> // std::filesystem::path
11#include <stdexcept> // std::runtime_error
12#include <string> // std::string
13#include <string_view> // std::string_view
14#include <utility> // std::move
15
16namespace sourcemeta::core {
17
18// Exporting symbols that depends on the standard C++ library is considered
19// safe.
20// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
21#if defined(_MSC_VER)
22#pragma warning(disable : 4251 4275)
23#endif
24
27class SOURCEMETA_CORE_URITEMPLATE_EXPORT URITemplateParseError
28 : public std::exception {
29public:
31 URITemplateParseError(const std::uint64_t column) : column_{column} {}
32
33 [[nodiscard]] auto what() const noexcept -> const char * override {
34 return "The input is not a valid URI Template";
35 }
36
38 [[nodiscard]] auto column() const noexcept -> std::uint64_t {
39 return this->column_;
40 }
41
42private:
43 std::uint64_t column_;
44};
45
48class SOURCEMETA_CORE_URITEMPLATE_EXPORT URITemplateExpansionError
49 : public std::runtime_error {
50public:
52 URITemplateExpansionError(const std::string &message)
53 : std::runtime_error{message} {}
54};
55
58class SOURCEMETA_CORE_URITEMPLATE_EXPORT URITemplateRouterVariableMismatchError
59 : public std::exception {
60public:
63 const std::string_view right)
64 : left_{left}, right_{right} {}
65
66 [[nodiscard]] auto what() const noexcept -> const char * override {
67 return "Variable name mismatch when adding route";
68 }
69
71 [[nodiscard]] auto left() const noexcept -> const std::string & {
72 return this->left_;
73 }
74
76 [[nodiscard]] auto right() const noexcept -> const std::string & {
77 return this->right_;
78 }
79
80private:
81 std::string left_;
82 std::string right_;
83};
84
87class SOURCEMETA_CORE_URITEMPLATE_EXPORT URITemplateRouterInvalidSegmentError
88 : public std::exception {
89public:
92 const std::string_view segment)
93 : message_{message}, segment_{segment} {}
94
95 [[nodiscard]] auto what() const noexcept -> const char * override {
96 return this->message_;
97 }
98
100 [[nodiscard]] auto segment() const noexcept -> const std::string & {
101 return this->segment_;
102 }
103
104private:
105 const char *message_;
106 std::string segment_;
107};
108
112class SOURCEMETA_CORE_URITEMPLATE_EXPORT
113 URITemplateRouterInvalidOperationIdError : public std::exception {
114public:
117 : operation_id_{operation_id} {}
118
119 [[nodiscard]] auto what() const noexcept -> const char * override {
120 return "Invalid operation identifier";
121 }
122
124 [[nodiscard]] auto operation_id() const noexcept -> const std::string & {
125 return this->operation_id_;
126 }
127
128private:
129 std::string operation_id_;
130};
131
135class SOURCEMETA_CORE_URITEMPLATE_EXPORT
136 URITemplateRouterDuplicateOperationIdError : public std::exception {
137public:
140 const std::string_view operation_id)
141 : operation_id_{operation_id} {}
142
143 [[nodiscard]] auto what() const noexcept -> const char * override {
144 return "Duplicate operation identifier";
145 }
146
148 [[nodiscard]] auto operation_id() const noexcept -> const std::string & {
149 return this->operation_id_;
150 }
151
152private:
153 std::string operation_id_;
154};
155
158class SOURCEMETA_CORE_URITEMPLATE_EXPORT URITemplateRouterSaveError
159 : public std::exception {
160public:
162 URITemplateRouterSaveError(std::filesystem::path path, const char *message)
163 : path_{std::move(path)}, message_{message} {}
164
165 [[nodiscard]] auto what() const noexcept -> const char * override {
166 return this->message_;
167 }
168
170 [[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
171 return this->path_;
172 }
173
174private:
175 std::filesystem::path path_;
176 const char *message_;
177};
178
181class SOURCEMETA_CORE_URITEMPLATE_EXPORT URITemplateRouterReadError
182 : public std::exception {
183public:
185 URITemplateRouterReadError(std::filesystem::path path)
186 : path_{std::move(path)} {}
187
188 [[nodiscard]] auto what() const noexcept -> const char * override {
189 return "Failed to open router file for reading";
190 }
191
193 [[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
194 return this->path_;
195 }
196
197private:
198 std::filesystem::path path_;
199};
200
201#if defined(_MSC_VER)
202#pragma warning(default : 4251 4275)
203#endif
204
205} // namespace sourcemeta::core
206
207#endif
auto segment() const noexcept -> const std::string &
Get the offending segment.
Definition uritemplate_error.h:100
auto operation_id() const noexcept -> const std::string &
Get the conflicting operation identifier.
Definition uritemplate_error.h:148
URITemplateParseError(const std::uint64_t column)
Construct an error given the column number where parsing failed.
Definition uritemplate_error.h:31
auto right() const noexcept -> const std::string &
Get the conflicting variable name.
Definition uritemplate_error.h:76
URITemplateExpansionError(const std::string &message)
Construct an error with a descriptive message.
Definition uritemplate_error.h:52
auto operation_id() const noexcept -> const std::string &
Get the offending operation identifier.
Definition uritemplate_error.h:124
URITemplateRouterDuplicateOperationIdError(const std::string_view operation_id)
Construct an error given the conflicting operation identifier.
Definition uritemplate_error.h:139
URITemplateRouterVariableMismatchError(const std::string_view left, const std::string_view right)
Construct an error given the existing and conflicting variable names.
Definition uritemplate_error.h:62
URITemplateRouterReadError(std::filesystem::path path)
Construct an error given the target path.
Definition uritemplate_error.h:185
URITemplateRouterInvalidOperationIdError(const std::string_view operation_id)
Construct an error given the offending operation identifier.
Definition uritemplate_error.h:116
URITemplateRouterInvalidSegmentError(const char *message, const std::string_view segment)
Construct an error given a descriptive message and the offending segment.
Definition uritemplate_error.h:91
auto left() const noexcept -> const std::string &
Get the existing variable name.
Definition uritemplate_error.h:71
URITemplateRouterSaveError(std::filesystem::path path, const char *message)
Construct an error given the target path and a descriptive message.
Definition uritemplate_error.h:162
auto column() const noexcept -> std::uint64_t
Get the column number of the error.
Definition uritemplate_error.h:38
auto path() const noexcept -> const std::filesystem::path &
Get the path that could not be read.
Definition uritemplate_error.h:193
auto path() const noexcept -> const std::filesystem::path &
Get the path that could not be saved.
Definition uritemplate_error.h:170