Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
80 lines
1.8 KiB
C++
80 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility>
|
|
|
|
namespace Void::Result
|
|
{
|
|
/// Default error type for Void::Result::Result<T, E>.
|
|
///
|
|
/// An Error carries a free-form `message` and an optional chained
|
|
/// `cause`. New context layers can be added with `with_context()`,
|
|
/// which returns a new Error whose `cause` is the previous error —
|
|
/// useful for "loading config: parsing line 42: invalid number".
|
|
///
|
|
/// Errors are cheap to copy (the cause chain is shared via
|
|
/// shared_ptr).
|
|
class Error
|
|
{
|
|
public:
|
|
|
|
Error() = default;
|
|
|
|
explicit Error(std::string message) noexcept
|
|
: m_message(std::move(message))
|
|
{
|
|
}
|
|
|
|
Error(std::string message, Error cause)
|
|
: m_message(std::move(message)),
|
|
m_cause(std::make_shared<Error>(std::move(cause)))
|
|
{
|
|
}
|
|
|
|
[[nodiscard]] const std::string& message() const noexcept
|
|
{
|
|
return m_message;
|
|
}
|
|
|
|
[[nodiscard]] const std::shared_ptr<const Error>&
|
|
cause() const noexcept
|
|
{
|
|
return m_cause;
|
|
}
|
|
|
|
/// Returns a new Error whose message is `ctx` and whose cause
|
|
/// is *this.
|
|
[[nodiscard]] Error with_context(std::string ctx) const&
|
|
{
|
|
return Error{std::move(ctx), *this};
|
|
}
|
|
|
|
[[nodiscard]] Error with_context(std::string ctx) &&
|
|
{
|
|
return Error{std::move(ctx), std::move(*this)};
|
|
}
|
|
|
|
/// Render the full cause chain as
|
|
/// "outer\n caused by: middle\n caused by: inner"
|
|
[[nodiscard]] std::string format() const
|
|
{
|
|
std::string out = m_message;
|
|
const Error* cur = m_cause.get();
|
|
while (cur != nullptr) {
|
|
out += "\n caused by: ";
|
|
out += cur->m_message;
|
|
cur = cur->m_cause.get();
|
|
}
|
|
return out;
|
|
}
|
|
|
|
private:
|
|
|
|
std::string m_message;
|
|
std::shared_ptr<const Error> m_cause;
|
|
};
|
|
|
|
} // namespace Void::Result
|