2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2016-03-29 16:37:21 +00:00
|
|
|
#pragma once
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
#include <string>
|
2021-12-28 02:32:22 +00:00
|
|
|
#include <string_view>
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
#include "libtransmission/tr-macros.h"
|
2014-06-23 02:38:53 +00:00
|
|
|
|
|
|
|
/** @brief Structure holding error information. */
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_error
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
public:
|
|
|
|
tr_error() = default;
|
|
|
|
|
|
|
|
tr_error(int code, std::string message)
|
|
|
|
: message_{ std::move(message) }
|
|
|
|
, code_{ code }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto code() const noexcept
|
|
|
|
{
|
|
|
|
return code_;
|
|
|
|
}
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto message() const noexcept
|
|
|
|
{
|
|
|
|
return std::string_view{ message_ };
|
|
|
|
}
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
[[nodiscard]] constexpr auto has_value() const noexcept
|
|
|
|
{
|
|
|
|
return code_ != 0;
|
|
|
|
}
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
[[nodiscard]] constexpr operator bool() const noexcept
|
|
|
|
{
|
|
|
|
return has_value();
|
|
|
|
}
|
2023-01-25 18:20:10 +00:00
|
|
|
|
2023-12-01 22:48:16 +00:00
|
|
|
void set(int code, std::string&& message)
|
|
|
|
{
|
|
|
|
code_ = code;
|
|
|
|
message_ = std::move(message);
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
void set(int code, std::string_view message)
|
|
|
|
{
|
|
|
|
code_ = code;
|
|
|
|
message_.assign(message);
|
|
|
|
}
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2023-12-01 22:48:16 +00:00
|
|
|
void set(int code, char const* const message)
|
|
|
|
{
|
|
|
|
set(code, std::string_view{ message != nullptr ? message : "" });
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
void prefix_message(std::string_view prefix)
|
|
|
|
{
|
|
|
|
message_.insert(std::begin(message_), std::begin(prefix), std::end(prefix));
|
|
|
|
}
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
// convenience utility for `set(errno, tr_strerror(errno))`
|
|
|
|
void set_from_errno(int errnum);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
private:
|
|
|
|
/** @brief Error message */
|
|
|
|
std::string message_;
|
|
|
|
|
|
|
|
/** @brief Error code, platform-specific */
|
|
|
|
int code_ = 0;
|
|
|
|
};
|