Skip to content

Commit 39b0e52

Browse files
committed
chore: add Exception
1 parent 31c366e commit 39b0e52

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

include/mrdox/Support/Error.hpp

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ namespace mrdox {
3131
// Error
3232
//
3333

34+
class Exception;
35+
3436
/** Holds the description of an error, or success.
3537
*/
3638
class [[nodiscard]] MRDOX_DECL
@@ -161,15 +163,28 @@ class [[nodiscard]] MRDOX_DECL
161163
return message_ == rhs.message_;
162164
}
163165

164-
#if 0
165166
/** Return a null-terminated error string.
166167
*/
167168
char const*
168-
what() const noexcept override
169+
what() const noexcept
169170
{
170171
return reason_.c_str();
171172
}
172-
#endif
173+
174+
/** Throw Exception(*this)
175+
176+
@pre this->failed()
177+
*/
178+
void Throw() const;
179+
180+
/** Throw Exception(*this), or do nothing if no failure.
181+
*/
182+
void maybeThrow() const
183+
{
184+
if(! failed())
185+
return;
186+
Throw();
187+
}
173188

174189
constexpr void swap(Error& rhs) noexcept
175190
{
@@ -190,10 +205,51 @@ class [[nodiscard]] MRDOX_DECL
190205
static Error success() noexcept;
191206
};
192207

208+
//------------------------------------------------
209+
//
210+
// Exception
211+
//
212+
//------------------------------------------------
213+
214+
/** Type of all exceptions thrown by the API.
215+
*/
216+
class MRDOX_DECL
217+
Exception final : public std::exception
218+
{
219+
Error err_;
220+
221+
public:
222+
/** Constructor.
223+
*/
224+
explicit
225+
Exception(
226+
Error err) noexcept
227+
: err_(std::move(err))
228+
{
229+
}
230+
231+
/** Return the Error stored in the exception.
232+
*/
233+
Error const&
234+
error() const noexcept
235+
{
236+
return err_;
237+
}
238+
239+
/** Return a null-terminated error string.
240+
*/
241+
char const*
242+
what() const noexcept override
243+
{
244+
return err_.what();
245+
}
246+
};
247+
193248
//------------------------------------------------
194249
//
195250
// Expected
196251
//
252+
//------------------------------------------------
197253

198254
/** A container holding an error or a value.
199255
*/

0 commit comments

Comments
 (0)