GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Error.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_ERROR_HPP
5#define GREM_CORE_ERROR_HPP
6
8
13
14#include <exception> // std::exception, std::throw_with_nested
15#include <stdexcept> // std::runtime_error
16#include <utility> // std::move, std::forward
17
18namespace grem {
19
20namespace detail {
21
22struct FilepathError : std::exception {
23 String filepath;
24
25 explicit FilepathError(String filepath)
26 : filepath(std::move(filepath)) {}
27
28 [[nodiscard]] const char* what() const noexcept override {
29 return filepath.c_str();
30 }
31};
32
33} // namespace detail
34
38class Error : public std::runtime_error {
39public:
40 template <typename OuterException>
41 [[noreturn]] static void throwWithNested(OuterException&& error) {
42 std::throw_with_nested(std::forward<OuterException>(error));
43 }
44
45 [[noreturn]] static void throwWithNestedFilepath(String filepath) {
46 std::throw_with_nested(detail::FilepathError{std::move(filepath)});
47 }
48
49 [[noreturn]] static void throwWithNestedFilepath(CStringView filepath) {
50 std::throw_with_nested(detail::FilepathError{String{filepath}});
51 }
52
53 [[nodiscard]] GREM_API(core) static String formatCurrentExceptionMessage();
54
55 explicit Error(CStringView message)
56 : runtime_error(message.c_str()) {}
57
58 template <convertible_to<StringView> StringViewConvertible>
59 explicit Error(const StringViewConvertible& message) requires(!convertible_to<StringViewConvertible, CStringView>)
60 : runtime_error(String{StringView{message}}) {}
61
62 virtual void writeMessage(String& output) const {
63 output.append(what());
64 }
65
66 [[nodiscard]] virtual bool messageAttachesToPrecedingFilepath() const noexcept {
67 return false;
68 }
69};
70
71} // namespace grem
72
73#endif
static void throwWithNested(OuterException &&error)
Definition Error.hpp:41
virtual void writeMessage(String &output) const
Definition Error.hpp:62
virtual bool messageAttachesToPrecedingFilepath() const noexcept
Definition Error.hpp:66
static void throwWithNestedFilepath(String filepath)
Definition Error.hpp:45
static void throwWithNestedFilepath(CStringView filepath)
Definition Error.hpp:49
Error(CStringView message)
Definition Error.hpp:55
static String formatCurrentExceptionMessage()
Error(const StringViewConvertible &message)
Definition Error.hpp:59
Definition concepts.hpp:47
Definition aliases.hpp:9
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
StringBase< char, std::char_traits< char >, std::allocator< char > > String
Definition String.hpp:17
StringViewBase< char, std::char_traits< char > > StringView
Definition StringView.hpp:16
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635