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;
28 LLAPI ErrorInfoBase()
noexcept;
29 LLAPI
virtual ~ErrorInfoBase();
31 virtual std::string message()
const noexcept {
return message({}); }
32 virtual std::string message(std::string_view locale)
const noexcept = 0;
35 mutable std::unique_ptr<ErrorInfoBase> mInfo;
38 Error& operator=(Error&&)
noexcept =
default;
39 Error& operator=(Error
const&)
noexcept =
delete;
40 [[nodiscard]] Error(Error&&)
noexcept =
default;
41 [[nodiscard]] Error(Error
const&)
noexcept =
delete;
43 LL_CONSTEXPR23 Error()
noexcept =
default;
44 LL_CONSTEXPR23 ~Error()
noexcept =
default;
46 LL_CONSTEXPR23 Error(std::unique_ptr<ErrorInfoBase> i) noexcept : mInfo(std::move(i)) {}
48 LL_CONSTEXPR23 Error(::nonstd::unexpected_type<::ll::Error> i) noexcept : Error(std::move(i.error())) {}
50 LL_CONSTEXPR23
operator bool()
const noexcept {
return mInfo !=
nullptr; }
52 LL_CONSTEXPR23
operator Unexpected()
noexcept {
53 return ::nonstd::make_unexpected<Error>(std::in_place, std::move(mInfo));
56 LLNDAPI std::string message()
const noexcept;
58 LLNDAPI std::string message(std::string_view locale)
const noexcept;
61 LL_CONSTEXPR23
bool isA()
noexcept {
62 return mInfo ?
typeid(T) ==
typeid(*mInfo) :
false;
65 LL_CONSTEXPR23 T& as()
noexcept {
66 return *
static_cast<T*
>(mInfo.get());
68 LLAPI Error& join(Error)
noexcept;
70 Error& join(Unexpected err)
noexcept {
return join(std::move(err.error())); }
72 LLAPI Error
const& log(
io::Logger&, io::LogLevel = io::LogLevel::Error)
const noexcept;
74 LLAPI Error
const& log(
io::Logger&, std::string_view locale, io::LogLevel = io::LogLevel::Error)
const noexcept;
76 LLAPI Error
const& log(
CommandOutput&, CommandOutputMessageType = CommandOutputMessageType::Error)
const noexcept;
80 std::string_view locale,
81 CommandOutputMessageType = CommandOutputMessageType::Error)
const noexcept;
84 log(
brstd::function_ref<
void(std::string_view msg)> out, std::string_view locale = {})
const noexcept;
87struct StringError : ErrorInfoBase {
89 StringError(std::string str) : str(std::move(str)) {}
90 std::string message(std::string_view)
const noexcept override {
return str; }
92struct ErrorCodeError : ErrorInfoBase {
94 ErrorCodeError(std::error_code ec) : ec(ec) {}
95 std::string message(std::string_view)
const noexcept override {
return ec.message(); }
97[[nodiscard]]
inline Unexpected forwardError(
::ll::Error& err)
noexcept {
98 return ::nonstd::make_unexpected(std::move(err));
101[[nodiscard]]
inline Unexpected makeSuccessed() noexcept { return ::nonstd::make_unexpected(Error{}); }
103template <std::derived_from<::ll::ErrorInfoBase> T,
class... Args>
104[[nodiscard]] LL_CONSTEXPR23
inline Unexpected makeError(Args&&... args)
noexcept {
105 return ::nonstd::make_unexpected<Error>(std::in_place, std::make_unique<T>(std::forward<Args>(args)...));
107[[nodiscard]]
inline Unexpected makeStringError(std::string str)
noexcept {
108 return makeError<StringError>(std::move(str));
111[[nodiscard]]
inline Unexpected makeErrorCodeError(std::error_code ec)
noexcept {
112 return makeError<ErrorCodeError>(ec);
115[[nodiscard]]
inline Unexpected makeErrorCodeError(std::errc ec)
noexcept {
116 return makeError<ErrorCodeError>(make_error_code(ec));
119LLNDAPI Unexpected makeExceptionError(std::exception_ptr
const& exc = std::current_exception()) noexcept;
123namespace nonstd::expected_lite {
125class bad_expected_access<::
ll::Error> :
public bad_expected_access<void> {
126 std::shared_ptr<::ll::Error> mError;
127 std::string mMessage;
130 explicit bad_expected_access(
::ll::Error& e) noexcept
131 : mError(::std::make_shared<::ll::Error>(::std::move(e))),
132 mMessage(mError->message()) {}
134 char const* what()
const noexcept override {
return mMessage.c_str(); }
138 ::ll::Error const& error()
const& {
return *mError; }
140 ::ll::Error&& error() && { return ::std::move(*mError); }
142 ::ll::Error const&& error()
const&& { return ::std::move(*mError); }
146 static void rethrow(
::ll::Error const& e) {
throw bad_expected_access<::ll::Error>{
const_cast<::
ll::Error&
>(e)}; }
Definition CommandOutput.h:20
Definition function_ref.h:60