7#include "ll/api/base/Macro.h"
8#include "ll/api/io/LogLevel.h"
10#include "mc/server/commands/CommandOutputMessageType.h"
17template <
class T =
void>
18using Expected = ::nonstd::expected<T, ::ll::Error>;
20using Unexpected = ::nonstd::unexpected_type<::ll::Error>;
25 std::unique_ptr<Impl> impl;
31 virtual std::string message()
const noexcept = 0;
34 mutable std::unique_ptr<ErrorInfoBase> mInfo;
38 Error& operator=(
Error const&)
noexcept =
delete;
39 [[nodiscard]]
Error(
Error&&)
noexcept =
default;
40 [[nodiscard]]
Error(
Error const&)
noexcept =
delete;
42 LL_CONSTEXPR23
Error()
noexcept =
default;
43 LL_CONSTEXPR23
~Error()
noexcept =
default;
45 LL_CONSTEXPR23
Error(std::unique_ptr<ErrorInfoBase> i) noexcept : mInfo(std::move(i)) {}
47 LL_CONSTEXPR23
Error(::nonstd::unexpected_type<::ll::Error> i) noexcept :
Error(std::move(i.error())) {}
49 LL_CONSTEXPR23
operator bool()
const noexcept {
return mInfo !=
nullptr; }
51 LL_CONSTEXPR23
operator Unexpected()
noexcept {
52 return ::nonstd::make_unexpected<Error>(std::in_place, std::move(mInfo));
55 LLNDAPI std::string message()
const noexcept;
58 LL_CONSTEXPR23
bool isA()
noexcept {
59 return mInfo ?
typeid(T) ==
typeid(mInfo.get()) :
false;
62 LL_CONSTEXPR23 T& as()
noexcept {
63 return *
static_cast<T*
>(mInfo.get());
67 LLAPI
Error const& log(
io::Logger&, io::LogLevel = io::LogLevel::Error)
const noexcept;
69 LLAPI
Error const& log(
CommandOutput&, CommandOutputMessageType = CommandOutputMessageType::Error)
const noexcept;
74 StringError(std::string str) : str(std::move(str)) {}
75 std::string message()
const noexcept override {
return str; }
80 std::string message()
const noexcept override {
return ec.message(); }
82inline Unexpected forwardError(
::ll::Error& err)
noexcept { return ::nonstd::make_unexpected(std::move(err)); }
84inline Unexpected makeSuccessed() noexcept { return ::nonstd::make_unexpected(Error{}); }
86template <std::derived_from<::ll::ErrorInfoBase> T,
class... Args>
87inline Unexpected makeError(Args&&... args)
noexcept {
88 return ::nonstd::make_unexpected<Error>(std::in_place, std::make_unique<T>(std::forward<Args>(args)...));
90inline Unexpected makeStringError(std::string str)
noexcept {
return makeError<StringError>(std::move(str)); }
92inline Unexpected makeErrorCodeError(std::error_code ec)
noexcept {
return makeError<ErrorCodeError>(ec); }
94inline Unexpected makeErrorCodeError(std::errc ec)
noexcept {
return makeError<ErrorCodeError>(make_error_code(ec)); }
96LLNDAPI Unexpected makeExceptionError(std::exception_ptr
const& exc = std::current_exception()) noexcept;
100namespace nonstd::expected_lite {
102class bad_expected_access<::
ll::Error> :
public bad_expected_access<void> {
103 std::shared_ptr<::ll::Error> mError;
104 std::string mMessage;
107 explicit bad_expected_access(
::ll::Error& e) noexcept
108 : mError(::std::make_shared<::ll::Error>(::std::move(e))),
109 mMessage(mError->message()) {}
111 char const* what()
const noexcept override {
return mMessage.c_str(); }
115 ::ll::Error const& error()
const& {
return *mError; }
117 ::ll::Error&& error() && { return ::std::move(*mError); }
119 ::ll::Error const&& error()
const&& { return ::std::move(*mError); }
123 static void rethrow(
::ll::Error const& e) {
throw bad_expected_access<::ll::Error>{
const_cast<::
ll::Error&
>(e)}; }
Definition CommandOutput.h:19